OkHttp 3.0之後版本GET、POST請求以及POST上傳檔案的示例程式碼
阿新 • • 發佈:2019-02-07
首先說一下2.x到3.x關於POST請求的請求體類的改變:
在2.x中是 類名是:FormEncodingBuilder()
在3.x中是 類名是: FormBody,但是一般使用是 FormBody.builder()
目前網上很多示例程式碼都是基於2.x縮寫的,我看了很多也沒發現3.x的示例程式碼。無意間搜尋okhttp3包內類時,發現了FormBody這個類,才知道原來是類名換掉了。於是我又上okhttp官網查詢更新說明,但是並沒找到相關說明。
就此,我就把我寫的簡單示例程式碼分享一下吧:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private OkHttpClient client;
public static final String GET_URL = "http://bz.budejie.com/?typeid=2&ver=3.4.3&no_cry=1&client=android&c=wallPaper&a=wallPaperNew&index=1&size=60&bigid=0";
public static final String TYPE = "application/octet-stream";
public static final String POST_URL = "http://zhushou.72g.com/app/gift/gift_list/";
// 請求條件:platform=2&gifttype=2&compare=60841c5b7c69a1bbb3f06536ed685a48
public static final String POST_URL2 = "http://admin.wap.china.com/user/NavigateTypeAction.do?processID=getNavigateNews" ;
// 請求引數:page=1&code=news&pageSize=20&parentid=0&type=1
private TextView tvShow;
private void assignViews() {
tvShow = (TextView) findViewById(R.id.tv_show);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
assignViews();
initOkHttp();
}
private void initOkHttp() {
client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.build();
}
//此處為按鈕點選事件:Get請求、Post請求、Post上傳檔案
public void okhttp(View view) {
switch (view.getId()) {
case R.id.btn_get: //Get請求
Request request = new Request.Builder()
.get()
.url(GET_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 {
final String string = response.body().string();
// Log.i(TAG, "onResponse: "+string);
runOnUiThread(new Runnable() {
@Override
public void run() {
tvShow.setText(string);
}
});
}
});
break;
case R.id.btn_post: //Post請求
// 請求條件:platform=2&gifttype=2&compare=60841c5b7c69a1bbb3f06536ed685a48
// 請求引數:page=1&code=news&pageSize=20&parentid=0&type=1
RequestBody requestBodyPost = new FormBody.Builder()
.add("page", "1")
.add("code", "news")
.add("pageSize", "20")
.add("parentid", "0")
.add("type", "1")
.build();
Request requestPost = new Request.Builder()
.url(POST_URL)
.post(requestBodyPost)
.build();
client.newCall(requestPost).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String string = response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
tvShow.setText(string);
}
});
}
});
break;
case R.id.btn_post_file: //Post請求上傳檔案
File file = new File(Environment.getExternalStorageDirectory(), "dd.mp4");
if (!file.exists()) {
Toast.makeText(MainActivity.this, "檔案不存在", Toast.LENGTH_SHORT).show();
} else {
RequestBody fileBody = RequestBody.create(MediaType.parse(TYPE), file);
RequestBody requestBody = new MultipartBody.Builder().addFormDataPart("filename", file.getName(), fileBody).build();
Request requestPostFile = new Request.Builder()
.url("http://10.11.64.50/upload/UploadServlet")
.post(requestBody)
.build();
client.newCall(requestPostFile).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
runOnUiThread(new Runnable() {
@Override
public void run() {
tvShow.setText(response.toString());
}
});
}
});
}
break;
}
}
}