json解析+接口+网络请求+历史的今天

来源:互联网 发布:剑网3捏脸数据 编辑:程序博客网 时间:2024/06/03 20:45

一、一个例子查询历史的今天发生的事件

  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;@Overridepublic 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() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubnew Thread(networkTask).start();}});}private String GetStringURL() {// TODO Auto-generated method stubString 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() {@Overridepublic 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() {@Overridepublic void run() {URI r=null;     sp=GetStringURL(); try {r=new URI(sp);} catch (URISyntaxException e1) {// TODO Auto-generated catch blocke1.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;@Overridepublic String toString() {// TODO Auto-generated method stubreturn 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>

演示结果:



代码有点简单粗暴  嘻嘻

0 0
原创粉丝点击