GET request with parameters using Retrofit

问题: I am learning Retrofit. I've built an API using PHP Slim to check whether an email given by user exist or not in my database. My API is okay. I've tested it via Postman. Bu...

问题:

I am learning Retrofit. I've built an API using PHP Slim to check whether an email given by user exist or not in my database. My API is okay. I've tested it via Postman. But when I'm calling my API it's not giving correct result.

Api.java

package com.example.royta.retrofitdemo.APIs;


import com.example.royta.retrofitdemo.ModelClass.UserExist;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import retrofit2.http.Path;
import retrofit2.http.Query;

public interface Api {

    @GET("finduser")
    Call<UserExist> isUserExist(
            @Query("email") String email
    );

}

Please check the Screenshot for JSON response.

UserExist.java

package com.example.royta.retrofitdemo.ModelClass;

import com.google.gson.annotations.SerializedName;

public class UserExist {
    @SerializedName("email")
    private boolean email;

    public UserExist(boolean email) {
        this.email = email;
    }

    public boolean isEmailExist() {
        return email;
    }
}

RetrofitClient.java

package com.example.royta.retrofitdemo.APIs;

import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitClient {
    private static final String BASE_URL = "http://24d00bb3.ngrok.io/MyApi/public/";
    private static RetrofitClient retrofitClient;
    private Retrofit retrofit;

    private RetrofitClient() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public static synchronized RetrofitClient getInstance() {
        if(retrofitClient == null) {
            retrofitClient = new RetrofitClient();
        }
        return retrofitClient;
    }

    public Api getApi() {
        return retrofit.create(Api.class);
    }
}

Here is the call

FindAccount.java

Call <UserExist> call = RetrofitClient.getInstance().getApi().isUserExist("troy@yahoo.com");

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

                if(response.body().isEmailExist()) {
                    //send code in email;
                    Toast.makeText(FindAccount.this, "Send code in email", Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(FindAccount.this, "You are not registered", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<UserExist> call, Throwable t) {
                Log.d("ROYs", "onFailure block");
            }
        });

the email "troy@yahoo.com" is exist in my database.But the following call giving me false result. It should be true.

response.body().isEmailExist()

Screenshot is the proof of my API. Please Help. Thanks in advance. enter image description here


回答1:

You send it as application/x-www-form-urlencoded and not as GET params. Change the request to:

@FormUrlEncoded
@GET("finduser")
Call<UserExist> isUserExist(@Field("email") String email);

回答2:

Finally I found my problem. Actually problem was in my API. I used getParsedBody() method in my API to parse param value. But it only parse from body.In Postman, I am sending request from body. For this reason It was working fine in Postman.

Instead of getParsedBody() I used getQueryParams() method, and now its giving me the correct result.

  • 发表于 2019-01-15 00:28
  • 阅读 ( 214 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除