使用改进的第三方库登录

时间:2018-01-29 10:48:38

标签: android

http://localhost/shop/api2/login.php?email=xxxx@gmail.com&password=xxxxx

{ “成功”:1, “CUSTOMER_ID”: “2”, “customer_group_id”: “0”, “电子邮件”: “tanaji@gmail.com”}

 private void attemptLogin(final String email, final String password) {

         Retrofit retrofit = new Retrofit.Builder()
                 .addConverterFactory(GsonConverterFactory.create())
                 .baseUrl("http://192.168.1.14/shop/api2/")
                 .build();
         LoginApi service = retrofit.create(LoginApi.class);
            User user = new User();
            user.setEmail(email);
            user.setPassword(password);

         Call<ResponseBody> userCall = service.login(email,password);
         userCall.enqueue(new Callback<ResponseBody>() {
             @Override
             public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

                 ResponseBody resp = response.body();
                if (resp != null && resp.equals(1)) {

                     Toast.makeText(MainActivity.this, "Response"+response.body(), Toast.LENGTH_SHORT).show();

                     Intent in = new Intent(MainActivity.this, ProfileActivity.class);
                     startActivity(in);
                 }

             }
             @Override
             public void onFailure(Call<ResponseBody> call, Throwable t) {

                 Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_SHORT).show();
             }
         });


                if (response.isSuccessful() && response.body() != null && response.body().getStatus().equalsIgnoreCase("success")) {

.getStatus在我的代码中不起作用。

如果成功:1则需要登录,然后打开新页面。

如何成功获得json resopnse?

1 个答案:

答案 0 :(得分:0)

制作ResponseBody模型

<div role="group" aria-disabled="false" aria-labelledby="fieldset_title">
  <div id="fieldset_title">Title</div>
  <div>element1</div>
  <div>element2</div>
</div>

致电Retrofit

public class ResponseBody
{
private String customer_group_id;

private String email;

private String success;

private String customer_id;

public String getCustomer_group_id ()
{
    return customer_group_id;
}

public void setCustomer_group_id (String customer_group_id)
{
    this.customer_group_id = customer_group_id;
}

public String getEmail ()
{
    return email;
}

public void setEmail (String email)
{
    this.email = email;
}

public String getSuccess ()
{
    return success;
}

public void setSuccess (String success)
{
    this.success = success;
}

public String getCustomer_id ()
{
    return customer_id;
}

public void setCustomer_id (String customer_id)
{
    this.customer_id = customer_id;
}

@Override
public String toString()
{
    return "ClassPojo [customer_group_id = "+customer_group_id+", email = 
"+email+", success = "+success+", customer_id = "+customer_id+"]";
}
}