0

この形式で応答があります(応答全体)

これまでに次のコードを試しましたが、エラーが発生します

private String connect(String url) {

    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(url);
    HttpResponse response;
    String returnString = null;
    try {
        response = httpclient.execute(httpget);
        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entity = response.getEntity();
            if (entity != null) {

                InputStream instream = entity.getContent();

                String res = convertStreamToString(instream);

                JSONObject jsonObj = new JSONObject(res);

                String f = jsonObj.getString("Result");

                f = f.trim();

                System.out.println("!!!!!!!!!!!!!!! "+f);

                String s= jsonObj.getString("About"); 
                System.out.println("@@@@@@ "+s); 

                JSONArray get = jsonObj.getJSONArray("Result");



                // lets loop through the JSONArray and get all the items 
                for (int i = 0; i < get.length(); i++) { 
                    // printing the values to the logcat 
                    System.out.println("&&&&&&&&&&"+get.getJSONObject(i).getString("About").toString()); 
                    System.out.println("&&&&&&&&&&"+get.getJSONObject(i).getString("AboutMeText").toString());  
                } 



                instream.close();
            }
        } else {
            returnString = "Unable to load page - "
                    + response.getStatusLine();
        }
    } catch (IOException ex) {
        returnString = "Connection failed; " + ex.getMessage();
    } catch (JSONException ex) {
        returnString = "JSON failed; " + ex.getMessage();
    }
    return returnString;
}

private static String convertStreamToString(InputStream is) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

解析しようとするたびに、json失敗の例外が発生し、値がないと表示されます。ここで間違いがあった場合はお知らせください。

4

2 に答える 2

1

JSON が有効ではありません。これが必要ですか?

{
"Result": [
    {
        "About": "",
        "AboutMeText": {}
    }
]
}
于 2012-10-02T12:59:32.060 に答える
0

より簡単な方法は、GSONを使用することです。(すべてメモリからのコードなので...)

クラスを{Result: ["{"About":"","AboutMeText":[{}]]} 作成するからです

public class Result {
 private String About;
 private String AboutMeText;
  //getters and setters
}

次に、逆シリアル化するために

Gson gson = new Gson();
Result result = gson.fromJson(jsonString, Result.class);
于 2012-10-02T13:04:29.187 に答える