您的位置:首页 > 移动开发 > Objective-C

利用android自带的JSONObject解析json数据

2017-03-08 22:24 615 查看
话不多说,直接上代码:

json数据:http://www.haoservice.com/docs/6

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lhc.myhttp.MainActivity"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="点击查询当前天气!"/>
</LinearLayout>
主文件:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import static android.content.ContentValues.TAG;

public class MainActivity extends Activity {
//控件初始化
private Button button;
private  final String address = "http://apis.haoservice.com/weather?cityname=成都&key=你的Key";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//设置按钮的单击事件
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//网络请求只能在子线程中进行
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(address);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
InputStream in = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder builder = new StringBuilder();
String line;
//判断是否存在数据
while ((line = reader.readLine()) != null) {
builder.append(line);
//将数据转化为字符串类型
String data = builder.toString();

//创建一个方法,用来解析得到的json数据
parseison(data);
//关闭相关资源
reader.close();
in.close();
conn.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}

private void parseison(String data) {
try {
//判断数据获取是否成功
JSONObject jsonobject = new JSONObject(data);
int error_code = jsonobject.getInt("error_code");
if (error_code == 0) {
JSONObject jsonobject_1 = jsonobject.getJSONObject("result");
//当前实况天气
JSONObject json_2 = jsonobject_1.getJSONObject("sk");
String temp = json_2.getString("temp");
String wind_direction = json_2.getString("wind_direction");
String wind_strength = json_2.getString("wind_strength");
String humidity = json_2.getString("humidity");
String time = json_2.getString("time");
Log.d(TAG, "当前温度:"+temp+"\n"+"当前风向:"+wind_direction+"\n"+"当前风力:"+wind_strength+"\n"+"当前湿度:"+humidity+"\n"+"更新时间:"+time);
//当天的天气状况JSON数据
JSONObject today = jsonobject_1.getJSONObject("today");
String output = today.getString("city")+"\n"+
today.getString("date_y")+"\n"+
today.getString("week")+"\n"+
today.getString("temperature")+"\n"+
today.getString("weather")+"\n"+
today.getString("fa")+"\n"+
today.getString("fb")+"\n"+
today.getString("wind")+"\n"+
today.getString("dressing_index")+"\n"+
today.getString("dressing_advice")+"\n"+
today.getString("uv_index")+"\n"+
today.getString("comfort_index")+"\n"+
today.getString("wash_index")+"\n"+
today.getString("travel_index")+"\n"+
today.getString("exercise_index")+"\n"+
today.getString("drying_index");
Log.d(TAG, "今天的天气情况是:"+output);
} else {
Log.d(TAG, "parseison: 解析错误");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  json httpurlconnection