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

Enamul Haque
  • 4,789
  • 1
  • 37
  • 50
  • You are expecting a `List` but your response does not start with a `JsonArray` here . If thats your response you should be expecting `Call` only or modify your response as Array. – ADM Jul 14 '19 at 03:39
  • please show api response – darwin Jul 14 '19 at 05:17
  • @darwin it show error BEGIN_ARRAY but was BEGIN_OBJECT at line – Enamul Haque Jul 14 '19 at 05: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. – darwin Jul 14 '19 at 05:25
  • may be your list of post's wrapped inside another object – darwin Jul 14 '19 at 05:26
  • @darwin post man {"out_code":"0","out_message":"Successfully Login ","user_id":"D002","name":"Enamul Haque","mobile":"01921687433","email":"","usertype":"MEM","deposit_amount":"383000"} – Enamul Haque Jul 14 '19 at 05:27
  • @darwin i have also tried like public void onResponse(Call call, retrofit2.Response response) { Log.e("response.body", response.toString()); if (!response.isSuccessful()) { // textViewResult.setText("Code: " + response.code()); return; } } – Enamul Haque Jul 14 '19 at 05:31
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/196437/discussion-between-darwin-and-enamul-haque). – darwin Jul 14 '19 at 05:31

0 Answers0