android 编程 9.4.1
本帖最后由 alltolove 于 2017-11-5 08:17 编辑在你的apache目录下的htdocs目录里再新建个get_data.json文件[{"id":"5","version":"5.5","name":"Clash of Clans"},
{"id":"6","version":"7.0","name":"Boom Beach"},
{"id":"7","version":"3.5","name":"Clash Royale"}]
然后修改上个项目的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.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
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 javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
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.json").build();
try {
Response response = client.newCall(request).execute();
String responseData = response.body().string();
parseJSONWithJSONObject(responseData);
//parseXMLWithSAX(responseData);
//parseXMLWithPull(responseData);
//showResponse(responseData);
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
private void parseJSONWithJSONObject(String responseData) {
JSONArray jsonArray = null;
try {
jsonArray = new JSONArray(responseData);
for (int i=0; i<jsonArray.length();i++){
JSONObject jsonObject=jsonArray.getJSONObject(i);
String id = jsonObject.getString("id");
String name = jsonObject.getString("name");
String version = jsonObject.getString("version");
Log.d(TAG, "parseJSONWithJSONObject: id is"+id);
Log.d(TAG, "parseJSONWithJSONObject: name is"+name);
Log.d(TAG, "parseJSONWithJSONObject: version is"+version);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private void parseXMLWithSAX(String responseData) {
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
XMLReader xmlReader=factory.newSAXParser().getXMLReader();
ContentHandler contentHandler=new ContentHandler();
xmlReader.setContentHandler(contentHandler);
xmlReader.parse(new InputSource(new StringReader(responseData)));
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
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);
}
});
}
}
然后启动apache服务器,启动项目点击模拟器上的按钮就会在控制台的logcat里显示我们解析的数据了~
页:
[1]