http请求开源框架: AsyncHttpClient


同步/异步:
同步: 在同一个线程里面执行的.
异步:开启新的线程


1.GET获取服务器信息:
public   class   MainActivity   extends   Activity {
      
       private   TextView    tv_code ;

       @Override
       protected   void   onCreate(Bundle savedInstanceState) {
               super .onCreate(savedInstanceState);
            setContentView(R.layout.   activity_main );
            
               tv_code =(TextView) findViewById(R.id.   tv_code );
      }
      
      
       /**
       * Button触发事件
       */
       public   void   click(View view){
            
               //创建AsyncHttpClient对象
            AsyncHttpClient  client=   new   AsyncHttpClient();
            
            client.get(   "http://www.baidu.com" , new   AsyncHttpResponseHandler(){

                     @Override
                     public   void   onSuccess(String content) {
                           super .onSuccess(content);
                           tv_code .setText(content);
                  }

                     @Override
                     public   void   onFailure(Throwable error, String content) {
                      super .onFailure(error, content);
                    Toast. makeText(getApplicationContext(),   "获取失败....." , 0).show() ;
                    error.printStackTrace();
                  }
                  
                });
            
      }
      
}


-------------------------------------------------------------------------------------------------


2.Get/Post请求提交数据到服务器

public   class   LoginService {
      
       /**
       * GET方法获取服务器信息
       *   @param   context
       *   @param   userName
       *   @param   passWord
       */
       public   static   void   loginByGet(   final   Context context,String userName,String passWord){
            
            String path=   "http://192.168.1.8/Web/LoginServlet?userName="  +URLEncoder. encode(userName)+   "&passWord="  +URLEncoder.   encode (passWord) ;
            
            
               //创建AsyncHttpClient对象
            AsyncHttpClient  client=   new   AsyncHttpClient();
            client.get(path,   new   AsyncHttpResponseHandler(){
                     @Override
                     public   void   onSuccess(String content) {
                           super .onSuccess(content);
                        Toast. makeText(context,   "请求成功" , 0).show();
                  }
                  
                     @Override
                     public   void   onFailure(Throwable error, String content) {
                       super .onFailure(error, content);
                     Toast. makeText(context,   "请求失败" , 0).show();
                      error.printStackTrace();    //打印失败信息
                  }
                  
                  
            });
            
      }

       /**
       * POST方法提交数据到服务器
       *   @param   context
       *   @param   userName
       *   @param   passWord
       */
       public   static   void    (   final   Context context,String userName,String passWord){
            
            String path=   "http://192.168.1.8/Web/LoginServlet" ;
            
               //创建AsyncHttpClient对象
            AsyncHttpClient  client=   new   AsyncHttpClient();
            
            RequestParams  params=   new   RequestParams();    //创建信息提交者
            params.put(   "userName" , userName);
            params.put(   "passWord" , passWord);
            
            client.post(path,  params, new   AsyncHttpResponseHandler(){
                  
                     @Override
                     public   void   onSuccess(String content) {
                           super .onSuccess(content);
                        Toast. makeText(context,   "提交成功..." , 0).show();
                  }
                  
                     @Override
                     public   void   onFailure(Throwable error, String content) {
                        super .onFailure(error, content);
                      Toast. makeText(context,   "提交失败" , 0).show();
                      error.printStackTr ace();     //打印错误信息
                  }
                  
                  
            });
            
            
      }
      

}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值