最近使用了第三方包OkHttp来进行网络请求,感觉挺好用,响应速度也很快。


jar包(点击下载

github源码(点击下载

官网介绍(点击下载


而我用Android Studio,可以直接在gradle里面加上依赖:

compile 'com.squareup.okhttp:okhttp:2.4.0'
compile 'com.squareup.okio:okio:1.5.0'
对了,jar包是有两个,添加的话要两个都要添加才行。


基本使用

1.同步get请求:

final String Url = "http://vip.stock.finance.sina.com.cn/quotes_service/view/vMS_tradehistory.php?symbol=sh601988&date=2016-07-26&page=41";
OkHttpClient mOkHttpClient = new OkHttpClient();
Request request = new Request.Builder()
                        .url(Url)
                        .build();
try {
	Response response = mOkHttpClient.newCall(request).execute();
	if (response.isSuccessful()) {
		String data = new String(response.body().bytes(),"GBK");
                //请求成功以后的操作
	} else {
	}	
} catch (IOException e) {
	e.printStackTrace();
}


Response对象里面就是请求返回的数据了。


2.同步post请求:

final S<pre name="code" class="java">final String Url1 = "http://vip.stock.finance.sina.com.cn/quotes_service/view/vMS_tradehistory.php";
RequestBody body = new FormEncodingBuilder()
						.add("symbol","sh601988")
						.add("date","2016-07-26")
						.add("page","41")
						.build();

OkHttpClient mOkHttpClient = new OkHttpClient();
Request request = new Request.Builder()
		.url(Url1)
		.post(body)
		.build();
Call call = mOkHttpClient.newCall(request); 
call.enqueue(new Callback()
        {
            @Override
            public void onFailure(Request request, IOException e)
            {
				//失败的回调
            }

            @Override
            public void onResponse(final Response response) throws IOException
            {
                    String data = new String(response.body().bytes(),"GBK");
					//成功的回调
            }
        }); 

 当然了,这是参数为键值对的post请求,new一个RequestBody的对象,把参数封装起来。 

3.异步get请求:

String url = "http://vip.stock.finance.sina.com.cn/quotes_service/view/vMS_tradehistory.php?symbol=sh601988&date=2016-07-26&page=41";
//创建okHttpClient对象
OkHttpClient mOkHttpClient = new OkHttpClient();
//创建一个Request
final Request request = new Request.Builder()
                .url(url)
                .build();
//new call
Call call = mOkHttpClient.newCall(request); 
//请求加入调度
call.enqueue(new Callback()
        {
            @Override
            public void onFailure(Request request, IOException e)
            {
				//失败的回调
            }

            @Override
            public void onResponse(final Response response) throws IOException
            {
                    String data = new String(response.body().bytes(),"GBK");
					//成功的回调
            }
        }); 
这里要加入请求队列,还要实现回调方法。


4.异步post请求

final String Url1 = "http://vip.stock.finance.sina.com.cn/quotes_service/view/vMS_tradehistory.php";
RequestBody body = new FormEncodingBuilder()
						.add("symbol","sh601988")
						.add("date","2016-07-26")
						.add("page","41")
						.build();

OkHttpClient mOkHttpClient = new OkHttpClient();
Request request = new Request.Builder()
		.url(Url1)
		.post(body)
		.build();
Call call = mOkHttpClient.newCall(request); 
call.enqueue(new Callback()
        {
            @Override
            public void onFailure(Request request, IOException e)
            {
				//失败的回调
            }

            @Override
            public void onResponse(final Response response) throws IOException
            {
                    String data = new String(response.body().bytes(),"GBK");
					//成功的回调
            }
        }); 
post方法类似这么做。


最后,这里只是说一下简单的用法,详情以官网为准。




Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐