如何使用OkHttp获取Json字符串呢?
									
下文笔者讲述使用OkHttp获取url返回的Json字符串,如下所示
http://localhost/test/path/jsonpage
				 
				OkHttp获取JSON的实现思路
1.申明一个OKHttpClient对象 2.构建request 3.获取response返回值 4.解析返回值即可例:
http://localhost/test/path/jsonpage
{"users":[
    {"userName":"java", "lastName":"mao"}, 
    {"firstName":"adeal", "lastName":"niuniu"} 
]}
OkHttp代码
try {
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder()
        .url(urls[0])
        .build();
    Response responses = null;
    try {
        responses = client.newCall(request).execute();
    } catch (IOException e) {
        e.printStackTrace();
    }
    String jsonData = responses.body().string();
    JSONObject Jobject = new JSONObject(jsonData);
    JSONArray Jarray = Jobject.getJSONArray("users");
    for (int i = 0; i < Jarray.length(); i++) {
        JSONObject object     = Jarray.getJSONObject(i);
    }
}
									
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

			
               
               
               
               
          
