Android Json 解析
生活随笔
收集整理的這篇文章主要介紹了
Android Json 解析
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
方法一 使用API解析
json:
{"beaconid":"2397","state":"01","userid":"90"}獲取json的方法
private void sendRequestWithHttpClient(){new Thread(new Runnable() {@Overridepublic void run() {try { // HttpClient對(duì)象 HttpClient httpClient = new DefaultHttpClient(); // HttpGet對(duì)象HttpGet httpGet = new HttpGet("http://192.168.0.103/test.json");HttpResponse httpResponse = httpClient.execute(httpGet);if (httpResponse.getStatusLine().getStatusCode() == 200) {HttpEntity entity = httpResponse.getEntity(); // 取得返回的數(shù)據(jù)String response = EntityUtils.toString(entity, "utf-8");//解析jsonparseJSONWithJSONObject(response);}} catch (IOException e) {e.printStackTrace();}}}).start();}
解析json的方法
private void parseJSONWithJSONObject (String jsonData){try {JSONObject jsonObj = new JSONObject(jsonData);String beaconid = jsonObj.getString("beaconid");String state = jsonObj.getString("state");String uid = jsonObj.getString("userid");Log.d("beaconid","beaconid is"+" "+beaconid);Log.d("state","state is"+ " "+state);Log.d("userid","userid is"+ " "+uid);} catch (JSONException e) {e.printStackTrace();}}最后在MainActivity 調(diào)用?sendRequestWithHttpClient() 方法;
記得在AndroidMainfest.xml
<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /><uses-permission android:name="android.permission.VIBRATE" /><uses-permission android:name="android.hardware.sensor.accelerometer" />?
轉(zhuǎn)載于:https://www.cnblogs.com/stuart/p/4502968.html
總結(jié)
以上是生活随笔為你收集整理的Android Json 解析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 计算机485通讯原理,用RS-485设计
- 下一篇: Android 四大组件 —— 服务