开源项目(asyncHttpClient) get post 方式提交

Posted iFat的笔记本

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了开源项目(asyncHttpClient) get post 方式提交相关的知识,希望对你有一定的参考价值。

 get方式:
   public static void requestNetForGetLogin(final Context context,final Handler handler ,final String username, final String password) {
                //使用HttpClient请求服务器将用户密码发送服务器验证
                try{
                String path = "http://192.168.13.83:8080/xuexi/servlet/LoginServlet?username="+URLEncoder.encode(username,"utf-8")+"&pwd="+URLEncoder.encode(password,"utf-8");
        
                //创建一个AsyncHttpClient对象
                AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
                asyncHttpClient.get(path, new AsyncHttpResponseHandler() {
                    
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                        //statusCode:状态码    headers:头信息  responseBody:返回的内容,返回的实体
                        //判断状态码
                        if(statusCode == 200){
                            //获取结果
                            try {
                                String result = new String(responseBody,"utf-8");
                                Toast.makeText(context, result, 0).show();
                            } catch (UnsupportedEncodingException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }
                    
                    @Override
                    public void onFailure(int statusCode, Header[] headers,
                            byte[] responseBody, Throwable error) {
                        
                        System.out.println("...............onFailure");
                        
                    }
                });
                }catch (Exception e) {
                    e.printStackTrace();
                }
    }


        post方式:


            String path = "http://192.168.13.83:8080/xuexi/servlet/LoginServlet";
    
            AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
            RequestParams params = new RequestParams();
            params.put("username", username);
            params.put("pwd", password);
            
            //url:   parmas:请求时携带的参数信息   responseHandler:是一个匿名内部类接受成功过失败
            asyncHttpClient.post(path, params, new AsyncHttpResponseHandler() {
                
                @Override
                public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                    //statusCode:状态码    headers:头信息  responseBody:返回的内容,返回的实体
                    
                    //判断状态码
                    if(statusCode == 200){
                        //获取结果
                        try {
                            String result = new String(responseBody,"utf-8");
                            Toast.makeText(context, result, 0).show();
                        } catch (UnsupportedEncodingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                    
                }
                
                @Override
                public void onFailure(int statusCode, Header[] headers,
                        byte[] responseBody, Throwable error) {
                    
                }
     

以上是关于开源项目(asyncHttpClient) get post 方式提交的主要内容,如果未能解决你的问题,请参考以下文章

android 使用AsyncHttpClient框架上传文件以及使用HttpURLConnection下载文件

HttpClient和AsynchttpClient的get与post请求方式

android android-async-http 上传图片,android----AsyncHttpClient的get,post和图片上传

Android之网络编程

AsyncHttpClient 实战总结及思考

Android网络请求框架AsyncHttpClient实例详解(配合JSON解析调用接口)