Android:HTTP在DefaultHttpClient上发布崩溃执行

时间:2014-03-20 07:36:44

标签: android

我在Android中有以下代码,用于向servlet发送请求。我总是在HttpResponse response = httpClient.execute(httpPost);崩溃,网址和一切都很完美,我不知道这里有什么问题?

有人可以指导我吗?

    Button loginBtn = (Button) findViewById(R.id.button1);
        loginBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //Log.d("Cobrowse","Login Button Clicked");

                usrText = (EditText) findViewById(R.id.editText1);
                pswdText = (EditText) findViewById(R.id.editText2);

                if ( usrText.length()>0 && pswdText.length()>0 ){
                    Log.d("Cobrowse: User name: ", usrText.getText().toString());
                    Log.d("Cobrowse: User name: ", pswdText.getText().toString());

                    System.out.println("Make Http Request");

                    // Creating HTTP client
                    // HttpClient                    
                    HttpClient httpClient = new DefaultHttpClient();                     
                    // Creating HTTP Post
                    HttpPost httpPost = new HttpPost("http://192.168.1.21:8080/CobrowingAuthSevlet/AuthenticationServlet?login=1234&password=1234");
                    // Making HTTP Request
                    try {
                         ****//IT IS CRASHING HERE****                      
                         HttpResponse response = httpClient.execute(httpPost);

                        // writing response to log
                        Log.d("Http Response: ", response.toString());
                        System.out.println("Http Response: " + response.toString());

                    } catch (ClientProtocolException e) {
                        // writing exception to log
                        e.printStackTrace();

                    } catch (IOException e) {
                        // writing exception to log
                        e.printStackTrace();
                    }
                    catch (Throwable t) { 
                        t.printStackTrace(); 
                    }

                }

        });

更新:

我尝试过AsyncTask,仍面临同样的问题:到达此行时没有任何表现 - > TheTask().execute

public void onClick(View v) {
                // TODO Auto-generated method stub
                //Log.d("Cobrowse","Login Button Clicked");

                usrText = (EditText) findViewById(R.id.editText1);
                pswdText = (EditText) findViewById(R.id.editText2);

                if ( usrText.length()>0 && pswdText.length()>0 ){
                    Log.d("Cobrowse: User name: ", usrText.getText().toString());
                    Log.d("Cobrowse: User name: ", pswdText.getText().toString());

                    System.out.println("Make Http Request");

                    new TheTask().execute("http://192.168.1.21:8080/CobrowingAuthSevlet/AuthenticationServlet?login=1234&password=1234");

                }
}

和,

class TheTask extends AsyncTask<String,String,String>
{
    protected void onPostExecute(String result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);
        // update textview here
        System.out.println("onPostExecute");
        System.out.println("onPostExecute: result" + result);
    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
    }

@Override
    protected String doInBackground(String... params) {

    System.out.println("doInBackground");

    try
    {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost method = new HttpPost(params[0]);
        HttpResponse response = httpclient.execute(method);
        HttpEntity entity = response.getEntity();
        if(entity != null){
            return EntityUtils.toString(entity);
        }
        else{
            return "No string.";
        }
     }
     catch(Exception e){
         return "Network problem";
     }
}
}

1 个答案:

答案 0 :(得分:2)

“当应用程序尝试在其主线程上执行网络操作时抛出此异常”。你应该调用asynctask。看看这个解决方案:How to fix android.os.NetworkOnMainThreadException?