0

I am using retrofit 2.6. when post a request to my web server it shows error Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $. I have used bellow code..

  1. In android i have used to post request like bellow ..

      private void getPosts() {
        String basurl = "http://10.11.201.179:8084/TestWeb/";
    
        Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(basurl)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    
        JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
       Map<String, String> parameters = new HashMap<>();
       parameters.put("user_id", "12345");
       parameters.put("password", "123");
    
      Call<List<Post>> call = jsonPlaceHolderApi.getPosts(parameters);
    
    
      call.enqueue(new Callback<List<Post>>() {
        @Override
        public void onResponse(Call<List<Post>> call, retrofit2.Response<List<Post>> response) {
            Log.e("response.body", response.toString());
            if (!response.isSuccessful()) {
                // textViewResult.setText("Code: " + response.code());
                return;
            }
    
            Log.e("response.body", response.body().toString());
    
            List<Post> posts = response.body();
    
            for (Post post : posts) {
                Log.e("name", post.getName());
    
                //textViewResult.append(content);
            }
        }
    
        @Override
        public void onFailure(Call<List<Post>> call, Throwable t) {
            Log.e("***errror", t.getMessage());
        }
     });
    
    }
    
  2. Here is my interface to post name JsonPlaceHolderApi

    public interface JsonPlaceHolderApi {
      @POST("TestS")
      Call<List<Post>> getPosts(@QueryMap Map<String, String> parameters);
     }
    
  3. Here i have bind the response server

    public class Post {
     @SerializedName("mobile")
     @Expose
     public String mobile;
    
    @SerializedName("name")
    @Expose
    public String name;
    
    //Getter serter
    }
    
  4. Here is my json

     {
      "name": "1",
      "mobile": "01921687433"
      }
    

Please help me

8
  • You are expecting a List<Post> but your response does not start with a JsonArray here . If thats your response you should be expecting Call<Post> only or modify your response as Array. Commented Jul 14, 2019 at 3:39
  • please show api response Commented Jul 14, 2019 at 5:17
  • @darwin it show error BEGIN_ARRAY but was BEGIN_OBJECT at line Commented Jul 14, 2019 at 5:24
  • can u show the api response from postman of any other rest client, the error indicates that your are expecting a list of Post's but it returns an Json object instead of array. Commented Jul 14, 2019 at 5:25
  • may be your list of post's wrapped inside another object Commented Jul 14, 2019 at 5:26

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.