4

I don't know where my problem is. I've to post raw body like

{"app_id":"abced","app_key":"abced","request":"login","username":"abce@gmail.com","password":"1234"}

I'm using retrofit 2.0. I created a interface like

    @FormUrlEncoded
@POST("index.php/")
Call<LoginResponse> getHome(@Field("app_id") String request,
                            @Field("app_key") String request1,
                            @Field("request") String request2,
                            @Field("username") String request3,
                            @Field("password") String request4);

and calling like this:

 Retrofit retrofitget = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MyAPI ApiData = retrofitget.create(MyAPI.class);
        Call<LoginResponse> GalleryGetSet = ApiData.getHome("abced","abced","login","abce@gmail.com","1234");
        GalleryGetSet.enqueue(new Callback<LoginResponse>() {
            @Override
            public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
                Log.e(TAG, "data1" + response.body().getError());
            }

            @Override
            public void onFailure(Call<LoginResponse> call, Throwable t) {
                Log.e(TAG, "Error2" + t.getMessage());
            }
        });

always getting error : End of input at line 1 column 1 Is it right process to post the raw body data through retrofit post or i'm wrong. Already google it but not got my solution. Please anyone help me out.

I also tried in this way in interface

@FormUrlEncoded
@POST("index.php")
Call<LoginResponse> getHome1(@Field("login") String data);

and while calling

 Retrofit retrofitget = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
        MyAPI ApiData = retrofitget.create(MyAPI.class);

        Call<LoginResponse> listGalleryGetSet = ApiData.getHome("ABCD","ABCD","login","ABC@gmail.com","1234");
        listGalleryGetSet.enqueue(new Callback<LoginResponse>() {
            @Override
            public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
                Log.e(TAG, "data1" + response.body().getError());
            }

            @Override
            public void onFailure(Call<LoginResponse> call, Throwable t) {
                Log.e(TAG, "Error" + t.getMessage());
            }
        });

My LoginResponse class

public class LoginResponse {

private String error;

private Boolean vlink;

/**
 *
 * @return
 * The error
 */
public String getError() {
    return error;
}

/**
 *
 * @param error
 * The error
 */
public void setError(String error) {
    this.error = error;
}

/**
 *
 * @return
 * The vlink
 */
public Boolean getVlink() {
    return vlink;
}

/**
 *
 * @param vlink
 * The vlink
 */
public void setVlink(Boolean vlink) {
    this.vlink = vlink;
}

}

**This is how I'm posting on postman**

dependency

 compile 'com.squareup.retrofit2:retrofit-converters:2.0.0-beta3'
 compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
 compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'

Thanks in advance

AMAN SINGH
  • 3,491
  • 6
  • 28
  • 44
  • and what is your example login json response. If you update it too i will fix your problem – Yasin Kaçmaz May 23 '16 at 15:51
  • pls check it I already updated my LoginResponse pojo class **{"error":"Account Not Activated. Please check you email for Activate your Account.","vlink":true}** And this is my json response – AMAN SINGH May 24 '16 at 07:43
  • if anyone needs any help, feel free to ask anytime – AMAN SINGH Dec 28 '16 at 12:46
  • 1
    @AMANSINGH can you please share your answer. I had the same problem but didn't find any solution! – Mitesh Vanaliya Jun 27 '18 at 05:47
  • @AMANSINGH I am also having the same issue but couldn't find a solution yet. Can you be more specific about how you solved the issue? From the below-accepted answers, I couldn't find any solution which works for me. – Nikhil Aug 07 '18 at 09:15
  • what type of data you've to post @Nikhil – AMAN SINGH Aug 07 '18 at 10:11
  • better ask new SO and I'll reply you there – AMAN SINGH Aug 07 '18 at 10:11
  • @AMANSINGH Thank you, buddy, I have sorted it out using this https://stackoverflow.com/questions/32519618/retrofit-2-0-how-to-get-deserialised-error-response-body?rq=1 – Nikhil Aug 07 '18 at 11:14

3 Answers3

5

Instead of using

@POST("index.php")

please try to use

@POST("/index.php")

and post as a

Call<LoginResponse> getHome(@Body YourBodyPojo pojo);

surely this will help you.

1

Paste your example body Json to this site, select source Json, annotation None : Json Schema 2 Pojo

Then in your Api :

@POST("index.php")
public Call<LoginResponse> getHome(@Body YourBodyPojo pojo);

Then calling :

OkHttpClient okClient = new OkHttpClient.Builder()
            .connectTimeout(5,TimeUnit.SECONDS)
            .build();


    Gson gson = new GsonBuilder()
            .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
            .create();

    Retrofit client = new Retrofit.Builder()
            .baseUrl(baseUrl)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .client(okClient)
            .build();

    MyAPI apiData = retrofitget.create(MyAPI.class);

    LoginRequest loginRequest=new LoginRequest(); 
    loginRequest.setUserName("abc"); 
    loginRequest.setPassword("123");  
    Call<LoginResponse> listGalleryGetSet = apiData.getHome(loginRequest);
    listGalleryGetSet.enqueue(new Callback<LoginResponse>() {
        @Override
        public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
            Log.e(TAG, "data1" + response.body().getError());
        }

        @Override
        public void onFailure(Call<LoginResponse> call, Throwable t) {
            Log.e(TAG, "Error" + t.getMessage());
        }
    });

Response class :

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;


public class YourResponse{

@SerializedName("error")
@Expose
private String error;
@SerializedName("vlink")
@Expose
private Boolean vlink;

/**
 * 
 * @return
 * The error
 */
 public String getError() {
     return error;
 }

/**
 * 
 * @param error
 * The error
 */
 public void setError(String error) {
    this.error = error;
 }

/**
 * 
 * @return
 * The vlink
 */
 public Boolean getVlink() {
    return vlink;
 }

/**
 * 
 * @param vlink
 * The vlink
 */
 public void setVlink(Boolean vlink) {
     this.vlink = vlink;
 }

}

Body class :

package com.example;


public class YourBodyClass{

private String appId;
private String appKey;
private String request;
private String username;
private String password;

/**
 * 
 * @return
 * The appId
 */
public String getAppId() {
   return appId;
}

/**
 * 
 * @param appId
 * The app_id
 */
public void setAppId(String appId) {
   this.appId = appId;
}

/**
 * 
 * @return
 * The appKey
 */
public String getAppKey() {
   return appKey;
}

/**
 * 
 * @param appKey
 * The app_key
 */
public void setAppKey(String appKey) {
   this.appKey = appKey;
}

/**
 * 
 * @return
 * The request
 */
public String getRequest() {
   return request;
}

/**
 * 
 * @param request
 * The request
 */
public void setRequest(String request) {
   this.request = request;
}

/**
 * 
 * @return
 * The username
 */
public String getUsername() {
   return username;
}

/**
 * 
 * @param username
 * The username
 */
public void setUsername(String username) {
   this.username = username;
}

/**
 * 
 * @return
 * The password
 */
public String getPassword() {
   return password;
}

/**
 * 
 * @param password
 * The password
 */
public void setPassword(String password) {
   this.password = password;
}

}
Yasin Kaçmaz
  • 6,573
  • 5
  • 40
  • 58
  • Thanks for ur reply. I did this but i'm getting error ** Unable to create @ Body converter for class LoginRequest(my pojo class)**. I remove first line @ FormUrlEncoded bcoz i was getting error **@ Body parameters cannot be used with form or multi-part encoding.** can you pls tell me how to pass the login(json) data? – AMAN SINGH May 23 '16 at 15:39
  • please check it, I posted my LoginResponse class above and my LoginRequest class is same as you told **pojo class with annotation nonce** Call getHome1(@Body LoginRequest pojo); – AMAN SINGH May 23 '16 at 15:51
  • yeah i'm using **GsonConvertorFactory** and change annotation but same error is thr **Unable to create @Body converter for class LoginRequest... – AMAN SINGH May 23 '16 at 16:00
  • and i sent data like LoginRequest loginRequest = new LoginRequest(); loginRequest.setUserName("abc"); loginRequest.setPassword("123"); – AMAN SINGH May 23 '16 at 16:08
  • i've updated my answer by looking my working code. You can add headers etc or delete content type header in my answer. Can you try it. And i will say you again can you post your example json response of login process – Yasin Kaçmaz May 23 '16 at 16:20
  • hop it will work... but can yu pls explain about **.authenticator(tauth)** what is **tauth** here. whr is its definition – AMAN SINGH May 24 '16 at 07:53
  • its just authanticator with oauth2 you can disable it. – Yasin Kaçmaz May 24 '16 at 12:41
  • No.. still not working **Unable to create @Body converter for class LoginRequest** where **LoginRequest** is d pojo class which i've to post – AMAN SINGH May 24 '16 at 13:09
  • please tell me whr the problem is.. whr is my mistake.. pls – AMAN SINGH May 25 '16 at 07:09
  • i have updated my answer by looking my working code. Can you try like in the answer – Yasin Kaçmaz May 25 '16 at 09:26
  • I don't know what d problem wid me still i'm same error **End of input at line 1 column 1** I setup everything as yu said but still at same position. what to do now? – AMAN SINGH May 25 '16 at 11:31
  • Can you make an example request in Postman and add screenshot here. And i will inspect it. Where you are getting error. – Yasin Kaçmaz May 25 '16 at 11:32
  • did you tried "@Body HashMap body " or "@Body Jsonobject obj" in api service . If you dont tried , tell me i will update my answer – Yasin Kaçmaz May 25 '16 at 19:21
  • no i didn't tried hashmap.. please update ur answer. really thanks – AMAN SINGH May 25 '16 at 20:03
  • instead of update can you take a look at here : http://stackoverflow.com/questions/21398598/how-to-post-raw-whole-json-in-the-body-of-a-retrofit-request – Yasin Kaçmaz May 25 '16 at 20:09
0

Just reposting an answer which worked for me.

@Override
public void onResponse(Call<Void> call, retrofit2.Response<Void> response) {
        if (response.isSuccessful()) {

        //Do something if response is ok
        } else {

            JsonParser parser = new JsonParser();
            JsonElement mJson = null;
            try {
                mJson = parser.parse(response.errorBody().string());
                Gson gson = new Gson();
                MyError errorResponse = gson.fromJson(mJson, MyError.class);
            } catch (IOException ex) {
                ex.printStackTrace();
            }

        }
Nikhil
  • 911
  • 15
  • 28