简体   繁体   中英

Retrofit recyclerview Expected a string but was BEGIN_OBJECT at line 1 column 2 pth $

I was using the retrofit get method to fetch some details from the server.

My retrofit interface is

@GET("studentlist/{schoolid}/{driverid}")
    Call<String> getStudentList(@Path("schoolid") String schoolid,@Path("driverid") String driverid);

And when i call my activity I'm getting the two query from the bundle and the data like


        Bundle bundle = getIntent().getExtras();
        schoolname = bundle.getString("school_id");
        driverid = bundle.getString("dri_number");

Call<String> call = api.getStudentList(schoolname, driverid);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Toast.makeText(StudentListActivity.this, response.message(), Toast.LENGTH_SHORT).show();
                if (response.isSuccessful()){
                    if (response.body() != null){
                        Toast.makeText(StudentListActivity.this, response.body(), Toast.LENGTH_SHORT).show();
                        String jsonResponse = response.body().toString();
                        writeRecycler(jsonResponse);
                    } else {
                        Log.i("onEmptyResponse", "Returned empty response");
                    }
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Toast.makeText(StudentListActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });

And saving the response like

    @SerializedName("status")
    private String status;

    @SerializedName("res")
    public List<StudentListResponse.StudentsList> resp = new ArrayList<>();

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

probably your json has the wrong structure. Verify your JSON structure and validate.

Verify if your JSON data starts with an open quotes.

Gson is expecting your JSON string to begin with an object opening brace. eg

{

But the string you have passed to it starts with an open quotes

"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM