retrofit Expected a string but was BEGIN_OBJECT at line 1 column 2 path $

若如初见. 提交于 2020-01-03 18:53:12

问题


I'm trying to get just string request but it's give error like this

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

And my API output is like this :

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

Code for response :

        @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();
                }
            }
        }

回答1:


Try like this

 @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();
            }
        }
    }



回答2:


.addConverterFactory(ScalarsConverterFactory.create()) .addxxxx




回答3:


I also faced similar issue. The answer provided by Dattatray Nande i.e. simply adding ScalarsConverterFactory.create() in Retrofit.Builder helped.

Additionally don't forget to add content-type as shown below to your interface

@Headers("Content-Type: text/html")
@GET("/apiMethod")
Call<String> YourAPIFunction();



回答4:


Error say's you want to get result in String body. If you want to do this, Just add ScalarsConverterFactory.create() in your Retrofit.Builder.

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;
}

Use retrofit Implementation in app level build.gradle.

implementation 'com.squareup.retrofit2:converter-scalars:2.1.0'



来源:https://stackoverflow.com/questions/48296987/retrofit-expected-a-string-but-was-begin-object-at-line-1-column-2-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!