Reputation: 1
I am using ngrok to put my local php project into the web. I need to get an id of a user in a login action. My Android gives me
Response{protocol=http/1.1, code=404, message=Not Found, url=http://ab873755.ngrok.io/site/login?login=123&pass=123456}
Can't get a responce in retrofit Android Studio. Url is correct. Checked it in Postman
When I put this url into Postman it's all fine and gives the right response. This page returns
{"user_id":1}
package com.example.backgroundservice.ui.login;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class WResponce {
@SerializedName("user_id")
@Expose
private String user_id;
public String getUserId() {
return user_id;
}
public void setUserId(String user_id) {
this.user_id = user_id;
}
}
package com.example.backgroundservice.ui.login;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
public interface JSONPlaceHolderApi {
@GET("site/login")
public Call<WResponce> getResponce(
@Query("login") String login,
@Query("pass") String password
);
}
package com.example.backgroundservice.ui.login;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class NetworkClient {
public static final String BASE_URL = "http://ab873755.ngrok.io/";
public static Retrofit retrofit;
public static Retrofit getRetrofitClient(){
if (retrofit == null){
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
package com.example.backgroundservice.ui.login;
import android.net.http.HttpResponseCache;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.example.backgroundservice.R;
import com.example.backgroundservice.ReceiveData;
import java.io.IOException;
public class LoginActivity extends AppCompatActivity {
final Handler mHandler = new Handler();
private Thread mUiThread;
private JSONPlaceHolderApi jsonPlaceHolderApi;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
public void login(View view) throws IOException, InterruptedException{
final EditText username = findViewById(R.id.username);
final EditText password = findViewById(R.id.password);
Retrofit retrofit = NetworkClient.getRetrofitClient();
jsonPlaceHolderApi = retrofit.create(JSONPlaceHolderApi.class);
Call call = jsonPlaceHolderApi.getResponce(username.getText().toString(),password.getText().toString());
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
if(response.isSuccessful()){
WResponce res = (WResponce) response.body();
String a = ((WResponce) response.body()).getUserId();
Log.d("myres",a);
Log.d("myres",response.body().toString());
}
Log.d("mymsg",response.message());
Log.d("mymsg",response.toString());
}
@Override
public void onFailure(Call call, Throwable t) {
}
});
}
}
Upvotes: 0
Views: 723
Reputation: 819
Put base url = http://ab873755.ngrok.io
. You are not supposed to put the path in base url. Put the path in @GET("sites/login").
Make sure your ngrok url is active one. Seems like it was expired
Upvotes: 2
Reputation: 11457
The URL
you are using is not working in POSTMAN
.
Things to Note
1) Whether you are passing anything in Authorization or not.
2) Whether you are sending anything in Headers
Incorrect Response in Postman (Screenshot)
Upvotes: 0