无法通过Web服务获取完整的json数据

时间:2014-11-13 13:36:22

标签: android json

我试图通过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:值[{“rank”:1,“1”:“11”,“level”: “11”, “0”: “the_godfather”, “piratename”: “the_godfather”},{ “等级”:2 “1”: “6”, “水平”: “6”, “0”:“死神”, “piratename”: “死神”},{ “等级”:3 “1”: “6”, “水平”: “6”, “0”: “飓风”, “piratename”: “飓风”} ,{ “秩”:4 “1”: “6”, “水平”: “6”, “0”: “the_batman”, “piratename”: “the_batman”},{ “秩”:5“,1 “:” 6" , “水平”: “6”, “0”: “the_boatswain”, “piratename”: “the_boatswain”},{ “秩”:6中, “1”: “5”, “水平”: “5”, “0”: “battlemonger”, “piratename”: “battlemonger”},{ “秩”:7中, “1”: “5”, “水平”: “5”, “0”:“MishuAnubis ”, “piratename”: “MishuAnubis”},{ “秩”:8, “1”: “5”, “水平”: “5”, “0”: “YogeshPatil”, “piratename”: “YogeshPatil”} ,{ “秩”:

2 个答案:

答案 0 :(得分:0)

尝试使用此方法可能会对您有所帮助。

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;

}

答案 1 :(得分:0)

你得错了钥匙

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

没有名为"success"

的键

这样做:

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