我正在尝试向 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
最佳答案
我认为文档描述足够丰富,可以理解异常NetworkOnMainThreadException
The exception that is thrown when an application attempts to perform a networking operation on its main thread.
This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged. See the document
试试这个链接,让你知道如何处理网络操作 http://developer.android.com/training/articles/perf-anr.html
关于java - Android HttpGet 请求未发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15926181/