android 编程 9.3.1
还是继续修改上次项目的代码,但首先要安装apache,官网下载的步骤非常复杂我就不说了,https://jingyan.baidu.com/article/29697b912f6539ab20de3cf8.html这是我在网上找到的教程,我也是根据这个才下载下来的。咱们还是主要关注安卓的学习。下载后腰找到你安装目录里D:\Apache24\htdocs这个地方里新建个get_data.xml文件<apps><app>
<id>1</id>
<name>Google Maps</name>
<version>1.0</version>
</app>
<app>
<id>2</id>
<name>Chrome</name>
<version>2.1</version>
</app>
<app>
<id>3</id>
<name>Google Play</name>
<version>2.3</version>
</app>
</apps>
完后用管理员模式打开控制台,进入apache24/bin目录输入apacheMonitor启动服务器,还要在屏幕右下角右键点击小图标,选open service,
然后修改mainactivity.javapackage com.example.xinwei.networktest;
import android.app.DownloadManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private static final String TAG = "MainActivity";
TextView responseText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendRequest = (Button)findViewById(R.id.send_request);
responseText = (TextView)findViewById(R.id.response_text);
sendRequest.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view.getId()==R.id.send_request){
//sendRequestWithHttpURLConnection();
sendRequestWithOkHttp();
}
}
private void sendRequestWithOkHttp() {
new Thread(){
@Override
public void run(){
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("http://192.168.56.1/get_data.xml").build();
try {
Response response = client.newCall(request).execute();
String responseData = response.body().string();
parseXMLWithPull(responseData);
//showResponse(responseData);
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
private void parseXMLWithPull(String responseData) {
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xmlPullParser = factory.newPullParser();
xmlPullParser.setInput(new StringReader(responseData));
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(TAG,"id is"+id);
Log.d(TAG,"name is"+name);
Log.d(TAG,"version is"+version);
}
break;
default:
break;
}
eventType=xmlPullParser.next();
}
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void sendRequestWithHttpURLConnection() {
new Thread() {
@Override
public void run() {
HttpURLConnection connection = null;
BufferedReader reader=null;
try {
URL url = new URL("http://www.fishC.com");
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-type", "application/x-java-serialized-object");
Log.d("MainActivity", ""+connection.getResponseCode());
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
InputStream in = connection.getInputStream();
reader=new BufferedReader(new InputStreamReader(in));
StringBuilder response=new StringBuilder();
String line;
while ((line=reader.readLine())!=null){
response.append(line);
}
showResponse(response.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (connection!=null){
connection.disconnect();
}
}
}
}.start();
}
private void showResponse(final String response) {
runOnUiThread(new Runnable() {
@Override
public void run() {
responseText.setText(response);
}
});
}
}
里面第56行这句Request request = new Request.Builder().url("http://192.168.56.1/get_data.xml").build();
http后面需要你查找自己的局域网ipv4地址,在控制台用ipconfig命令。(这里我试过如果按书上写的地址是连接失败的)这样就可以成功的在日志上打印出结果来了。
页:
[1]