java.net.ProtocolException:预期为229个字节,但接收到8192

时间:2018-12-10 07:00:46

标签: java android

有人可以帮我解决这个问题吗?所以,我遇到一个错误的情况

  

java.net.ProtocolException:预期为229个字节,但接收到8192

当我从捕获相机的结果中完成保存照片并将我立即将其上传到服务器时,出现此错误。对于这样的编码

ServiceGenerator.java

public class ServiceGenerator {
    private static OkHttpClient.Builder builder() {
        OkHttpClient.Builder okhttpBuilder = new OkHttpClient().newBuilder();
        okhttpBuilder.connectTimeout(60, TimeUnit.SECONDS);
        okhttpBuilder.writeTimeout(60, TimeUnit.SECONDS);
        okhttpBuilder.readTimeout(60, TimeUnit.SECONDS);

        return okhttpBuilder;
    }

    private static HttpLoggingInterceptor interceptor() {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        return interceptor;
    }

    public static <S> S createBaseService(Context context, Class<S> serviceClass) {
        OkHttpClient.Builder okhttpBuilder = builder();

        if (BuildConfig.DEBUG) {
            okhttpBuilder.addInterceptor(interceptor());
        }

        final Auth auth = PrefManager.getPref(context, PrefManager.PREF_AUTH, Auth.class);

        if(auth != null) {
            okhttpBuilder.addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request request = chain.request();
                    Request newReq = request.newBuilder()
                            .header("Authorization", "Bearer " + auth.getToken())
                            .header("Accept", "application/json")
                            .header("Content-Type", "application/x-www-form-urlencoded")
                            .build();
                    return chain.proceed(newReq);
                }
            });
        }

        OkHttpClient client = okhttpBuilder.build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Endpoint.BASE_URL)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        return retrofit.create(serviceClass);
    }
}

EditProfileActivity.java

private void savePicture(Bitmap bitmap) {
    File captureFile = null;
    try {
        captureFile = createImageFile("Camera");
        FileOutputStream stream = new FileOutputStream(captureFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        stream.flush();
        stream.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
}

private File createImageFile(String directory) {
    // External sdcard location
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DCIM), directory);

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "IMG_" + timeStamp;

    File imageFile = null;
    try {
        imageFile = File.createTempFile(imageFileName, ".jpg", mediaStorageDir);
        Log.d(TAG, imageFile.getPath());
        Bus.post(new ChangePictureEvent(imageFile));
    } catch (IOException e) {
        Log.d(TAG, "Oops! Failed create " + imageFileName + " file");
    }
    return imageFile;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        savePicture(photo);
    }
}

编辑ProfileFragment.java

@Subscribe
public void pictureEvent(ChangePictureEvent event) {
    File file = event.getFilePath();
    String extension = file.getName().substring(file.getName().lastIndexOf(".") + 1);
    RequestBody reqFile = RequestBody.create(MediaType.parse("image/"+extension), file);
    MultipartBody.Part body = MultipartBody.Part.createFormData("image", file.getName(), reqFile);
    ProfileService services = ServiceGenerator.createBaseService(context, ProfileService.class);
    Call<Response<String>> call = services.changePictures(body);

    call.enqueue(new Callback<Response<String>>() {
        @Override
        public void onResponse(Call<Response<String>> call, retrofit2.Response<Response<String>> response) {
            Response<String> data = response.body();
            Log.e("message", data.getData());
        }

        @Override
        public void onFailure(Call<Response<String>> call, Throwable t) {
            Log.e(TAG+".error", t.toString());
        }
    });
}

我使用的网络库是Retrofit2。寻求帮助来解决此问题。

0 个答案:

没有答案