首页 新闻 会员 周边

android httpclient登陆错误

0
悬赏园豆:60 [待解决问题]

刚开始学android,做客户端实现登陆功能的时候,使用httpclinet连接返回了Request method 'POST' not supported的错误,

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText1=(EditText) findViewById(R.id.editText1);
        editText2=(EditText) findViewById(R.id.editText2);
        textView=(TextView) findViewById(R.id.textview);
        button=(Button) findViewById(R.id.button1);
        httpClient = new DefaultHttpClient();
        button.setOnClickListener(new OnClickListener() {
            
            public void onClick(View v) {
                System.out.println("username="+editText1.getText()+"&password="+editText2.getText());
                readNet("http://192.168.202.246/iot/login","username="+editText1.getText()+"&password="+editText2.getText());
                
            }
        });
    }

    public void readNet(String URL,String inString){
        
        new AsyncTask<String, Void, String>(){

            @Override
            protected String doInBackground(String... params) {
                String urlString=params[0];
                HttpPost post=new HttpPost(urlString);
                StringEntity entitytrString;
                try {
                    entitytrString = new StringEntity(params[1]);
                    post.setEntity(entitytrString);
                } catch (UnsupportedEncodingException e1) {
                    e1.printStackTrace();
                }
                try {
                    HttpResponse httpResponse=httpClient.execute(post);
                    String valString=EntityUtils.toString(httpResponse.getEntity());
                    System.out.println(valString);
                    
                    return valString;
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(String result) {
                // TODO Auto-generated method stub
                textView.setText(result);
            }
            
            
        }.execute(URL,inString);
    }
}
219饮水机的主页 219饮水机 | 初学一级 | 园豆:142
提问于:2015-09-18 17:06
< >
分享
所有回答(2)
0

配置了android访问网络的权限么?

JackWang-CUMT | 园豆:2866 (老鸟四级) | 2015-09-21 14:15

有的

支持(0) 反对(0) 219饮水机 | 园豆:142 (初学一级) | 2015-09-21 14:29

@219饮水机: 

感觉你的URL用的GET方式,但是发送请求是用的HTTPPOST,是不是二者不匹配导致的?

 1  Log.i(TAG, "POST request");
 2             // 先获取用户名和年龄
 3             String name = mNameText.getText().toString();
 4             String age = mAgeText.getText().toString();
 5 
 6             NameValuePair pair1 = new BasicNameValuePair("username", name);
 7             NameValuePair pair2 = new BasicNameValuePair("age", age);
 8 
 9             List<NameValuePair> pairList = new ArrayList<NameValuePair>();
10             pairList.add(pair1);
11             pairList.add(pair2);
12 
13             try
14             {
15                 HttpEntity requestHttpEntity = new UrlEncodedFormEntity(
16                         pairList);
17                 // URL使用基本URL即可,其中不需要加参数
18                 HttpPost httpPost = new HttpPost(baseURL);
19                 // 将请求体内容加入请求中
20                 httpPost.setEntity(requestHttpEntity);
21                 // 需要客户端对象来发送请求
22                 HttpClient httpClient = new DefaultHttpClient();
23                 // 发送请求
24                 HttpResponse response = httpClient.execute(httpPost);
25                 // 显示响应
26                 showResponseResult(response);
27             }
28             catch (Exception e)
29             {
30                 e.printStackTrace();
31             }
支持(0) 反对(0) JackWang-CUMT | 园豆:2866 (老鸟四级) | 2015-09-21 14:42
0

建议不要再使用HttpClient,使用HttpURLConnection代替HttpClient,原因是Android 6.0不再支持HttpClient

总李写代码 | 园豆:221 (菜鸟二级) | 2016-06-23 17:15
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册