运行截图
MainActivity.java
package csdn.example.com.notification.NetWorkTest;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.IOException;
import java.io.StringReader;
import csdn.example.com.notification.R;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class Main6Activity extends AppCompatActivity implements View.OnClickListener {
TextView request_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main6);
request_text = (TextView) findViewById(R.id.request_text);
Button send_Request = (Button) findViewById(R.id.send_request);
send_Request.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId() == R.id.send_request){
Log.d("T","没毛病,老铁");
//sendRequestWithHttpURLConnection();
sendRequestWithOkHttp();
}
}
private void sendRequestWithOkHttp() {
new Thread(new Runnable() {
@Override
public void run() {
try {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://10.7.90.163:8088/get_data.xml")
.build();
Response response = client.newCall(request).execute();
String requestData = response.body().string();
parseXMLWithPull(requestData);
showResponse(requestData);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
private void parseXMLWithPull(String xmlData) {
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xmlPullParser = factory.newPullParser();
xmlPullParser.setInput(new StringReader(xmlData));
int eventType = xmlPullParser.getEventType();
String id = "";
String name = "";
String version = "";
while (eventType != xmlPullParser.END_DOCUMENT){
String nodeName = xmlPullParser.getName();
switch (eventType){
//开始解析某个节点
case XmlPullParser.START_TAG:{
if("id".equals(nodeName)){
id = xmlPullParser.nextText();
}else if ("name".equals(nodeName)){
name = xmlPullParser.nextText();
}else if("version".equals(nodeName)){
version = xmlPullParser.nextText();
}
break;
}
//完成解析某个节点
case XmlPullParser.END_TAG:{
if("app".equals(nodeName)){
Log.d("Main6Activity","id is "+id);
Log.d("Main6Activity","name is "+name);
Log.d("Main6Activity","version is "+version);
}
}
default:
break;
}
eventType = xmlPullParser.next();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void showResponse(final String response) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Log.d("T","没毛病,老铁1"+response);
request_text.setText(response);
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/send_request"
android:text="发送请求"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"
>
<TextView
android:background="@color/colorPrimary"
android:id="@+id/request_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
</LinearLayout>