Android应用程序停止工作-servlets和Httpget

Android应用程序停止工作-servlets和Httpget,android,tomcat,servlets,Android,Tomcat,Servlets,我有个问题。我从servlets和Android开始我的冒险,我设计了一个简单的servlet,它写“Hello world”。现在我想在我的Android应用程序中接收此消息。我的代码: public class MyServlet extends Activity implements OnClickListener{ Button button; TextView outputText; public static final String URL = "http://10.0.2.2

我有个问题。我从servlets和Android开始我的冒险,我设计了一个简单的servlet,它写“Hello world”。现在我想在我的Android应用程序中接收此消息。我的代码:

public class MyServlet extends Activity implements OnClickListener{

Button button;
TextView outputText;

public static final String URL = "http://10.0.2.2:8080/ServletExample/HelloServletExample";


@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    findViewsById();

    button.setOnClickListener(this);

}

private void findViewsById() {
    button = (Button) findViewById(R.id.button);
    outputText = (TextView) findViewById(R.id.outputTxt);
}

public void onClick(View view) {

    String output = null;



        HttpClient httpclient = new DefaultHttpClient();
        try {
            HttpResponse response = httpclient.execute(new HttpGet(URL));
            HttpEntity httpEntity = response.getEntity();
            output = EntityUtils.toString(httpEntity);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    //} catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    //} catch (IOException e) {
        // TODO Auto-generated catch block
    //}



    outputText.setText(output);


}
我的servlet:

@WebServlet("/HelloServletExample")
public class HelloServletExample extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public HelloServletExample() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    PrintWriter out = response.getWriter();
    out.println("Hello World");

}

}
我不知道为什么,但我的应用程序崩溃了。我试着注释了一些行,结果发现问题出在这行:

HttpEntity httpEntity = response.getEntity();

为什么?

如果使用API级别>=9则无需使用
AsyncTask
从Ui线程发出网络请求,只需在创建方法上添加活动,如下所示:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().
                                                   permitAll().build();

StrictMode.setThreadPolicy(policy); 

如果您注释HttpEntity HttpEntity=response.getEntity()行然后它工作了?如果是,则在应用程序crashI出现此异常后,在logcat中搜索
NetworkMainThreadException
。为什么?我的应用程序是针对Android 4的,我在互联网上读到,我这样做的方式是检索数据的正确方法。哦,我必须使用AsyncTask?如果你使用的是Android 4,则无需使用AsyncTask。好吧,使用10.0.2.2,应用程序将挂起。。。当我将其更改为localhost时,它不会收到任何内容。问题在哪里?但谢谢你,它不再崩溃了@user1928115:如果这个答案有助于你解决当前问题,那么将它标记为答案。谢谢