I am using a API for login
http://abc.demoserver.com/API/login.php?username=johndoe&password=123456
this API is in GET method
and also using retrofit2 so my code snippet is here.
APIUtils.java
public static final String BASE_URL = "http://abc.demoserver.com/API/";
public static APIService getAPIService() {
return RetrofitClient.getClient(BASE_URL).create(APIService.class);
}
APIServiceInterface.java
@GET("login.php")
Call<JsonObject> studentLogin(@Query("username") String username,
@Query("password") String password);
and my RetrofitClient.java is
public class RetrofitClient {
private static Retrofit retrofit = null;
public static Retrofit getClient(String baseUrl) {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
This gives me response null but when I hit this URL in browser it gives the response
in below format
{
"details": [
{
"ID": "56895",
"user_login": "johndoe",
"user_pass": "$P$BPjOjHab\iHVP0/YPOIly.b1",
"user_email": "[email protected]",
"user_registered": "2017-02-10 07:28:39",
"user_status": "0",
"display_name": "John Doe",
"instructor": "23",
"token": "212cde4b85163cbd05962ef",
"profile_img": "http://abc.demoserver.com/wp-content\/uploads\/avatars\/16895\/165113b6b900b4cf145b84c31.jpg"
}
],
"status_code": 1,
"status_description": "Login successful."
}
UPDATED
Call back
public void loginData(String username, String password) {
final ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Please wait...");
dialog.setCancelable(false);
dialog.show();
apiService.studentLogin(username, password).enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
dialog.dismiss();
try {
if (response != null) {
JSONObject josn = new JSONObject(response.body().toString());
JSONArray jarray = josn.getJSONArray("details");
for (int i = 0; i < jarray.length(); i++) {
JSONObject jsonObject = jarray.getJSONObject(0);
user_id = jsonObject.getString("ID");
user_name = jsonObject.getString("user_login");
user_mail = jsonObject.getString("user_email");
user_display_name = jsonObject.getString("display_name");
user_registered = jsonObject.getString("user_registered");
user_status = jsonObject.getString("user_status");
token = jsonObject.getString("token");
instructor = jsonObject.getString("instructor");
user_pass = jsonObject.getString("user_pass");
profile_img = jsonObject.getString("profile_img");
}
}
} catch (Exception e) {
e.printStackTrace();
Log.e("Exception", e.getMessage());
}
}
@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
Log.e("onFailure", "Unable to get API." + t.getMessage());
dialog.dismiss();
}
});
}
how to call a GET method API in retrofit2
first of all copy the response of your API and create your bean classes through
this site * By clicking source type to JSON * Annotation Style to GSON * Unticking useDoubleNumbers * Unticking AllowAdition Property
then copy those files to your project
then in your activity