如何获得“数据”中的“url”字段?

2022-01-28 13:08:24 标签 javaandroidjsonretrofitpogo

我试图得到一个直接的url通过图像,我上传到imgbb。com与POST

这是一个json,我应该得到当我发送一个帖子到imgbb https://api。imgbb。com/

{
  "data": {
    "id": "2ndCYJK",
    "title": "c1f64245afb2",
    "url_viewer": "https://ibb.co/2ndCYJK",
    "url": "https://i.ibb.co/w04Prt6/c1f64245afb2.gif",
    "display_url": "https://i.ibb.co/98W13PY/c1f64245afb2.gif",
    "size": "42",
    "time": "1552042565",
    "expiration":"0",
    "image": {
      "filename": "c1f64245afb2.gif",
      "name": "c1f64245afb2",
      "mime": "image/gif",
      "extension": "gif",
      "url": "https://i.ibb.co/w04Prt6/c1f64245afb2.gif",
    },
    "thumb": {
      "filename": "c1f64245afb2.gif",
      "name": "c1f64245afb2",
      "mime": "image/gif",
      "extension": "gif",
      "url": "https://i.ibb.co/2ndCYJK/c1f64245afb2.gif",
    },
    "medium": {
      "filename": "c1f64245afb2.gif",
      "name": "c1f64245afb2",
      "mime": "image/gif",
      "extension": "gif",
      "url": "https://i.ibb.co/98W13PY/c1f64245afb2.gif",
    },
    "delete_url": "https://ibb.co/2ndCYJK/670a7e48ddcb85ac340c717a41047e5c"
  },
  "success": true,
  "status": 200
}

我试图创建一个POJO,但我的POJO转换器告诉我,这是一个不正确的JSON。当我试图得到一个响应体,我不能从那里找到url。

我的代码是:

在MainActivity

private void uploadToServer(Bitmap bitmap, Context context) throws IOException {
        //Convert bitmap to byte array
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
        byte[] bitmapData = bos.toByteArray();
        //create file
        String currentDate = new SimpleDateFormat("ddMMyyyy_HHmmss", Locale.getDefault()).format(new Date());
        File f = new File(context.getCacheDir(), "temp_"+currentDate );
        f.createNewFile();
        FileOutputStream fos = new FileOutputStream(f);
        fos.write(bitmapData);
        fos.flush();
        fos.close();
        Retrofit retrofit = RetroClient.getRetrofitClient(context);
        UploadApi uploadAPIs = retrofit.create(UploadApi.class);
        RequestBody requestFile =  RequestBody.create(MediaType.parse("multipart/form-data"), f);
        MultipartBody.Part body = MultipartBody.Part.createFormData("image", f.getName(), requestFile);
        Call<ResponseBody> call = uploadAPIs.uploadImage(RetroClient.KEY_API,body);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.isSuccessful()) {
                    Toast.makeText(context, "I send photo", Toast.LENGTH_SHORT);
                    response.body();
                }
                else {
                    Toast.makeText(context, "response isn't successful", Toast.LENGTH_SHORT);
                }
            }
            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Toast.makeText(context, "on failure", Toast.LENGTH_SHORT);
            }
        });
    }

接口

public interface UploadApi {
    @Multipart()
    @POST("/1/upload?expiration=100&key=")
    Call<ResponseBody> uploadImage(@Query("key") String key, @Part() MultipartBody.Part file );
}
阅读全文

▼ 版权说明

相关文章也很精彩
推荐内容
更多标签
相关热门
全站排行
随便看看

错说 cuoshuo.com —— 程序员的报错记录

部分内容根据CC版权协议转载;网站内容仅供参考,生产环境使用务必查阅官方文档

辽ICP备19011660号-5

×

扫码关注公众号:职场神器
发送: 1
获取永久解锁本站全部文章的验证码