【问题标题】:Can't get complete json data through webservice无法通过webservice获取完整的json数据
【发布时间】:2014-11-13 13:36:21
【问题描述】:

我正在尝试通过https://www.cyb3rpirat3s.in/android/leaderboard.php 网站以 json 的形式获取大量数据..但问题是从上面的链接获取一些数据后,logcat 出现无法解析 json 的错误(由于不完整获取 json)。我是 android 新手,不知道我应该如何处理大量的 json。我什至已经为它搜索过,但无法找到相关信息。 提前致谢

public JSONObject makeHttpRequest(String url, List<NameValuePair> params) {

        try {
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));
                HttpResponse httpResponse = httpClient.execute(httpPost);
                int status=httpResponse.getStatusLine().getStatusCode();
                if(status==200){
                    HttpEntity httpEntity = httpResponse.getEntity();
                    String data=EntityUtils.toString(httpEntity);
                    json=data.split("<!--")[0];
                    Log.d("sdjb",json);
                }           
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            jObj = new JSONObject(json);
           Log.d("sf",jObj.getString("success"));
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
            return jObj;
    }

logcat 错误:

11-13 19:03:22.873: E/JSON Parser(7979): 解析数据时出错 org.json.JSONException: Value [{"rank":1,"1":"11","level": "11","0":"the_godfather","piratename":"the_godfather"},{"rank":2,"1":"6","level":"6","0":"ShiniGami ","piratename":"ShiniGami"},{"rank":3,"1":"6","level":"6","0":"hurricane","piratename":"hurricane"} ,{"rank":4,"1":"6","level":"6","0":"the_batman","piratename":"the_batman"},{"rank":5,"1 ":"6","level":"6","0":"the_boatswain","piratename":"the_boatswain"},{"rank":6,"1":"5","level": "5","0":"battlemonger","piratename":"battlemonger"},{"rank":7,"1":"5","level":"5","0":"MishuAnubis ","piratename":"MishuAnubis"},{"rank":8,"1":"5","level":"5","0":"YogeshPatil","piratename":"YogeshPatil"} ,{“排名”:

【问题讨论】:

  • 对不起我的错误......

标签: android json


【解决方案1】:

试试这个方法可能对你有帮助。

public static JSONObject JsonHttpRequest(String url, String method,
        List<NameValuePair> params) {

    InputStream is = null;
    JSONObject jObj = null;
    String json = "";
    try {


        if (method == "GET") {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "" + paramString;
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        }

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}

【讨论】:

  • 这个方法不行……因为get方法会限制json的大小
  • 但你可以简单地删除 if 条件..但为什么不投票??
【解决方案2】:

你拿错了钥匙

Log.d("sf",jObj.getString("success"));

没有名为"success"的键

这样做:

jObj.getString("piratename");
jObj.getString("level");
jObj.getString("rank");
jObj.getString("0");
jObj.getString("1");

【讨论】:

  • 异常在到达 Log.d("sf",jObj.getString("success"))..你告诉我的情况下会给我 nullpointerexception ...我没有反对你
猜你喜欢
  • 2017-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多