Android 如何按顺序获取json响应
我需要按照浏览器中显示的顺序获得json对象响应,但这是随机的。 这是我的Post函数,用于从客户端获取响应。 我还希望通过迭代器而不是像字符串一样获取所有数组字段 jsonserch2。getString@id; 我使用迭代器myVeryOwnIterator=jsonserch2.keys作为填充迭代器Android 如何按顺序获取json响应,android,json,Android,Json,我需要按照浏览器中显示的顺序获得json对象响应,但这是随机的。 这是我的Post函数,用于从客户端获取响应。 我还希望通过迭代器而不是像字符串一样获取所有数组字段 jsonserch2。getString@id; 我使用迭代器myVeryOwnIterator=jsonserch2.keys作为填充迭代器 String response = postData("http://url",generateRequestJson(this)); public static String post
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 "";
}
试试这个
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;
}
请将您的完整代码粘贴到此处。在浏览器中,它显示序列,但当您从代码中获取序列时,它不在序列中,但当您解析json,然后根据您的序列进行解析时,您将获得完美的数据。使用json不可能吗?