2015-02-11
原CSDN博客已弃用,文章会逐渐迁移过来。
转帖请注明本文出自weimeig的博客(http://blog.csdn.net/weimeig/article/details/78656734),请尊重他人的辛勤劳动成果,谢谢
一、添加依赖
build.gradle
dependencies {
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.squareup.okio:okio:1.7.0'
}
权限:
<uses-permission android:name="android.permission.INTERNET" />
HelloOkhttp
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* Created by Administrator on 2017/11/27.
*/
public class HelloOkhttp {
public static void main(String [] args) throws IOException {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().
url("http://www.baidu.com").build();
try {
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
System.out.println(response.body().string());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
QueryHttp
import java.io.IOException;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* Created by Administrator on 2017/11/27.
*/
public class QueryHttp {
public static void main(String args[]) {
OkHttpClient client = new OkHttpClient();
HttpUrl httpUrl = HttpUrl.parse("https://api.heweather.com/x3/weather").
newBuilder().
addQueryParameter("city", "beijing").
addQueryParameter("key", "d17ce22ec5404ed883e1cfcaca0ecaa7").
build();
String url = httpUrl.toString();
System.out.println(httpUrl.toString());
Request request = new Request.Builder().url(url).build();
try {
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
System.out.println(response.body().string());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
HeadHttp
import java.io.IOException;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* Created by Administrator on 2017/11/27.
*/
/**
* 请求头
*/
public class HeadHttp {
public static void HadeHttp(String url){
OkHttpClient client =new OkHttpClient();
Request request = new Request.Builder().url(url).
addHeader("User-Agent","from nate http").build();
try {
Response response = client.newCall(request).execute();
if(response.isSuccessful()){
Headers headers = response.headers();
for(int i=0;i<headers.size();i++){
System.out.println(headers.name(i)+":"+headers.value(i));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String []args){
HadeHttp("http://www.baidu.com");
}
}
同步和异步请求比较
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* Created by Administrator on 2017/11/27.
*/
/**同步
*
*/
public class AsyncHttp {
public static void SendHttp(String url){
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
try {
Response response = client.newCall(request).execute();
if (response.isSuccessful()){
System.out.println(response.body().string());
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*异步
*
*/
public static void SendAsyncHttp(String url){
System.out.println(Thread.currentThread().getId());
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if (response.isSuccessful()){
System.out.println(Thread.currentThread().getId());
}
}
});
}
public static void main(String [] args){
SendAsyncHttp("http://www.baidu.com");
// SendHttp("http://www.baidu.com");
}
}