HTT presponse = httpClient.execute(HTTPGET); [英] httpResponse = httpClient.execute(httpGet);

查看:339
本文介绍了HTT presponse = httpClient.execute(HTTPGET);的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Andr​​oid应用程序,不断崩溃。 我试图通过电话后连接到Web服务。 但是,我的应用程序不断崩溃每次试图调用web服务的时间。

 进口android.app.Activity;

    进口org.apache.http.HttpEntity;
    进口org.apache.http.Htt presponse;
    进口org.apache.http.client.methods.HttpGet;
    进口org.apache.http.impl.client.DefaultHttpClient;

    进口java.io.IOException异常;
    公共类PostActivity延伸活动{

        //静态字符串响应= NULL;

        公共无效loadPost()抛出IOException异常{
            DefaultHttpClient的HttpClient =新DefaultHttpClient();
            HttpEntity httpEntity = NULL;
            HTT presponse HTT presponse = NULL;

            字符串blogFeedUrl =HTTP://本地主机/ medapp /公/移动/后;

            HTTPGET HTTPGET =新HTTPGET(blogFeedUrl);
            HTT presponse = httpClient.execute(HTTPGET); //<  - 这是应用程序崩溃

        }

    }
 

解决方案

从code已公布,并在同有关的进口,依赖于操作系统(ESP蜂窝及以后),您的应用程序会崩溃,由于在 NetworkOnMainThreadException 。您正在尝试在主线程中的网络操作,而不是在后台线程或 Asyctask

在你的logcat(如果你张贴的it'l帮忙), NetworkOnMainThreadException 将被抛出:

  E / AndroidRuntime(673):java.lang.RuntimeException的:无法启动的活动
    ComponentInfo {com.example / com.example.ExampleActivity}:android.os.NetworkOnMainThreadException
 

的解释,为什么出现这种情况是有据可查的 Android开发者的网站< /一>:

  

A NetworkOnMainThreadException 被抛出时,应用程序   尝试在其主线程进行网络操作。这是   只抛出针对蜂窝SDK或更高版本的应用程序。   针对早期SDK版本的应用程序可以做   网络对他们的主事件循环线程,但它的高度   气馁。

穿过:

为什么应用程序会崩溃或工作依赖于操作系统

尽量的AsyncTask避免NetworkOnMainThread

你为什么不使用严格模式选择,因为你的解决方案,只有调试(我建议避免这种情况实际上也,你知道什么是现在的问题):
<一href="http://stackoverflow.com/questions/18297485/android-os-networkonmainthreadexception-sending-an-email-from-android/18297516#18297516">Critical要解决这个问题,而不是通过设置主题策略

My android app keeps on crashing. I'm trying to connect to a webservice via post call. But my app keeps on crashing every time it tries to call the webservice.

 import android.app.Activity;

    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;

    import java.io.IOException;
    public class PostActivity extends Activity {

        //static String response = null;

        public void loadPost() throws IOException {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpEntity httpEntity = null;
            HttpResponse httpResponse = null;

            String blogFeedUrl = "http://localhost/medapp/public/mobile/post";

            HttpGet httpGet = new HttpGet(blogFeedUrl);
            httpResponse = httpClient.execute(httpGet);  // <-- this is where the application crashes

        }

    }

解决方案

From the code you have posted and related imports in the same, depending on the O.S(Esp Honeycomb and onwards), your application would crash due to the NetworkOnMainThreadException. You are attempting the network operation on the main thread, not in a background thread or Asyctask.

In your logcat(if you post that it'l help), NetworkOnMainThreadException will be thrown:

E/AndroidRuntime(673): java.lang.RuntimeException: Unable to start activity
    ComponentInfo{com.example/com.example.ExampleActivity}: android.os.NetworkOnMainThreadException

The explanation as to why this occurs is well documented on the Android developer's site:

A NetworkOnMainThreadException 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.

Go through:

Why the app would crash or work depending on O.S.

Try AsyncTask to avoid NetworkOnMainThread

Why should you not use the Strict Mode alternative as your solution and only to debug(i'd suggest avoid that also actually, you know what is the problem now):
Critical to fix it, not by setting Thread policies

这篇关于HTT presponse = httpClient.execute(HTTPGET);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆