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
- json解析+接口+网络请求+历史的今天
- 网络请求,JSON解析
- 网络请求解析json的方式HttpClient
- 网络请求和JSON解析
- 网络请求和JSON解析
- swift 网络请求&json解析
- Json解析网络请求数据
- 网络请求和JSON解析
- 请求网络解析json串
- 网络请求json解析IllegalStateException,
- 请求第三方接口返回json格式数据的解析
- iOS 自己封装的网络请求,json解析的类
- iOS 自己封装的网络请求,json解析的类
- Android--网络请求与json数据解析的使用
- Android---Volley请求天气接口JSON解析
- 网络接口协议 JSON 解析 Crash 的哪些事
- 利用接口请求数据,以及JSON 解析请求下来的数据
- C#中解析json以及网络请求
- 0630-c代码
- iOS 截屏功能的实现
- iOS开发 - 获取推送证书
- Linux正则表达式与文件格式化处理
- oracle11g for linux 启动/停止服务
- json解析+接口+网络请求+历史的今天
- 初学Java反射之简单明了的反射例子。
- Map(键-值)
- 手动创建WorkSpace方式的iOS多工程联合编译包含Static Library
- tensorflow从0开始(7)——利用tensorflow进行开发的准备工作(续1)
- C#控制台 for循环的标准格式
- java文件IO,关闭顺序
- sql数据库中怎么根据存储过程名字,查出存储过程的内容?
- 十进制和二进制快速转换