在 Java/Android 上打印 Json API 请求的结果
Printing out results of Json API Request on Java/Android
我正在使用 Konstantin Burov 在 Whosebug post () 上演示的以下指南:
首先,请求访问网络的权限,将以下内容添加到您的清单中:
<uses-permission android:name="android.permission.INTERNET" />
然后最简单的方法是使用与 Android 绑定的 Apache http 客户端:
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet(URL));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
String responseString = out.toString();
//..more logic
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
如果你想让它在单独的线程上 运行 我建议扩展 AsyncTask:
class RequestTask extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try {
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
//TODO Handle problems..
} catch (IOException e) {
//TODO Handle problems..
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//Do anything with response..
}
}
然后您可以通过以下方式提出请求:
new RequestTask().execute("http://whosebug.com");
我的问题是现在如何 post 字符串中的实际结果?我得到的只是执行 new RequestTask().execute(url).toString();
时的地址
您的回复作为参数传递给 onPostExecute(String)
。
您应该在此方法中处理响应。请注意 onPostExecute(String)
在 UI 线程上是 运行,因此您不能在此方法中执行冗长的操作。
当您调用 new RequestTask().execute(url).toString();
时,您只是在调用 AsyncTask
的 toString()
方法(execute()
的 check the return value:它只是 returns this
,即您在 execute()
上调用的 AsyncTask。
我正在使用 Konstantin Burov 在 Whosebug post () 上演示的以下指南:
首先,请求访问网络的权限,将以下内容添加到您的清单中:
<uses-permission android:name="android.permission.INTERNET" />
然后最简单的方法是使用与 Android 绑定的 Apache http 客户端:
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet(URL));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
String responseString = out.toString();
//..more logic
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
如果你想让它在单独的线程上 运行 我建议扩展 AsyncTask:
class RequestTask extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... uri) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try {
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
} else{
//Closes the connection.
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
//TODO Handle problems..
} catch (IOException e) {
//TODO Handle problems..
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//Do anything with response..
}
}
然后您可以通过以下方式提出请求:
new RequestTask().execute("http://whosebug.com");
我的问题是现在如何 post 字符串中的实际结果?我得到的只是执行 new RequestTask().execute(url).toString();
时的地址您的回复作为参数传递给 onPostExecute(String)
。
您应该在此方法中处理响应。请注意 onPostExecute(String)
在 UI 线程上是 运行,因此您不能在此方法中执行冗长的操作。
当您调用 new RequestTask().execute(url).toString();
时,您只是在调用 AsyncTask
的 toString()
方法(execute()
的 check the return value:它只是 returns this
,即您在 execute()
上调用的 AsyncTask。