简体   繁体   中英

Error in Code for Get & Post Request in Retrofit 2

I have an Api, Api = " http://retailapi.airtechsolutions.pk/api/login/admin@gmail.com/admin/ "

I am new to Retrofit2, If some one can help me through my code

I have an Error while doing GET and POST Request

While Using Get Api: the Username and password are null

While Using Post Api: I am getting 404 Error code

Login Activity:

private JsonPlaceHolderApi jsonPlaceHolderApi;

protected void onCreate(Bundle savedInstanceState)
    {   super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        Retrofit retrofit = new Retrofit.Builder().baseUrl("http://retailapi.airtechsolutions.pk/api/login/admin@gmail.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);

        getLogin();
        createPost();
    }

private void getLogin()
    {
        Call<Login> call = jsonPlaceHolderApi.getLogin();

        call.enqueue(new Callback<Login>()
        {
            @Override
            public void onResponse(Call<Login> call, Response<Login> response)
            {
                if (!response.isSuccessful())
                {
                    Log.i("Code: " , String.valueOf(response.code()));
                    return;
                }

                Login logins = response.body();


                    String content = "";
                    content += "UserName: " + logins.getUsername() + "\n";
                    content += "Password: " + logins.getPassword() + "\n";
                    content += "Status: " + logins.getStatus() + "\n";
                    content += "Description: " + logins.getDescription() + "\n\n";

                    Log.i("Read me", content);
            }

            @Override
            public void onFailure(Call<Login> call, Throwable t)
            {
                Log.i("Error", t.getMessage());
            }

        });
    }

    private void createPost() {
        Login login = new Login("New Name", "New Password");

        Call<Login> call = jsonPlaceHolderApi.createPost(login);

        call.enqueue(new Callback<Login>() {
            @Override
            public void onResponse(Call<Login> call, Response<Login> response) {

                if (!response.isSuccessful())
                {
                    Log.i("Code: " , String.valueOf(response.code()));
                    return;
                }

                Login loginResponse = response.body();

                String content = "";
                content += "Code: " + response.code() + "\n";
                content += "UserName: " + loginResponse.getUsername() + "\n";
                content += "Password: " + loginResponse.getPassword() + "\n";
                content += "Status: " + loginResponse.getStatus() + "\n";
                content += "Description: " + loginResponse.getDescription() + "\n\n";

                Log.i("Read me",content);
            }

            @Override
            public void onFailure(Call<Login> call, Throwable t) {
                Log.i("Failure", t.getMessage());
            }
        });
    }

Login Class:

String username;
    String password;
    int Status;
    String Description;

    public Login(String username, String password)
    {
        this.username = username;
        this.password = password;
    }

    public String getUsername()
    {
        return username;
    }

    public String getPassword()
    {
        return password;
    }

    public int getStatus()
    {
        return Status;
    }

    public String getDescription()
    {
        return Description;
    }

And An Interface Class Called JsonPlaceHolderApi

public interface JsonPlaceHolderApi
{
    @GET("admin")
    Call<Login> getLogin();

    @POST("admin")
    Call<Login> createPost(@Body Login login);
}

Starting with the GET issue, you are getting fields (Username and password) equals to null because they are not at the top level of your JSON, so, in order to fix the GET issue you need to create the following class :

LoginResponse

import com.google.gson.annotations.SerializedName;

public class LoginResponse {
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public int getStatus() {
        return status;
    }

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

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @SerializedName("Users")
    User user;
    @SerializedName("Status") int status;
    @SerializedName("Description") String description;
}

Add a user class which will host all the user's data :

User

public class User {

     public User(String username, String password) {
          this.username = username;
          this.password = password;
     }

     public String getClientno() {
          return clientno;
     }

     public void setClientno(String clientno) {
          this.clientno = clientno;
     }

     public String getCompanyno() {
          return companyno;
     }

     public void setCompanyno(String companyno) {
          this.companyno = companyno;
     }

     public String getUserno() {
          return userno;
     }

     public void setUserno(String userno) {
          this.userno = userno;
     }

     public String getUsername() {
          return username;
     }

     public void setUsername(String username) {
          this.username = username;
     }

     public String getPassword() {
          return password;
     }

     public void setPassword(String password) {
          this.password = password;
     }

     public int getRowno() {
          return rowno;
     }

     public void setRowno(int rowno) {
          this.rowno = rowno;
     }

     String clientno;
     String companyno;
     String userno;
     String username;
     String password;
     int rowno;
}

Then update your JsonPlaceHolderApi interface to return a Call<LoginResponse> instead of Call<Login> :

@GET("admin")
Call<LoginResponse> getLogin();

@POST("admin")
Call<LoginResponse> createPost(@Body User login);

One more thing, replace both getLogin() and createPost() with the following :

private void getLogin() {
    Call<LoginResponse> call = jsonPlaceHolderApi.getLogin();

    call.enqueue(new Callback<LoginResponse>() {
        @Override
        public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
            if (!response.isSuccessful()) {
                http://retailapi.airtechsolutions.pk/api/login/admin@gmail.com/

                Log.i("Code: ", String.valueOf(response.code()));
                return;
            }

            LoginResponse loginResponse = response.body();


            String content = "";
            content += "UserName: " + loginResponse.user.getUsername() + "\n";
            content += "Password: " + loginResponse.user.getPassword() + "\n";
            content += "Status: " + loginResponse.getStatus() + "\n";
            content += "Description: " + loginResponse.getDescription() + "\n\n";

            Log.i("Read me", content);
        }

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

    });
}

private void createPost() {
    User login = new User("New Name", "New Password");

    Call<LoginResponse> call = jsonPlaceHolderApi.createPost(login);

    call.enqueue(new Callback<LoginResponse>() {
        @Override
        public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {

            if (!response.isSuccessful()) {
                Log.i("Code: ", String.valueOf(response.code()));
                return;
            }

            LoginResponse loginResponse = response.body();

            String content = "";
            content += "Code: " + response.code() + "\n";
            content += "UserName: " + loginResponse.user.getUsername() + "\n";
            content += "Password: " + loginResponse.user.getPassword() + "\n";
            content += "Status: " + loginResponse.getStatus() + "\n";
            content += "Description: " + loginResponse.getDescription() + "\n\n";

            Log.i("Read me", content);
        }

        @Override
        public void onFailure(Call<LoginResponse> call, Throwable t) {
            Log.i("Failure", t.getMessage());
        }
    });
}

For the POST issue, 405 (Method Not Allowed) , it seems like the "admin" action doesn't support the POST method.

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