Android 在Activity类中进行Http请求时出现异常

  1. public class HttpActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            final EditText editText  = new EditText(this);
            Button button = new Button(this);
            button.setText("登录");
            button.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    httpGet(editText); 
                }
    
            });
        }
    
        private void httpGet(final EditText editText) {
            try {
                HttpClient httpClient = new DefaultHttpClient();
                String username = "tom";
                String password = "123";
                String uri = "http://www.baidu.com?username=" + URLEncoder.encode(username,"utf-8") + "&password="+ URLEncoder.encode(password,"utf-8");
                HttpGet httpGet = new HttpGet(uri);
                HttpResponse response = httpClient.execute(httpGet);
                if (response.getStatusLine().getStatusCode() == 200) {
                    InputStream inputStream = response.getEntity().getContent();
                    String result = NetUtils.parseStream(inputStream);
                    editText.setText(result);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

     

具体的报错信息呢,不一定是你代码的问题,可能是接口本身的问题(比如:你请求的方式和参数)

这个地方报错