当前位置: 首页 > 知识库问答 >
问题:

改装应为字符串,但在第1行第2列路径处为BEGIN\u对象$

颜华池
2023-03-14

我试图只获取字符串请求,但它的给定错误如下

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

我的API输出如下:

{
    "status": true,
    "message": "Video uploaded successfully!",
    "data": {
        "video_name": "674631516178278_abc2.mp4",
        "video_thumbnail": "674631516178278_thumb0017.jpg"
    }
}

响应代码:

        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            try {
                if (response.body() != null)
                    Log.e("UploadResponse>>>", response.body());
                layoutUpload.setVisibility(View.GONE);
                if (file.exists()) {
                    file.delete();
                    thumb.delete();
                }
            } catch (Exception e) {
                e.printStackTrace();
                layoutUpload.setVisibility(View.GONE);
                if (file.exists()) {
                    file.delete();
                    thumb.delete();
                }
            }
        }

共有3个答案

奚正谊
2023-03-14

错误表示您想在字符串正文中获得结果。如果您想这样做,只需在Retrofit. Builder中添加ScalarsConverterFactory.create()。

public static Retrofit getClient() {
    Gson gson = new GsonBuilder()
            .setLenient()
            .create();
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(getBaseUrl())
            .addConverterFactory(ScalarsConverterFactory.create())
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();
    return retrofit;
}

在应用程序级别使用改造实施build.gradle.

实现的com。整理一下。改装2:转换器标量:2.1.0'

贺经纶
2023-03-14

.addConverterFactory(ScalarsConverterFactory.create())。地址XXXX

萧安怡
2023-03-14

像这样试试

 @Override
    public void onResponse(Call<ResponseBody> call, Response< ResponseBody > response) {
        try {
            if (response.body() != null)
                Log.e("UploadResponse>>>", response.body());
            layoutUpload.setVisibility(View.GONE);
            if (file.exists()) {
                file.delete();
                thumb.delete();
            }
        } catch (Exception e) {
            e.printStackTrace();
            layoutUpload.setVisibility(View.GONE);
            if (file.exists()) {
                file.delete();
                thumb.delete();
            }
        }
    }
 类似资料: