私が多くのプロジェクトで行っていることは、最初にhttpストリームから読み取った文字列を以下のコードを使用してJSONObjectに変換することです。
JSONObject jsonOBJ=new JSONObject(jsonString);
次に、を使用jsonobject.getString("tag")
して各タグを読み取ります。コードの場合、次のようになります。
String reference1=jsonobject.getString("Reference1");
ここで、referece1=の値String content
そして、これが私のhttpgetコードです。
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();
}