Android 之 网络访问服务器,解析JSON数据(返回无乱码)


Android 之 网络访问服务器,解析JSON数据


分享到:
评论(0) |2014-04-21 |发布  飞菲

提示:在访问网络,或者服务器的数据一定要注意 网络权限的声明: 
1 <uses-permission android:name="android.permission.INTERNET"/>


001 package com.sun.parsejsondata;
002  
003 import java.io.IOException;
004  
005 import org.apache.http.HttpEntity;
006 import org.apache.http.HttpResponse;
007 import org.apache.http.client.ClientProtocolException;
008 import org.apache.http.client.HttpClient;
009 import org.apache.http.client.methods.HttpGet;
010 import org.apache.http.impl.client.DefaultHttpClient;
011 import org.apache.http.util.EntityUtils;
012 import org.json.JSONArray;
013 import org.json.JSONException;
014 import org.json.JSONObject;
015  
016 import android.test.AndroidTestCase;
017 import android.util.Log;
018  
019 /*{
020     "index_num":"121286",
021     "total":300,
022     "result_num":10,
023     "web_url":"http://www.aibang.com",
024     "wap_url":"http://wap.aibang.com",
025     "bizs":{
026         "biz":[{
027             "id":"19180584-418982077",
028             "name":"北京饭店",
029             "addr":"东城区东长安街号(近王府井大街)",
030             "tel":"4006778919-1011",
031             "cate":"宾馆酒店;美食;其他美食;五星级酒店;婚宴酒店;结婚",
032             "rate":4,
033             "cost":0,
034             "desc":"北京饭店始建于年,位于北京市中心,步行五,毗邻皇宫紫禁城,与繁华的...",
035             "dist":"-1",
036             "lng":"116.411096",
037             "lat":"39.9084880",
038             "img_url":"http://img0.aibangjuxin.com/ipic/15f6cfa4e1cc98cf_8.jpg"
039         },
040         ...
041         ]
042     }
043 }*/
044  
045  
046 /**
047  *  获取网络数据 -- json 格式,并进行解析
048  * @author Administrator
049  *
050  */
051 public class ParseJson  {
052  
053     /**
054      * 解析JSON 数据
055      * @throws Exception
056      */
057     public void parseJson(String jsonStr) throws Exception{
058          
059         JSONObject root = new JSONObject(jsonStr);
060          
061         // 编号
062         String index_num = (String) root.get("index_num");
063         int total = root.getInt("total");
064         int result_num = root.getInt("result_num");
065          
066         JSONObject bizsO = root.getJSONObject("bizs");
067         JSONArray arr = bizsO.getJSONArray("biz");
068         for (int i = 0; i < arr.length(); i++) {
069             JSONObject bizs_ =arr.getJSONObject(i);
070             String id = bizs_.getString("id");
071             String addr = bizs_.getString("addr");
072             Log.i("msg","编号:"+id+"',地址"+addr);
073         }
074          
075     }
076      
077     /**
078      * 访问网络数据
079      * @throws IOException
080      * @throws ClientProtocolException
081      */
082     public String accessIntent() throws Exception{
083          
084         // 请求服务器地址
085         String path = "http://169.254.111.39:8080/myjson.html";
086          
087         // Get 方式进行请求
088         HttpGet httpGet = new HttpGet(path);
089          
090         // 默认客户端
091         HttpClient httpClient = new DefaultHttpClient();
092          
093         // 请求响应
094         HttpResponse httpResponse = httpClient.execute(httpGet);
095          
096         // 判断响应结果
097         if(httpResponse.getStatusLine().getStatusCode() == 200){ // 响应成功
098              
099             HttpEntity httpEntity = httpResponse.getEntity();
100             String result = EntityUtils.toString(httpEntity, "gbk");
101             Log.i("msg",result);
102             return result;
103         }
104          
105         return null;
106     }  
107      
108 }


post的请求方式: 

01 public class HttpURLActivity extends Activity {
02     @Override
03     public void onCreate(Bundle savedInstanceState) {
04         super.onCreate(savedInstanceState);
05         System.out.println("start url...");
06         String url = "http://192.168.2.112:8080/JustsyApp/Applet";
07         // 第一步,创建HttpPost对象
08         HttpPost httpPost = new HttpPost(url);
09   
10         // 设置HTTP POST请求参数必须用NameValuePair对象
11         List<NameValuePair> params = new ArrayList<NameValuePair>();
12         params.add(new BasicNameValuePair("action""downloadAndroidApp"));
13         params.add(new BasicNameValuePair("packageId""89dcb664-50a7-4bf2-aeed-49c08af6a58a"));
14         params.add(new BasicNameValuePair("uuid""test_ok1"));
15   
16         HttpResponse httpResponse = null;
17         try {
18             // 设置httpPost请求参数
19             httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
20             httpResponse = new DefaultHttpClient().execute(httpPost);
21             //System.out.println(httpResponse.getStatusLine().getStatusCode());
22             if (httpResponse.getStatusLine().getStatusCode() == 200) {
23                 // 第三步,使用getEntity方法活得返回结果
24                 String result = EntityUtils.toString(httpResponse.getEntity());
25                 System.out.println("result:" + result);
26                 T.displayToast(HttpURLActivity.this"result:" + result);
27             }
28         catch (ClientProtocolException e) {
29             e.printStackTrace();
30         catch (IOException e) {
31             e.printStackTrace();
32         }
33         System.out.println("end url...");
34         setContentView(R.layout.main);
35     }
36 }




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值