То, что я делал во многих проектах, является первым преобразовать строку чтения из потока HTTP в JSONObject используя код ниже:
JSONObject jsonOBJ=new JSONObject(jsonString);
Затем использовать jsonobject.getString("tag")
прочитать каждый тег. Для вашего кода, было бы что-то вроде:
String reference1=jsonobject.getString("Reference1");
Теперь значение referece1 = String content
А вот мой HTTP получить код:
String url="Your URL Goes Here";
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse response;
try {
try
{
InetAddress i = InetAddress.getByName(url);
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
String result= convertStreamToString(instream);
stringContent=new StringContent(result);
instream.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
И ниже является convertStreamToString
блок
private static String convertStreamToString(InputStream is)
{
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
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, а не JSONP. – Quentin
@Quentin, но отображается. Эта операция поддерживает ответы JSONP. Функция обратного вызова может быть задана с помощью параметра запроса обратного вызова Url. на странице справки. – wolverine
- Поскольку вы не пишете JS, который работает в браузере, вам не нужен JSONP, поэтому возможность службы необязательно предоставить его не имеет значения. – Quentin