json解析+介面+網路請求+歷史的今天
阿新 • • 發佈:2018-12-04
一、一個例子查詢歷史的今天發生的事件
1.去聚合資料或者阿凡達 申請歷史的今天的APPkey
2.檢視介面使用規範:
3.根據獲取的資料:請求結果為:{"total":18,"result":[{"year":2003,"month":4,"day":9,"title":"著名劇作家吳祖光逝世","type":1},{"year":2002,"month":4,"day":9,"title":"中國出版集團在京成立","type":1},{"year":1998,"month":4,"day":9,"title":"麥加朝覲慘劇","type":1},{"year":1997,"month":4,"day":9,"title":"吳作人逝世","type":1},{"year":1997,"month":4,"day":9,"title":"國際商會三十二屆世界大會在滬舉行","type":1}],"error_code":0,"reason":"Succes"} 建立相應的javabean類。
二、程式碼部分
MainActivity.java類
<span style="font-size:18px;">package com.example.t22_09_jsontwo; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import com.google.gson.Gson; public class MainActivity extends Activity { private HttpResponse httpResponse = null; private HttpEntity httpEntity = null; private Button bt; private EditText et1,et2; private TextView showtv; String sp; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt=(Button) findViewById(R.id.subb); et1=(EditText) findViewById(R.id.yue); et2=(EditText) findViewById(R.id.ri); showtv=(TextView) findViewById(R.id.show); sp=GetStringURL(); bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub new Thread(networkTask).start(); } }); } private String GetStringURL() { // TODO Auto-generated method stub String s="http://api.avatardata.cn/HistoryToday/LookUp?key=c43bdd056d034bf6be97e12993bf8362"; String p="&type=1&page=1&rows=5"; String n="&yue="+et1.getText().toString(); String m="&ri="+et2.getText().toString(); return s+n+m+p; } Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); Bundle data = msg.getData(); String val = data.getString("value"); Log.i("mylog", "請求結果為:" + val); Gson gson = new Gson(); Aaa w = gson.fromJson(val, Aaa.class); List<Bbb> bs=new ArrayList<Bbb>(); bs=w.result; StringBuffer temp=new StringBuffer(); for(int i=0;i<bs.size();i++) { String s=bs.get(i).year+"年"+bs.get(i).month+"月 "+bs.get(i).day+"日 "+" 事件:"+bs.get(i).title+"\n"; temp.append(s); //System.out.println(bs.get(i).month+"月 "+bs.get(i).day+"日 "+" 事件:"+bs.get(i).title); } showtv.setText(temp); } }; Runnable networkTask = new Runnable() { @Override public void run() { URI r=null; sp=GetStringURL(); try { r=new URI(sp); } catch (URISyntaxException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } HttpGet httpGet = new HttpGet(r); // 生成一個Http客戶端物件 HttpClient httpClient = new DefaultHttpClient(); // 使用Http客戶端傳送請求物件 InputStream inputStream = null; try { // 返回給我們的響應 httpResponse = httpClient.execute(httpGet); // 發給我們的響應的內容 httpEntity = httpResponse.getEntity(); inputStream = httpEntity.getContent(); BufferedReader reader = new BufferedReader( new InputStreamReader(inputStream)); String result = ""; String line = ""; while ((line = reader.readLine()) != null) { result = result + line; } Message msg = new Message(); Bundle data = new Bundle(); data.putString("value", result); msg.setData(data); handler.sendMessage(msg); } catch (Exception e) { e.printStackTrace(); } finally { try { inputStream.close(); } catch (Exception e) { e.printStackTrace(); } } } }; } </span>
兩個bean類
<span style="font-size:18px;">package com.example.t22_09_jsontwo;
import java.util.List;
public class Aaa {
int total;
List<Bbb> result;
int error_code;
String reason;
}
</span>
<span style="font-size:18px;">package com.example.t22_09_jsontwo; public class Bbb { int year; int month; int day; String title; @Override public String toString() { // TODO Auto-generated method stub return year+"-"+month+"-"+title; } } </span>
佈局檔案
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/yue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入月份"
android:inputType="text"
/>
<EditText
android:id="@+id/ri"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入幾號"
android:inputType="text"
/>
<Button
android:id="@+id/subb"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="獲取資料" />
<TextView
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FFA07A"
android:textSize="20sp"
android:layout_margin="10dp"
/>
</LinearLayout></span>
演示結果:
程式碼有點簡單粗暴 嘻嘻