码迷,mamicode.com
首页 > Web开发 > 详细

Httpclient

时间:2019-01-05 22:52:57      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:text   syn   ace   array   放松   password   name   pre   one   

  • httpclient实现get和post提交
  • 代码实现过程 
    • 到包 compile ‘com.loopj.android:android-async-http:1.4.9’ 把这个包导入到build.gradle里 
    • httlclient实现get提交数据
    private void httpclientGET() {
        try {
            String username = mName.getText().toString().trim();
            String password = mPwd.getText().toString().trim();
            String path = "http://192.168.0.156:8080/loginServlet/loginServlet?username=" + username + "&&password=" + password;
            //使用httpclient提交数据
            HttpClient client = new DefaultHttpClient();
            //实现向服务器放松请求
            HttpGet httpGet = new HttpGet(path);
            HttpResponse httpResponse = client.execute(httpGet);
            InputStream is = httpResponse.getEntity().getContent();
            String content = StreamUtils.StreamToString(is);
            showToast(content);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

    • 使用httpclient实现post提交
    private void httpclientPost() {
        try {
            String username = mName.getText().toString().trim();
            String password = mPwd.getText().toString().trim();
            String path = "http://192.168.0.156:8080/loginServlet/loginServlet";
            //定义请求体
            String data = "username=" + username + "&&password=" + password;
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(path);
            //定义请求体
            List<NameValuePair> list = new ArrayList<>();
            BasicNameValuePair nameValuePair = new BasicNameValuePair("username", username);
            list.add(nameValuePair);
            BasicNameValuePair passwordValuePair = new BasicNameValuePair("password", password);
            list.add(passwordValuePair);
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list);
            post.setEntity(entity);
            HttpResponse response = client.execute(post);
            InputStream is = response.getEntity().getContent();
            String content = StreamUtils.StreamToString(is);
            showToast(content);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

Httpclient

标签:text   syn   ace   array   放松   password   name   pre   one   

原文地址:https://www.cnblogs.com/nangongyibin/p/10226388.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!