關于okhttp上傳文件
老師講的類已經被不用了,對于新的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); }
這個是我寫的,可是一直報錯,在服務端圖片接收不到,圖片是按老師寫的
2016-12-28
因為我習慣性的寫的private
2016-12-28
應該把你的報錯信息貼出來,否則不是經驗豐富的我們只有猜。我按照視頻的敲,服務端也接受不到圖片,最后在網上找到原因,mPhoto和mPhotoFileName兩個變量要寫set和get方法,重啟服務器就可以接收到了。不一定對你有用,希望能幫到你。
2016-12-23
if?(!file.exists())?{
????????L.e(file.getAbsolutePath()?+?"not?exist!");
????return;
????}
試試能不能走下去