例一:execute的用法,代码如下所示:
public class MainActivity extends AppCompatActivity {
private Button button;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
textView = (TextView) findViewById(R.id.textView2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
OkHttpClient okHttpClient = new OkHttpClient();
/* 如果发起一条POST请求要比GET请求复杂一点,代码如下:
RequestBody requestBody=new FormBody.Builder()
.add("username","admin")
.add("password","12345")
.build();
Request request = new Request.Builder()
.url("http://www.baidu.com")
.post(requestBody)//在这里调用post
.build();*/
Request request = new Request.Builder()
.url("http://www.baidu.com")
.build();
try {
Response response = okHttpClient.newCall(request).execute();
final String responsedata = response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(responsedata);
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}
).start();
}
});
}
}
例二:enqueue的用法,代码如下所示:
public class MainActivity extends AppCompatActivity {
private Button button;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
textView = (TextView) findViewById(R.id.textView2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder()
.url("http://www.baidu.com")
.build();
okHttpClient.newCall(request).enqueue(new Callback() {//enqueue与execute不同之处就在于,enqueue内部已经创建好了子线程,就不需要自己再创建了
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, final Response response) throws IOException {//被外部类调用的变量就需要加上final,并且加上final之后,这个变量将不能再给其指定新的堆地址或者是值
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
textView.setText(response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
});
}
});
}
}
例三:post请求,以字符串的形式写入数据,代码如下所示:
OkHttpClient okHttpClient = new OkHttpClient();
MediaType mediaType=MediaType.parse("text/x-markdown; charset=utf-8");
Request request = new Request.Builder().url("https://api.github.com/markdown/raw").post(RequestBody.create(mediaType,"我是付晶晶")).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.d("MainActivity", "response.body():" + response.body().string());
//输出结果为:06-29 17:28:26.210 27763-27791/jiankang.liang.com.ceshi D/MainActivity: response.body():我是付晶晶
}
});
例四:post请求,以流的形式写入数据,代码如下所示:
OkHttpClient okHttpClient = new OkHttpClient();
RequestBody requestBody=new RequestBody() {
@Nullable
@Override
public MediaType contentType() {
return MediaType.parse("text/x-markdown; charset=utf-8");
}
@Override
public void writeTo(BufferedSink sink) throws IOException {
sink.write("提交流".getBytes());
}
};
Request request = new Request.Builder().url("https://api.github.com/markdown/raw").post(requestBody).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.d("MainActivity", "response.body():" + response.body().string());
//输出结果为:06-29 17:48:48.620 32505-32539/jiankang.liang.com.ceshi D/MainActivity: response.body():提交流
}
});
例五:post请求,上传文件到服务器,代码如下所示:
OkHttpClient okHttpClient = new OkHttpClient();
MediaType mediaType=MediaType.parse("text/x-markdown; charset=utf-8");
File file=new File("/storage/emulated/0/ceshi.txt");
RequestBody requestBody=RequestBody.create(mediaType,file);
Request request = new Request.Builder().url("https://api.github.com/markdown/raw").post(requestBody).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.d("MainActivity", "response.body():" + response.body().string());
//输出结果为:06-29 22:54:48.735 27302-27336/jiankang.liang.com.ceshi D/MainActivity: response.body():woshiceshiwenjian
}
});
例六:post请求,提交表单数据到服务器,代码如下所示:
OkHttpClient okHttpClient = new OkHttpClient();
RequestBody requestBody=new FormBody.Builder().add("search", "Jurassic Park").build();
Request request = new Request.Builder().url("https://en.wikipedia.org/w/index.php").post(requestBody).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.d("MainActivity", "response.body():" + response.body().string());
/*输出结果为:response.body():
Jurassic Park - Wikipedia