关于通过get方法请求json数据出现数据不全的解决方案

一、JSON
什么是 JSON ?
JSON语法是javascrapt的语法的子集
JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation)
JSON 是轻量级的文本数据交换格式
JSON 独立于语言 *
JSON 具有自我描述性,更易理解

JSON 值可以是:
数字(整数或浮点数)
字符串(在双引号中)
逻辑值(true 或 false)
数组(在方括号中)
对象(在花括号中)
中间用逗号隔开
null
(1)json表示数组的方法(在方括号中)
举例如下,这个employees对象包含了两个关于员工记录的数组,每个数组里包含了两条信息:名字(name)、工资(money)。

{“employees”:[{“name”:”tom”,”money”:”20”},{“name”:”jim”,”money”:”20”}] }
(2)JSON创建对象的方式(在花括号中)
person={“name”:”tom”,”age”:20}

二、get()方法请求网络数据
相信这一块大家应该都熟悉,直接上端常用的请求代码
(1)创建的工具类,返回list集合类
public class JsonTools {
public static List getJson(String url) throws Exception {
//创建解析后数据的存储对象
List list = new ArrayList();
//创建HttpGet对象
HttpGet request = new HttpGet(url);
//BasicHTtpParams对象用来存储键值对
HttpParams httpParameters = new BasicHttpParams();
//设置超时连接
int timeoutConnection = 3000;

HttpConnectionParams.setConnectionTimeout(httpParameters,timeoutConnection);
//获得response对象
HttpResponse response = new DefaultHttpClient(httpParameters).execute(request);
//判断连接 是否成功
if (response.getStatusLine().getStatusCode() == 200) {
//用getEntity方法得到josn数据
String json = EntityUtils.toString(response.getEntity(), “gbk”);
//创建jsono数组,开始解析
JSONArray jsonArray = new JSONArray(json);
for (int i = 0; i < jsonArray.length(); i++) {
//得到json解析后数组中的值
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
//定义对象,用来存储解析后的数据
Recipe recipe = new Recipe();
//存储数据
recipe.setRecipeName(jsonObject.getString(“recipeName”));
recipe.setRecipeImageUrl(jsonObject.getString(“recipeImageUrl”));

list.add(recipe);

}
}
return list;
}

(2)输入URl,返回的是list集合类
String url = baseUrl + “YiMenuOS/webService/allDishesByPage?index=”+index+”&format=json”;
httpClient = new DefaultHttpClient(httpParameters);
//创建HttpGet对象
HttpGet get = new HttpGet(url);
HttpResponse response = httpClient.execute(get);
SQLiteHelper sqLiteHelper = new SQLiteHelper(context);
db = sqLiteHelper.getWritableDatabase();
//判断请求响应状态码,状态码为200表示服务器成功响应了客户端的请求
if (response.getStatusLine().getStatusCode() == 200) {

                StringBuilder builder = new StringBuilder();
                //通过使用getEntity()获得返回的json类型的数据存入输入流中
                InputStream in=response.getEntity().getContent();
                //将输入流放入缓冲区中
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(in));
                //在缓冲区中每次读取一行赋值给s,同事将s传递给bulider
                String s;
                while((s=reader.readLine())!=null)
                {

                    builder.append(s);
                }
                //得到json解析后数组的值
                JSONObject jsonObject = new JSONObject(builder.toString());
                System.out.println("dishes::::::::::::" + jsonObject);
                //得到需要的数组的类名,创建json数组并解析
                JSONArray jsonArray = jsonObject.getJSONArray("dishes");
                int len = jsonArray.length();
                //这户是打印的log检测json数组的长度,方便调试
                System.out.println("长度"+len);
                    List<Receive> list=new ArrayList();
                for (int i = 0; i < len; i++) {
                //得到json解析后数组中的值
                    JSONObject obj = (JSONObject) jsonArray.get(i);
            //定义对象存储数据

            Receive receive=new Receive();
            receive.id=obj.getInt("ID");
            receive.name=obj.getString("name");
            receive.age=obj.getInt("age");
            list.add(receive);
            }

        }
    }catch (ConnectTimeoutException conExc) {

        return false;

    } catch (SocketTimeoutException socketTimeout) {

        return false;

    } finally {

        if(httpClient!=null)
            httpClient.getConnectionManager().shutdown();
    }

    return true;
    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值