XUtils——GET请求

在使用Android studio的时候,需要在 build.gradle(Module:app)中添加   useLibrary'org.apache.http.legacy'

添加位置如:

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
useLibrary'org.apache.http.legacy'

}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile files('libs/xUtils-2.6.14.jar')
compile files('libs/universal-image-loader-1.9.3.jar')
compile files('libs/gson-2.2.4.jar')
}

//----------------------------------------------------------------------

//HttpUtils实例化对象
     HttpUtils http = new HttpUtils();
       /*
                *发送请求send(HttpMethod method, String url, RequestCallBack<T> callBack)
             * method请求方式
              * url请求地址
              *RequestCallBack <String>请求完后的回调监听String是请求完后你想让他返回什么类型的
             */
           http.send(HttpRequest.HttpMethod.GET, urlPath,new RequestCallBack<String>() {
                       @Override
                       public void onLoading(long total, long current, boolean isUploading) {
                         }
           @Override
           public void onSuccess(ResponseInfo<String> responseInfo) {
             String s = responseInfo.result;
             System.out.println(s);
                              
             }
                 @Override
                 public void onStart() {
                    
                  }
                 @Override
                 public void onFailure(HttpException error, String msg) {
                    
                  }
               });

//-----------------eclipse---中-----这么用-----------------------

//创建一个请求方法

private void HttpUtilRequest() {
           //创建HttpUtils
        HttpUtils httpUtils=new HttpUtils();
        //发送GET请求
        httpUtils.send(HttpMethod.GET, urlPath, new RequestCallBack<String>() {

            @Override//失败
            public void onFailure(HttpException arg0, String arg1) {
                // TODO Auto-generated method stub                
            }

            @Override//成功
            public void onSuccess(ResponseInfo<String> arg0) {
                String str=arg0.result;
                Log.i("-----++++++-----+++++", str);
                Message message=new Message();
                message.what=0;
                message.obj=str;
                handler.sendMessage(message);
            }
        });
        
        
        
    }

原文地址:https://www.cnblogs.com/changyiqiang/p/5717331.html