如何使用翻新Android捕获图像并发布数据

时间:2019-06-19 09:13:09

标签: android retrofit2

我以前推送数据时遇到错误,而我得到的错误是:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.oakridgebs.dealsinmall, PID: 3414
java.lang.IllegalArgumentException: Only one encoding annotation is allowed.
    for method ClickAndPostApi.clicPost
    at retrofit2.Utils.methodError(Utils.java:52)
    at retrofit2.Utils.methodError(Utils.java:42)
    at retrofit2.RequestFactory$Builder.parseMethodAnnotation(RequestFactory.java:227)
    at retrofit2.RequestFactory$Builder.build(RequestFactory.java:161)
    at retrofit2.RequestFactory.parseAnnotations(RequestFactory.java:65)
    at retrofit2.ServiceMethod.parseAnnotations(ServiceMethod.java:25)
    at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:168)
    at retrofit2.Retrofit$1.invoke(Retrofit.java:147)
    at java.lang.reflect.Proxy.invoke(Proxy.java:913)
    at $Proxy9.clicPost(Unknown Source)
    at com.oakridgebs.dealsinmall.ClickAndPostActivity$1.onClick(ClickAndPostActivity.java:134)
    at android.view.View.performClick(View.java:6392)
    at android.view.View$PerformClick.run(View.java:25121)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:197)
    at android.app.ActivityThread.main(ActivityThread.java:7022)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:515)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:837)

这是我的界面

@Multipart
@FormUrlEncoded
@POST("click_and_posts")
Call<ResponseBody> clicPost(
        @Header("Token") String token,  // if there is headers
        @Part  MultipartBody.Part file,
        @Field("click_and_post[category_id]") String category_id,
        @Field("click_and_post[brand_id]") String brand_id,
        @Field("click_and_post[location]") String location
);

我正在捕获图像,并按照以下“活动结果”中所示的方法获取uri

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 7 && resultCode == RESULT_OK && data != null && data.getData() != null) {

        photoUri = data.getData();
        try {
            // Adding captured image in bitmap.
            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), photoUri);

            // adding captured image in imageview.
            capturedImage.setImageBitmap(bitmap);

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

在这种情况下,我正在服用photouri,并且我正在通过,如下所示

我的代码是

File originalFile = FileUtils.getFile(getApplicationContext(), photoUri);
            RequestBody filePart = RequestBody.create(MediaType.parse(getContentResolver().getType(photoUri)),
                    originalFile);

            MultipartBody.Part file = MultipartBody.Part.createFormData("click_and_post[image]", originalFile.getName(), filePart);


            Call<ResponseBody> call = LoginSignupClients.getInstance().getApiClickandPost().clicPost(token, file, category_id, brand_id, location );
            call.enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                    if (response.code() == 200){
                        Toast.makeText(ClickAndPostActivity.this, "You Posted the Deal Sucessfully", Toast.LENGTH_LONG).show();
                        Intent intent = new Intent(ClickAndPostActivity.this, DashboardActivity.class);
                        startActivity(intent);
                        finish();
                    }else {
                        Toast.makeText(ClickAndPostActivity.this, "Unable to post Your Data", Toast.LENGTH_LONG).show();
                    }
                }

                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {
                    Toast.makeText(ClickAndPostActivity.this, t.getMessage(), Toast.LENGTH_LONG).show();
                }
            });

否则,有任何方法可以传递位图。你能帮我吗

1 个答案:

答案 0 :(得分:0)

在所描述的错误中,它只能使用一个编码注释,因此请尝试删除以下注释:

 @FormUrlEncoded

在您的界面中