0

私は Android プラットフォームを初めて使用するわけではありません。以前に多くの json ページを解析したことがありますが、1 つのリンクが問題を引き起こしています。次の関数を使用してjsonオブジェクトを返しています。プログラムは reader.readline() で停止し、ロードは停止しません。

public JSONObject getJSONFromUrl(String url) {
    DefaultHttpClient httpClient = new DefaultHttpClient();

    try {

        HttpResponse httpResponse = httpClient.execute(new HttpGet(url));
        if (httpResponse.getStatusLine().getStatusCode()==200)
        {
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        }

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        System.out.println("Unsupported Encoding");
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        System.out.println("Client Protocol");
    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("IO");
    }

    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);

        }
        is.close();
        jsonStr = sb.toString();
    } catch (Exception e) {
        System.out.println("Error here");
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {

        jObj = new JSONObject(jsonStr);

    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    return jObj;

}

誰でも何かを提案できますか?

4

1 に答える 1

1

これは、サーバーがあなたのケースで応答を終了していないことが原因であると確信しています。最初に「curl」をクライアントとして使用してそれを分離してみてください。

また、次のように EntitUtils を使用すると、ボディを String にフェッチする簡単な方法があります。

EntityUtils.toString(entity);

2 番目のパラメーターとしてエンコーディングを指定することもできます。 http://developer.android.com/reference/org/apache/http/util/EntityUtils.html

于 2012-11-18T04:29:35.113 に答える