關(guān)于okhttp上傳文件
老師講的類已經(jīng)被不用了,對(duì)于新的okhttp3.5.0,上傳表單帶圖片怎么圖片一直傳布上去,也看了okhttp3.x的demo
public?void?doUpload(View?view)?{ ????File?file?=?new?File(Environment.getExternalStorageDirectory(),?"2.jpg"); ????if?(!file.exists())?{ ????????L.e(file.getAbsolutePath()?+?"not?exist!"); ????} ????RequestBody?requestBody?=?new?MultipartBody.Builder() ????????????.setType(MultipartBody.FORM) ????????????.addFormDataPart("username",?"xiaoqiang") ????????????.addFormDataPart("password",?"123") ????????????.addFormDataPart("mPhoto",?"2.jpg",?RequestBody.create(MediaType.parse("image/jpeg"),?file)) ????????????.build(); ????Request?request?=?new?Request.Builder() ????????????.url(BaseUrl?+?"uploadInfo") ????????????.post(requestBody) ????????????.build(); ????executeRequest(request); }
這個(gè)是我寫的,可是一直報(bào)錯(cuò),在服務(wù)端圖片接收不到,圖片是按老師寫的
2016-12-28
因?yàn)槲伊?xí)慣性的寫的private
2016-12-28
應(yīng)該把你的報(bào)錯(cuò)信息貼出來,否則不是經(jīng)驗(yàn)豐富的我們只有猜。我按照視頻的敲,服務(wù)端也接受不到圖片,最后在網(wǎng)上找到原因,mPhoto和mPhotoFileName兩個(gè)變量要寫set和get方法,重啟服務(wù)器就可以接收到了。不一定對(duì)你有用,希望能幫到你。
2016-12-23
if?(!file.exists())?{
????????L.e(file.getAbsolutePath()?+?"not?exist!");
????return;
????}
試試能不能走下去