我是 android 开发方面的新手,我知道我想尝试的一件事是如何使用 HTTP Get。我已经设置了整个方法,以便发送文本并且我的结果以字符串形式返回,但我想知道的是如何获取该字符串并仅提取我真正需要的部分。例如字符串回来了
{"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);
}
}
}