码迷,mamicode.com
首页 > Web开发 > 详细

网络解析json

时间:2017-02-18 14:27:17      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:data   generated   throw   start   request   instance   url   ica   method   

public class myActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}


public void buooo(View v) throws Exception{

new Thread(){
public void run() {

try {
getdate();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}.start();
}
private void getdate() throws Exception {
// TODO Auto-generated method stub
URL url=new URL(
"http://v.juhe.cn/toutiao/index?type=yule&key=c8fe066ae002d20891fbc48a1783a1ee");
HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.connect();
StringBuffer stringBuffer= new StringBuffer();
final StringBuilder sb1 = new StringBuilder();
String data = null;
InputStream inputStream=httpURLConnection.getInputStream();
BufferedReader be=new BufferedReader(new InputStreamReader(inputStream));
while ((data = be.readLine()) != null) {
stringBuffer.append(data);
}
JSONObject jsonObject=new JSONObject(stringBuffer.toString());
jsonObject=jsonObject.getJSONObject("result");
JSONArray array=jsonObject.getJSONArray("data");
jsonObject = (JSONObject)array.get(1);
sb1.append("标题:" + jsonObject.get("title") + "\r\n");
sb1.append("时间:" + jsonObject.get("date") + "\r\n");
sb1.append("类型:" + jsonObject.get("category") + "\r\n");
sb1.append("作者:" + jsonObject.get("author_name") + "\r\n");
runOnUiThread(new Runnable() {

@Override
public void run() {
// 就相当于在UI线程中运行一样
Toast.makeText(getApplicationContext(), sb1.toString(), 1)
.show();
}
});
inputStream.close();
be.close();

};
}

网络解析json

标签:data   generated   throw   start   request   instance   url   ica   method   

原文地址:http://www.cnblogs.com/YFeiY7/p/6413014.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!