私は開発のAndroid側に不慣れであり、私が試したかったことの1つは、HTTPGetの使用方法でした。テキストが送信され、結果が文字列で返されるようにメソッド全体を設定しましたが、知りたかったのは、その文字列を取得して、本当に必要な部分だけを抽出する方法でした。たとえば、文字列は
{"id":"124343","name":"somename" }
そして、その開始のid部分を取得したいだけの場合、Androidでそれをどのように行うのでしょうか。私はドキュメントを検索していますが、これまでのところ実際には何も見つかりませんでした。そして、私が見つけたもののほとんどは、JSONの使用も中心にしています。
以下は私が現在使用しているコードです(いくつかの異なる投稿から一緒にコンパイルしました)が、解析目的でJSONの使用に切り替える必要があるかもしれませんが、どこで変更を加えるかがわかりません
//This class is called after a button is pressed and the HTTPGet string is compiled from text within a textview and a static string (this already works)
private class LongRunningGetIO extends AsyncTask<Void, Void, String> {
protected String getASCIIContentFromEntity(HttpEntity entity)
throws IllegalStateException, IOException {
InputStream in = entity.getContent();
StringBuffer out = new StringBuffer();
int n = 1;
while (n > 0) {
byte[] b = new byte[4096];
n = in.read(b);
if (n > 0)
out.append(new String(b, 0, n));
}
return out.toString();
}
@Override
protected String doInBackground(Void... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
String question = questionText.getText().toString();
String newString = httpPath + "?userText=" + question;
Log.i("Message", newString);
HttpGet httpGet = new HttpGet(newString);
String text = null;
try {
HttpResponse response = httpClient.execute(httpGet,
localContext);
HttpEntity entity = response.getEntity();
text = getASCIIContentFromEntity(entity);
} catch (Exception e) {
return e.getLocalizedMessage();
}
return text;
}
protected void onPostExecute(String results) {
if (results != null) {
Log.i("String", results);
}
}
}