使用Retrofit上传Android Photo Camera - ASP.NET MVC端点

时间:2015-07-08 22:03:54

标签: android asp.net-mvc retrofit

希望能够在我的Android应用中将图像发布到我的后端。我在这做错了什么?我一直收到内部服务器错误,但我知道端点已启动并正常工作(能够从其他客户端调用它)

我有一个像这样的ASP.NET MVC端点设置:

    [HttpPost]
    public JsonResult PostImage(int id, string accessToken, HttpPostedFileBase img)

在我的Android代码中,我正在使用Retrofit这样的设置:

@Multipart
@POST("/endpoint/postimage")
void postImage(
        @Part("id") int id,
        @Part("accessToken") TypedString accessToken,
        @Part("img") TypedByteArray img,
        Callback<Boolean> callback);

我有这样的图像处理代码:

ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, bos);
byte[] bitmapData = bos.toByteArray();

//  convert to typed
TypedByteArray typedByteArray = new TypedByteArray("application/octet-stream", bytes);
TypedString str = new TypedString("myString");

RestAdapter adapter = new RestAdapter.Builder()
        .setEndpoint("https://myendpoint.net").build();

ApiService apiService = adapter.create(ApiService.class);
apiService.PostImage(1492, str, typedByteArray, new Callback<Boolean>() {

    @Override
    public void success(Boolean success, Response response) {

        Boolean test = success;
    }

    @Override
    public void failure(RetrofitError error) {

        //Log.e("TappedIn", error.getMessage());
    }
});

1 个答案:

答案 0 :(得分:0)

仅供参考我已经解决了这个问题!

这是我的最终代码:

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

  if (requestCode == 1 && resultCode == RESULT_OK) {

  Bundle extras = data.getExtras();
  Bitmap bitmap = (Bitmap) extras.get("data");

  ByteArrayOutputStream out = new ByteArrayOutputStream();
  bitmap.compress(Bitmap.CompressFormat.JPEG, 65, out);
  byte[] bytes = out.toByteArray();

  RestAdapter adapter = new RestAdapter.Builder()
  .setEndpoint("https://myendpoint.com").build();

  ApiService apiService = adapter.create(ApiService.class);

  TypedByteArray typedByteArray = new TypedByteArray("image/*", bytes) {
    @Override
    public String fileName() {
    return "img.jpg";
    }
  };

TypedString myVar = new TypedString("abc");

apiService.postVenueMenu(venueId, myVar, typedByteArray, new Callback<SuccessDTO>() {

                @Override
                public void success(SuccessDTO success, Response response) {

                }

                @Override
                public void failure(RetrofitError error) {

                }
            });
        }
    }