我正在尝试向http://www.google.com/发送 HTTP 请求,并希望将所有响应保存在我的 android 应用程序中。我尝试了很多方法,但每次出现错误或 logcat 错误或我的模拟器停止时。
你能指出我的代码有什么问题并给我一些配置提示吗?我希望能够同时使用GET
和POST
。
public void executeHttpGet() throws Exception {
BufferedReader in = null;
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI("http://www.google.com/"));
HttpResponse response = client.execute(request);
in = new BufferedReader
(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
result.setText(sb.toString());
in.close();
String page = sb.toString();
System.out.println(page);
Debug.out(sb.toString());
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
日志猫:
04-09 20:56:07.730: E/AndroidRuntime(9128): FATAL EXCEPTION: main
04-09 20:56:07.730: E/AndroidRuntime(9128): java.lang.RuntimeException: Unable to start activity ComponentInfo{mainpackage.rest_client/mainpackage.rest_client.GetResponse}: android.os.NetworkOnMainThreadException
04-09 20:56:07.730: E/AndroidRuntime(9128): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-09 20:56:07.730: E/AndroidRuntime(9128): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-09 20:56:07.730: E/AndroidRuntime(9128): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-09 20:56:07.730: E/AndroidRuntime(9128): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-09 20:56:07.730: E/AndroidRuntime(9128): at android.os.Handler.dispatchMessage(Handler.java:99)
04-09 20:56:07.730: E/AndroidRuntime(9128): at android.os.Looper.loop(Looper.java:137)
04-09 20:56:07.730: E/AndroidRuntime(9128): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-09 20:56:07.730: E/AndroidRuntime(9128): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 20:56:07.730: E/AndroidRuntime(9128): at java.lang.reflect.Method.invoke(Method.java:511)
04-09 20:56:07.730: E/AndroidRuntime(9128): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-09 20:56:07.730: E/AndroidRuntime(9128): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-09 20:56:07.730: E/AndroidRuntime(9128): at dalvik.system.NativeStart.main(Native Method)
04-09 20:56:07.730: E/AndroidRuntime(9128): Caused by: android.os.NetworkOnMainThreadException
04-09 20:56:07.730: E/AndroidRuntime(9128): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
04-09 20:56:07.730: E/AndroidRuntime(9128): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
04-09 20:56:07.730: E/AndroidRuntime(9128): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
04-09 20:56:07.730: E/AndroidRuntime(9128): at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-09 20:56:07.730: E/AndroidRuntime(9128): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
04-09 20:56:07.730: E/AndroidRuntime(9128): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-09 20:56:07.730: E/AndroidRuntime(9128): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-09 20:56:07.730: E/AndroidRuntime(9128): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-09 20:56:07.730: E/AndroidRuntime(9128): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-09 20:56:07.730: E/AndroidRuntime(9128): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-09 20:56:07.730: E/AndroidRuntime(9128): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-09 20:56:07.730: E/AndroidRuntime(9128): at mainpackage.rest_client.GetResponse.QueryGooglePlus(GetResponse.java:75)
04-09 20:56:07.730: E/AndroidRuntime(9128): at mainpackage.rest_client.GetResponse.onCreate(GetResponse.java:61)
04-09 20:56:07.730: E/AndroidRuntime(9128): at android.app.Activity.performCreate(Activity.java:5104)
04-09 20:56:07.730: E/AndroidRuntime(9128): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-09 20:56:07.730: E/AndroidRuntime(9128): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-09 20:56:07.730: E/AndroidRuntime(9128): ... 11 more
04-09 20:56:08.330: D/dalvikvm(9128): GC_CONCURRENT freed 204K, 11% free 2889K/3216K, paused 6ms+44ms, total 417ms
04-09 20:56:11.640: I/Process(9128): Sending signal. PID: 9128 SIG: 9