android ——Okhttp 多参数表单提交图片给服务器

表单上传图片

private voiduploadMultiFile() {

finalFile file = FileUtils.getSmallBitmap(mFile.getPath());

Uri uri = Uri.fromFile(file);

Log.e(TAG,"path:"+path);

Log.e(TAG,"mFile:"+ String.valueOf(mFile));

Log.e(TAG,"file:"+ String.valueOf(file));

Log.e(TAG,"uri:"+ String.valueOf(uri));

RequestBody fileBody = RequestBody.create(MediaType.parse("image"), newFile(getCachePath(this),"user-avatar.jpg"));

RequestBody requestBody =newMultipartBody.Builder()

.setType(MultipartBody.FORM)

.addFormDataPart("image[]","user-avatar.jpg",fileBody)

.addFormDataPart("uid",MeManager.getUid())

.addFormDataPart("token",MeManager.getToken())

.build();

Request request =newRequest.Builder()

.url(NetConfig.HOST+HEAD_CHANGE)

.post(requestBody)

.build();

finalokhttp3.OkHttpClient.Builder httpBuilder =newOkHttpClient.Builder();

OkHttpClient client = httpBuilder

.connectTimeout(10,TimeUnit.SECONDS)

.writeTimeout(15,TimeUnit.SECONDS)

.build();

client.newCall(request).enqueue(newCallback() {

@Override

public voidonFailure(Call call,IOException e) {

Log.e(TAG,"uploadMultiFile() e="+ e);

runOnUiThread(newRunnable() {

@Override

public voidrun() {

ToastUtils.showToast(R.string.request_failed);

}

});

}

@Override

public voidonResponse(Call call,Response response)throwsIOException {

String result = response.body().string();

try{

Log.e(TAG,"修改头像:"+ result);

JSONObject jsonObject =newJSONObject(result);

String code = jsonObject.getString("code");

if(code.equals("s_ok")) {

runOnUiThread(newRunnable() {

@Override

public voidrun() {

ToastUtils.showToast(R.string.change_head_success);

}

});

}else if(code.equals("error")) {

String error = jsonObject.getString("message");

runOnUiThread(newRunnable() {

@Override

public voidrun() {

ToastUtils.showToast(R.string.request_failed+error);

}

});

}

}catch(JSONException e) {

e.printStackTrace();

}

}

});

}

post请求

public voidPost(Viewv) {

String url=post_url.getText().toString();

String key=post_key.getText().toString();

String value=post_value.getText().toString();

okhttp3.OkHttpClient.Builder httpBuilder=newOkHttpClient.Builder();

OkHttpClient client=httpBuilder

.connectTimeout(30,TimeUnit.SECONDS)

.writeTimeout(30,TimeUnit.SECONDS)

.writeTimeout(180,TimeUnit.SECONDS)

.build();

FormBody.Builder builder=newFormBody.Builder();

/*添加两个参数*/

builder.add(key,value);

FormBody body=builder.build();

Request request=newRequest

.Builder()

.url(url)

.post(body)

.build();

client.newCall(request).enqueue(newCallback() {

public voidonResponse(Callcall,Responseresponse)throwsIOException{

finalString bodyStr= response.body().string();

final booleanok= response.isSuccessful();

runOnUiThread(newRunnable() {

public voidrun() {

if(ok) {

text_show.setText(bodyStr);

Toast.makeText(TestPostactivity.this,bodyStr,Toast.LENGTH_SHORT).show();

}else{

Toast.makeText(TestPostactivity.this,"server error : "+bodyStr,Toast.LENGTH_SHORT).show();

}

}

});

}

public voidonFailure(Callcall, finalIOExceptione) {

runOnUiThread(newRunnable() {

public voidrun() {

Toast.makeText(TestPostactivity.this,"error : "+e.toString(),Toast.LENGTH_SHORT).show();

}

});

}

});

}

}

你可能感兴趣的:(android ——Okhttp 多参数表单提交图片给服务器)