【问题标题】:how to get json response using retrofit如何使用改造获得 json 响应
【发布时间】:2019-06-21 15:18:54
【问题描述】:

{"status":"0","message":"登录失败:(此帐户不存在或电子邮件尚未验证!请尝试询问管理员激活然后登录;)" }

这是我的网址

http://zeenatkhanniazai.com/services/login.php

登录类和接口

public static String BASE_URL="http://zeenatkhanniazai.com/services/";
    public static loginServices loginn=null;

    public static loginServices Login(){
        if (loginn == null ){
            Retrofit retrofit=new Retrofit.Builder().baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

            loginn = retrofit.create(loginServices.class);
        }


        return loginn;
    }





    public interface loginServices{
        @POST("login.php/{uemail}/{upassword}")
        retrofit2.Call<User> login(@Path("uemail") String uemail, @Path("upassword") String upassword);
    }

MainActivity

String femail=email.getText().toString();
                String fpasssword=password.getText().toString();

                Call<User> call=LoginApi.Login().login(femail,fpasssword);
                call.enqueue(new Callback<User>() {
                    @Override
                    public void onResponse(Call<User> call, Response<User> response) {
                        User users=  response.body();

                        users.getMessege();


                        Toast.makeText(MainActivity.this, "respoces"+new Gson().toJson(response.body()), Toast.LENGTH_SHORT).show();


                    }

                    @Override
                    public void onFailure(Call<User> call, Throwable t) {
                        Toast.makeText(MainActivity.this, "f", Toast.LENGTH_SHORT).show(); }

这是我的用户类

@SerializedName("message")
    private String message;
    @SerializedName("status")
    private String status;

    public String getStatus() {
        return status;
    }

    public String message() {
        return message;
    }

【问题讨论】:

  • 您需要在@SerializedName 中使用message 而不是messege
  • 你能给我一个你请求 URL 的例子吗?首先确定它是否正确。我看到的是http://zeenatkhanniazai.com/services/login.php/youremail/yourpassword;对吗?
  • 它不工作
  • 是的,这是正确的@MuratGuc
  • testcase :尝试 @Field 而不是 @path 。也可以试试@POST("login.php")

标签: android web-services retrofit2


【解决方案1】:

试试这个:

API 客户端:

public class ApiClient {


    public static final String BASE_URL = "http://zeenatkhanniazai.com/services/";
    private static Retrofit retrofit = null;


    public static Retrofit getClient() {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

API接口:

public interface ApiInterface {

    @POST("login.php")
    @FormUrlEncoded
    Call<users> getTopRatedMovies(@Field("uemail") String uemail, @Field("upassword") String upassword);

}

用户类别:

public class users
{
    @SerializedName("message")
    private String message;
    @SerializedName("status")
    private String status;

    public String getStatus() {
        return status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

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

在主要活动中:

call.enqueue(new Callback<users>() {
            @Override
            public void onResponse(Call<users> call, Response<users> response) {
                progressDoalog.dismiss();
                int statusCode = response.code();
                String movies = response.body().getMessage();

                Log.e("sdasd",movies);
                //Log.w("response",new Gson().toJson(response));
                Log.w("response",new GsonBuilder().setPrettyPrinting().create().toJson(response));
              //  recyclerView.setAdapter(new MoviesAdapter(movies, R.layout.list_item_movie, getApplicationContext()));
            }

            @Override
            public void onFailure(Call<users> call, Throwable t) {
                progressDoalog.dismiss();
                // Log error here since request failed
                Log.e(TAG, t.toString());
            }
        });

输出:

Login unsuccessful :( This account doesn't exist or the email is not verified yet! try asking admin for activation and then logging in ;)

回应:

"body": {
      "message": "Login unsuccessful :( This account doesn\u0027t exist or the email is not verified yet! try asking admin for activation and then logging in ;)",
      "status": "0"
        },

【讨论】:

    猜你喜欢
    • 2019-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多