json解析
在解析之前首先需要找一个json在线解析的网址,便于查看其中的结构,{ } 代表的是json对象,[ ] 代表的是json数组
这是一个json 在线解析的网址 : http://www.bejson.com/jsonviewernew/
同时用到的还有免费的接口网站: http://www.avatardata.cn/ 阿凡达数据
这是一个简单的免费查看每日星座的api接口,只是用来练习json解析的
在接口网站中复制这个网址,复制到任意网址,会显示json数据,然后复制这些数据到json数据里,在点开视图就可以看到其中的
结构,然后就可以根据其中的结构来解析json数据
package com.zdsft.administrator.fourth.uritest;
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.widget.Button;
import android.widget.TextView;
import com.zdsft.administrator.fourth.R;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Created by Administrator on 2017/3/25.
* 今天的星座运势
*/
public class MyConstellationTest extends Activity{
//测试按钮
private Button test_btn;
//定义字符串url在接口网站中可以找到
private String url = "http://api.avatardata.cn/Constellation/Query?key=3e454bc55a5b423aa7c66e1d2978b542&consName=%E7%8B%AE%E5%AD%90%E5%BA%A7&type=today";
//用来显示查询到的结果
private TextView constellation_tvshow;
//接受解析后的数据
private String s_show ;
//定义handler,用于子线程与主线程的通信,
Handler handler;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.constellation);
//实例化控件
test_btn = (Button) findViewById(R.id.test_btn);
constellation_tvshow = (TextView) findViewById(R.id.constellation_tvshow);
test_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//因为activity中不能做耗时的操作所以开启线程来访问网络
new Thread(){
@Override
public void run() {
//访问网络
getUrl(url);
}
}.start();
}
});
//创建handle用于与子线程通信
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if (msg.what==1){
constellation_tvshow.setText(s_show);
}
}
};
}
//从网络获取数据
private void getUrl(String url){
//1.将字符串url转换成路径url
try {
URL url1 = new URL(url);
//2.打开链接
HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
//3.获取输入流
InputStream ips = conn.getInputStream();
//4.创建字符串来接受
StringBuffer sb = new StringBuffer();
byte[] b = new byte[1024];
int len = 0;
while ((len = ips.read(b))!=-1){
sb.append(new String(b,0,len));
}
Log.e("sb-------",sb.toString());
//关闭流
ips.close();
//关闭连接
conn.disconnect();
//将获得的字符串解析
pullJson(sb.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
//json解析
private void pullJson(String str){
//解析字符串
try {
JSONObject jsonObject = new JSONObject(str);
JSONObject jsonObject1 = jsonObject.getJSONObject("result1");
String name = jsonObject1.getString("name");
String content = jsonObject1.getString("summary");
s_show = name+"今天的运势:"+content;
//创建message通知主线程更改Ui
Message msg = new Message();
msg.what = 1;
handler.sendMessage(msg);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
//注意不要忘了在AndroidManifest.xml中注册访问网络权限<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zdsft.administrator.fourth">
<!--访问网络权限-->
<uses-permission android:name="android.permission.INTERNET" />
下面是布局文件constellation.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/test_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试按钮"/>
<TextView
android:id="@+id/constellation_tvshow"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20.0sp"
android:textColor="#00f"
android:gravity="top|left"
android:paddingLeft="20dp"/>
</LinearLayout>
这样解析就完了,能显示当天狮子座的运势