Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting simple JSON object response using Retrofit library

I have a web query with JSON response as:

{
    "status":true,
    "result":
      {
        "id":"1",
        "name":"ABC 1",
        "email":"[email protected]",
        "password":"123456",
        "status":false,
        "created":"0000-00-00 00:00:00"
      },
    "message":"Login successfully"
}

I am using the following code for:

@GET("/stockers/login")
public void login(
        @Query("email") String email,
        @Query("password") String password,
        Callback<JSONObject> callback);

In Debugger the query made by the Retrofit library is correct, however I get an empty JSON in response.

ApiManager.getInstance().mUrlManager.login(
        email.getText().toString().trim(),
        password.getText().toString().trim(),
        new Callback<JSONObject>()
        {
            @Override
            public void success(JSONObject jsonObj, Response response)
            {
                mDialog.dismiss();
like image 362
Muhammad Umar Avatar asked Jun 29 '15 09:06

Muhammad Umar


People also ask

How do you post raw whole JSON in the body of a retrofit request?

The Best Answer is The @Body annotation defines a single request body. Since Retrofit uses Gson by default, the FooRequest instances will be serialized as JSON as the sole body of the request. The Gson docs have much more on how object serialization works. And then use an instance of that class similar to #1.


1 Answers

Simply use JsonElement insted of JSONobject. Like:

@GET("/stockers/login")
Call<JsonElement> getLogin(
    @Query("email") String email,
    @Query("password") String password
);
like image 159
Shubham Vala Avatar answered Oct 01 '22 03:10

Shubham Vala