-3

ブラウザに表示されるように適切な順序でjsonオブジェクトの応答を取得する必要がありますが、ランダムです。これは、クライアントからの応答を取得するための私の Post 関数です。また、すべての配列フィールドを文字列ではなくイテレータで取得したい

jsonserch2.getString("@id");

塗りつぶしイテレーターにイテレーター myVeryOwnIterator=jsonserch2.keys() を使用します

String response = postData("http://url",generateRequestJson(this));


public static String postData(String url, String json) {
    final HttpClient httpclient = new DefaultHttpClient();
    final HttpPost httppost = new HttpPost(url);
    httppost.setHeader("Content-type", "application/x-www-form-urlencoded");

    final ArrayList<NameValuePair> values = new ArrayList<NameValuePair>();
    values.add(new BasicNameValuePair("data", json));
    String response = "";
    try {
        httppost.setEntity(new UrlEncodedFormEntity(values));
        final HttpResponse httpResponse = httpclient.execute(httppost);
        response = convertInputStreamToString(httpResponse);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return response;
}

private static String convertInputStreamToString(HttpResponse response) {
    BufferedReader bufferedReader = null;
    try {
        bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        // JsonReader reader = new JsonReader(new
        // InputStreamReader(response.getEntity().getContent()));
        // Log.e("",""+reader);
        final StringBuffer stringBuffer = new StringBuffer("");
        String line = "";
        final String LineSeparator = System.getProperty("line.separator");
        //

        //
        while ((line = bufferedReader.readLine()) != null) {
            stringBuffer.append(line + LineSeparator);
        }
        return stringBuffer.toString();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return "";
}
4

1 に答える 1

0

これを試してください

 public void getJsonData() {
      InputStream source = retrieveStream(url);     
      Gson gson = new Gson();      
      Reader reader = new InputStreamReader(source);      
      jsonPlots response = gson.fromJson(reader, jsonPlots.class);
      Toast.makeText(this, response.mFieldId, Toast.LENGTH_SHORT).show();  // this works
    ArrayList<Plants> results = response.PlantArray;

}  
于 2012-05-15T06:59:50.800 に答える