利用android自带的JSONObject解析json数据
2017-03-08 22:24
615 查看
话不多说,直接上代码:
json数据:http://www.haoservice.com/docs/6
布局:
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(); } } }); } }
相关文章推荐
- Android使用自带JSONObject解析JSON数据
- 【android】利用volley+Gson POST json请求来接收jsonObject并解析json数据
- Android使用自带JSONObject解析JSON数据 - jCuckoo的专栏 - 博客频道 - CSDN.NET
- Android中JSON数据解析:系统自带JSONObject以及第三方GSON的实现
- Android使用自带JSONObject解析JSON数据
- JSON复杂文档解析 Android自带JSONObject,JSONArray方法
- Android中用jsonObject解析JSON数据
- ios进行restful接口调用和json数据利用自带NSJSONSerialization进行数据解析
- android中用jsonObject解析json数据
- Android中的JSONObject和JSONArray解析json数据
- Android数据解析获取JSONObject的key值-Iterator应用
- android中用jsonObject解析json数据
- android中用jsonObject解析json数据
- 利用JsonObject解析Json数据
- android 学习笔记 解析json格式的数据 JSONObject 和GSON解析json格式数据
- android 利用socket 发送和解析Json数据
- 【Android开发经验】Json数据格式介绍、使用Android自带类库完成Json生成与解析
- android 将数据转换成JSON数据格式并使用JSONObject解析JSON格式的数据
- android中用jsonObject解析json数据
- Json数据格式介绍、使用Android自带类库完成Json生成与解析