parent
dbf7a6b128
commit
6cafdd2af4
@ -0,0 +1,135 @@
|
||||
package com.example.chapter9_okhttp;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import okhttp3.Call;
|
||||
import okhttp3.Callback;
|
||||
import okhttp3.FormBody;
|
||||
import okhttp3.Headers;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
}
|
||||
|
||||
public void getSync(View v){
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
|
||||
new Thread(){
|
||||
@Override
|
||||
public void run() {
|
||||
//
|
||||
String url = "http://api.k8s.mt178.com/mock/orange/product/search";
|
||||
//String url = "https://www.httpbin.org/get?a=1&b=2";
|
||||
Request request = new Request.Builder()
|
||||
.url(url)
|
||||
.get()
|
||||
.build();
|
||||
// 准备好请求的Call对象
|
||||
Call call = client.newCall(request);
|
||||
|
||||
try {
|
||||
Response response = call.execute();
|
||||
Log.v("OkHttp", "postAsync: " + response.body().string());
|
||||
//tv.setText(response.body().toString());
|
||||
}catch (IOException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
public void getAsync(View v){
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
String url = "https://www.httpbin.org/get?a=1&b=2";
|
||||
Request request = new Request.Builder()
|
||||
.url(url)
|
||||
.get()
|
||||
.build();
|
||||
// 准备好请求的Call对象
|
||||
Call call = client.newCall(request);
|
||||
|
||||
/*try {
|
||||
Response response = call.execute();
|
||||
Log.v("OkHttp", "postAsync: " + response.body().string());
|
||||
//tv.setText(response.body().toString());
|
||||
}catch (IOException e){
|
||||
e.printStackTrace();
|
||||
}*/
|
||||
call.enqueue(new Callback() {
|
||||
@Override
|
||||
public void onFailure(@NotNull Call call, @NotNull IOException e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
|
||||
Log.v("OkHttp", "postAsync: " + response.body().string());
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void postAsync(View v){
|
||||
|
||||
new Thread(){
|
||||
@Override
|
||||
public void run() {
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
|
||||
String url = "https://www.httpbin.org/post";
|
||||
// String url = "http://10.0.2.2:8080/index/register";
|
||||
// 表单请求体
|
||||
/*FormBody body = new FormBody.Builder()
|
||||
.add("username", "zhangsan")
|
||||
.add("password", "123456")
|
||||
.build();*/
|
||||
|
||||
// 上传JSON对象
|
||||
MediaType JSON = MediaType.parse(" application/json; charset=utf-8");
|
||||
String jsonStr = "{\"username\":\"%赵E\", \"nickname\":\"赵 E\"}";//json数据.
|
||||
RequestBody body = RequestBody.create(JSON, jsonStr);
|
||||
|
||||
// 上传File文件
|
||||
/*MediaType fileType = MediaType.parse("File/*");//数据类型为File
|
||||
File file = new File(" path");//file对象.
|
||||
RequestBody body = RequestBody.create(fileType, file);*/
|
||||
Request request = new Request.Builder()
|
||||
.url(url)
|
||||
.post(body)
|
||||
.build();
|
||||
|
||||
// 准备好请求的Call对象
|
||||
Call call = client.newCall(request);
|
||||
|
||||
try {
|
||||
Response response = call.execute();
|
||||
Log.v("OkHttp", "postAsync: " + response.body().string());
|
||||
//tv.setText(response.body().toString());
|
||||
}catch (IOException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
}
|
Loading…
Reference in new issue