Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();
                }
            }
        }
like image 950
Jayesh Rathod Avatar asked Jan 17 '18 08:01

Jayesh Rathod


Video Answer


3 Answers

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();
            }
        }
    }
like image 127
AMAN SINGH Avatar answered Oct 18 '22 20:10

AMAN SINGH


.addConverterFactory(ScalarsConverterFactory.create()) .addxxxx

like image 5
Super_PAN Avatar answered Oct 18 '22 22:10

Super_PAN


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'

like image 3
Dattatray Nande Avatar answered Oct 18 '22 22:10

Dattatray Nande