我是新的改造,並在我的應用程序中,我試圖發送圖像到服務器從圖庫使用retrofit,但我一直在收到「RetrofitError:預期的329字節,但收到4096「的改造失敗方法的迴應。我從onAvtivityResult的intent中創建位圖,並將文件對象的路徑作爲名稱和位圖。RetrofitError:預計329字節,但在Android中收到4096
Uri mImgUri = returnedIntent.getData();
InputStream imageStream = getContentResolver().openInputStream(mImgUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
This is the error log,
retrofit.RetrofitError: expected 329 bytes but received 4096
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:395)
at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)
at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at retrofit.Platform$Android$2$1.run(Platform.java:142)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.net.ProtocolException: expected 329 bytes but received 4096
at com.squareup.okhttp.internal.http.HttpConnection$FixedLengthSink.write(HttpConnection.java:299)
at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:133)
at okio.RealBufferedSink$1.write(RealBufferedSink.java:148)
at retrofit.mime.TypedFile.writeTo(TypedFile.java:78)
This is my Retrofit adapter,
private void startRetrofitTask(Bitmap mBitmap,Uri mImgUri){
String mimeType = "image/png";
File mFile = AppUtils.convertBitmapToFile(this,mImgUri.getLastPathSegment(),mBitmap);
TypedFile fileToSend = null;
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(Constants.LOCAL_BASE_URL)
.setClient(new OkClient(new OkHttpClient()))
.build();
RetrofitCommonService mRetrofitCommonService = restAdapter.create(RetrofitCommonService.class);
if(mFile != null){
fileToSend = new TypedFile(mimeType, mFile);
}
mRetrofitCommonService.upLoadImage(new AppUtils(this).getPhone(),
getDeviceId(), fileToSend,this);
}
public static File convertBitmapToFile(Context mContext,String mName,Bitmap bitmap) {
if(bitmap == null)return null;
File filesDir = mContext.getFilesDir();
File imageFile = new File(filesDir, mName + ".png");
OutputStream os;
try {
os = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
os.flush();
os.close();
} catch (Exception e) {
Log.e("TAG", "Error writing bitmap", e);
}
return imageFile;
}
@Multipart
@POST("/user/asset")
@Headers("Accept:application/json")
void upLoadImage(@Header("mobile-number") String mPhone, @Header("uid") String imei,
@Part("file") TypedFile mBody,Callback<RetrofitResponse> response);
我得到了同樣的錯誤在一個手機,其他的手機都OK。我不知道什麼是錯的。 –