【问题标题】:Retrofit: passing multiple GET parameter改造:传递多个 GET 参数
【发布时间】:2018-04-25 12:12:07
【问题描述】:

我正在使用改造来进行 API 调用,以验证来自 android 应用程序中服务器的登录凭据。

例如,这是我的网址:http://example.com/park/app/login.php?username=xyz&password=1234&user_type=1&device_id=12345

我创建了 Utils 类,其中包含 URl 和常量。

public class Utils {
public static final String user_type = "1";
public static final String device_id = "12345";
private static final String LOGIN_URL = "http://example.com/";

public static  ApiServices getServices() {
    return RetroFitClient.getClient(LOGIN_URL).create(ApiServices.class);
}

public class RetroFitClient {
private static Retrofit retroFit = null;

public static Retrofit getClient(String url) {
    return (retroFit == null) ? (new Retrofit.Builder().baseUrl(url)
                    .addConverterFactory(GsonConverterFactory
                            .create()).build()) : retroFit;
}

}

这是界面:

public interface ApiServices {
@GET("park/app/login.php?")
Call<Result> loginUser
        (@Query("username") String username,
         @Query("password") String passwoord,
         @Query("user_type") String user_type,
         @Query("device_id") String device_id);

}

当我调用服务时

 private void doLogin(String name, String passwoord, String user_type, String deviceid) {
    Call<Result> call = apiServices.loginUser(name, passwoord, user_type, deviceid);
    call.enqueue(new Callback<Result>() {
        @Override
        public void onResponse(Call<Result> call, Response<Result> response) {
            Log.e("OnResponse", response.body().getMessage()+" status "+response.body().isStatus());
            if (response.body().isStatus()) {
                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intent);
            } else {
                Toast.makeText(LoginActivity.this, "UserName or Password is incorrect", Toast.LENGTH_SHORT).show();
            }
        }

我收到的状态为假

04-25 17:13:20.461 31947-32086/com.example.innobles.velparked D/dalvikvm: VFY: replacing opcode 0x71 at 0x000a
04-25 17:13:20.751 31947-31947/com.example.innobles.velparked E/OnResponse: Invalid User. status false

我哪里做错了? 有什么想法吗?

【问题讨论】:

  • 看起来你做的都是正确的。您输入的数据或服务器可能有问题。
  • @VladyslavMatviienko 我正在输入正确的数据。
  • 并且代码看起来正确。你确定如果你从浏览器调用 URL,它会起作用吗?
  • @VladyslavMatviienko 是的,当我从浏览器调用 URL 时,它工作正常。
  • @Arshad 无法将我的问题与您的解决方案联系起来

标签: android parameters get retrofit2


【解决方案1】:

编辑以下行:

@GET("park/app/login.php?")

收件人:

@GET("park/app/login")

还要检查您是否提供了有效的凭据。 此外,尝试将 user_type 作为 int 传递,如下所示:

@GET("park/app/login.php")
Call<Result> loginUser
    (@Query("username") String username,
     @Query("password") String passwoord,
     @Query("user_type") int user_type,
     @Query("device_id") String device_id);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-16
    • 2013-11-27
    • 2019-02-02
    相关资源
    最近更新 更多