解析 JSON 数据 使用官方提供的 JSONObject,也可以使用谷歌的开源库 GSON.
package com.gaoo.httpclientdemo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
public class MainActivity extends AppCompatActivity {
Button send;
TextView show;
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 0x123) {
String result = (String) msg.obj;
show.setText(result.toString());
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send = (Button) findViewById(R.id.send);
show = (TextView) findViewById(R.id.show);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendRequest();
Toast.makeText(MainActivity.this, "点我了", Toast.LENGTH_SHORT).show();
}
}
);
}
private void sendRequest() {
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection = null;
try {
URL url = new URL("http://10.0.3.2/dataJson.json");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(8000);
connection.setReadTimeout(8000);
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(new
InputStreamReader(in));
String response = new String();
String line;
while ((line = reader.readLine()) != null) {
response += line;
}
Log.d("----response--- ", response);
parseJSONWithJSONObject(response);
Message message = new Message();
message.what = 0x123;
message.obj = response.toString();
mHandler.sendMessage(message);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
}).start();
}
private void parseJSONWithJSONObject(String jsonData) {
Gson gson = new Gson();
List<App> appList = gson.fromJson(jsonData, new TypeToken<List<App>>() {}.getType());
for (int i = 0; i < appList.size(); i++) {
App app = appList.get(i);
Log.e("MainActivity", app.toString());
}
}
}
使用的javaBean
package com.gaoo.httpclientdemo;
/**
* Created by Administrator on 2016/7/7.
*/
public class App {
public String id;
public String name;
public String version;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
@Override
public String toString() {
return "App{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", version='" + version + '\'' +
'}';
}
}