I'm working with retrofit and I want to know which is the generated url.
I've tried with setRequestInterceptor and setProfiler.
The last one gives me the url but not query params... so It's not the complete one.
Thanks
This works for me:
service.yourRequest(params...).enqueue(new Callback<ResponseBody>() {
#Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Log.d(TAG, call.request().url().toString()); // here
}
#Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
For those who want to know
RestAdapter restAdapter = new RestAdapter.Builder()
.setLogLevel(RestAdapter.LogLevel.BASIC)
.setEndpoint(Metadata.CURRENT_SERVER)
.setRequestInterceptor(requestInterceptor)
.build();
call.request().url(), where call is an instance of retrofit2.Call
Related
I'm using Retrofit on my app to download a video file from the server, in the request i need to do a Post request,
on the interface I have added the parameters needed it....and on the java function I'm passing the parameters too, but
when I try to run the code i get an error:
java.lang.RuntimeException: An error occurred while executing
doInBackground()
#Headers("Content-Type: application/json; charset=UTF-8")
#Streaming
#POST
Call<ResponseBody> downloadFileStream(#Url String url, #QueryMap Map<String, Object> postdata);
private void downloadFile(String url) {
FileDownloadClient fileDownloadClient = retrofit.create(FileDownloadClient.class);
Call<ResponseBody> call = fileDownloadClient.downloadFileStream(url,postdata);
postdata.put("user", "test#test.com");
postdata.put("test", "test");
Call<ResponseBody> call = fileDownloadClient.downloadFileStream(url, postdata);
call.enqueue(new Callback<ResponseBody>() {
#Override
public void onResponse(Call<ResponseBody> call, final Response<ResponseBody> response) {
new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... voids) {
boolean success = writeResponseBodyToDisk(response.body());
return null;
}
}.execute();
}
#Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(MainActivity.this, "Mal", Toast.LENGTH_LONG).show();
}
});
}
I was having the same problm, try this...this worked for me
Your interface:
public interface FileDownloadClient {
#Streaming
#POST("yourAPI")
Call<ResponseBody> downloadFileStream(#Body Map<String, Object> postdata);
}
Change this on your downloadFile:
private void downloadFile() {
Retrofit.Builder builder = new Retrofit.Builder().baseUrl("yourwebsite/api/")
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
FileDownloadClient fileDownloadClient = retrofit.create(FileDownloadClient.class);
Map<String, Object> postdata = new HashMap<>();
postdata.put("user", "test#test.com");
postdata.put("test", "test");
Call<ResponseBody> call = fileDownloadClient.downloadFileStream(postdata);
}
Grandle:
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.google.code.gson:gson:2.6.1'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.squareup.retrofit2:converter-scalars:2.5.0'
All the thing working fine but how to call the the user{first_name,last_name,email}
You have to call a retrofit method in this way,
#FormUrlEncoded
#POST("employer/login")
Call<ResponseModelEmplyrLogin> loginEmployer(
#Field("data") String data);
public void loginEmployer(String data, final MyApiCallbackEmplyrLogin<EmployrDataBean> callback) {
ApiInterface apiService = ApiClient.createService();
Call<ResponseModelEmplyrLogin> call = apiService.loginEmployer(data);
call.enqueue(new Callback<ResponseModelEmplyrLogin>() {
#Override
public void onResponse(Call<ResponseModelEmplyrLogin> call, Response<ResponseModelEmplyrLogin> response) {
//Write your code
}
#Override
public void onFailure(Call<ResponseModelEmplyrLogin> call, Throwable t) {
//Write your code
}
});
}
Check out this link https://www.androidtutorialpoint.com/networking/retrofit-android-tutorial/ ...I think this will be helpful
i'm having issues getting Retrofit 2.0 to send POST requests to Python-Django.
Here's my Retrofit method.
public void sendNetworkRequest(User user) {
//Cria instância retrofit
final Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://127.0.0.1:8000/")
.addConverterFactory(GsonConverterFactory.create())
.build();
UserService services = retrofit.create(UserService.class);
Call<User> call = services.createAccount(user);
call.enqueue(new Callback<User>() {
#Override
public void onResponse(Call<User> call, Response<User> response) {
Toast.makeText(CadastrarActivity.this, "Você foi cadastrado com sucesso!", Toast.LENGTH_SHORT).show();
}
#Override
public void onFailure(Call<User> call, Throwable t) {
CheckConnection checkConnection = new CheckConnection();
if (checkConnection.equals(null)){
Toast.makeText(CadastrarActivity.this, "Conecte-se a internet!", Toast.LENGTH_SHORT).show();
}
else {
Log.e("heurys", t.getMessage());
System.out.println(t.getStackTrace());
Toast.makeText(CadastrarActivity.this, "Algo deu errado!", Toast.LENGTH_SHORT).show();
}
}
});
}
Here's my interface method used in the Rest call:
public interface UserService {
#POST("rest/cadastro")
Call<User> createAccount(#Body User user);
}
And here's my traceback error:
04-03 12:58:43.726 18692-18692/com.example.ccyrobuosi.estudos E/heurys: Failed to connect to /127.0.0.1:8000
In advance, my Python code works just fine, i used Postman to test it, and its getting the requests properly.
I am trying to use okhttp (3.4.1) to implement a HTTP 2.0 based client. Part of my requirement is to implement multiple asynchronous HTTP requests to different URLs with a callback to handle the response at a later time.
However, with my current implementation, I see that I cannot get all my asynchronous requests to use the same TCP connection unless I make a blocking HTTP request from my main thread at the beginning.
I understand that the enque() method used for asynchronous calls engages the dispatcher which seems to spawn a new thread for each of the requests.
Here is my code snippet:
OkHttpClient client = new OkHttpClient();
My async Get Request method looks as follows:
public void AsyncGet(String url) throws Exception {
Request request = new Request.Builder().url(url).build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
#Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
#Override
public void onResponse(Call call, Response response) throws IOException {
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
}
new Thread() {
#Override
public void run() {
/* Some code */
}
}.start();
}
});
}
My synchronous Get Request is as follows:
public Response SyncGet(String url) throws Exception {
Request request = new Request.Builder().url(url).build();
Call call = client.newCall(request);
Response response = call.execute();
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
}
return response;
}
Making a call sequence like the following triggers 2 TCP connections to the server.
AsyncGet(Url);
AsyncGet(Url2);
However, a call sequence like the following makes use of the same TCP connection.
SyncGet(Url);
AsyncGet(Url);
AsyncGet(Url2);
I have not debugged this but, it looks like OkHttp forces us to make a blocking HTTP request on the main thread first to possibly obtain the TCP connection context and then share that with other threads? Or, am I missing something?
You can call async to and set sector to each call later on based on the sector you can distinguish the response of call. Try this code; I hope it will help you!
Create a separate class for api call:
public class RestApi {
protected RestApiResponse serverResponse;
protected Context c;
public RestApi(RestApiResponse serverResponse, Context c) {
this.serverResponse = serverResponse;
this.c = c;
}
public void postDataWithoutAuth(String url,String sector) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://publicobject.com/helloworld.txt")
.build();
client.newCall(request).enqueue(new Callback() {
#Override
public void onFailure(Call call, IOException e) {
Log.e("response", call.request().body().toString());
serverResponse.onErrorLoaded(call.request().body().toString(), sector);
}
#Override
public void onResponse(Call call, Response response) throws IOException {
serverResponse.onResponseLoaded(response, sector);
}
});
}
}
Then create an interface for callback
public interface RestApiResponse {
public void onResponseLoaded(Response response, String sector);
public void onErrorLoaded(String response, String sector);
}
and access it from your Activity like this
RestApi apicall = new RestApi(this, getBaseContext());
apicall.postDataWithoutAuthUrlEncoded("ur url","your sector");
I am building a Login system for an Android app. I am using OkHttp to connect to my server and get a JSON response.
I have defined a class with the login return data (right now just a true/false response based on whether the user exists in the database), and then written the code to connect to the server, as shown below:
class UserLogin {
boolean status;
public void setStatus(boolean status) {
this.status = status;
}
public boolean getStatus() {
return status;
}
}
public class ClientServerInterface {
OkHttpClient client = new OkHttpClient();
boolean login(Request request) {
final Gson gson = new Gson();
client.newCall(request).enqueue(new Callback() {
UserLogin login;
#Override
public void onFailure(Call call, IOException e) {
}
#Override
public void onResponse(Call call, Response response) throws IOException {
login = gson.fromJson(response.body().charStream(), UserLogin.class);
login.setStatus(login.status);
}
});
// need to return the boolean response (status) here
}
}
The code which passes the Request variable to the login method works perfectly. I want login to return a boolean response so that I can pass that to other methods in other classes.
However, because the UserLogin object is defined in the callback I can't access it in the parent method. I have made a getStatus method but not sure how to use it properly to get the status in the main login method.
The code which passes the Request variable to the login method works
perfectly. I want login to return a boolean response so that I can
pass that to other methods in other classes.
you can't. enqueue executes the code in Async way. You don't know when the callback is invoked. What you could do is to add the Callback as parameter to your login method. E.g.
boolean login(Request request, final Callback callback) {
and either pass it to enqueue,
client.newCall(request).enqueue(callback);
or call the callback manually. E.g.
#Override
public void onResponse(Call call, Response response) throws IOException {
if (callback != null) {
callback.onResponse(call, response);
}
}
in both cases the caller of login will receive the callback on the provided object and, accordingly to the content it receives, can decide wha actions undertake
You can do this using a SynchronousQueue:
final SynchronousQueue<Boolean> queue = new SynchronousQueue<>();
client.newCall(request).enqueue(new Callback() {
UserLogin login;
#Override
public void onFailure(Call call, IOException e) {
queue.put(false);
}
#Override
public void onResponse(Call call, Response response) throws IOException {
queue.put(true);
}
});
return queue.take();
Add loginStatus variable to class like below and one more to indicate login operation completion.
public class ClientServerInterface {
OkHttpClient client = new OkHttpClient();
private boolean loginStatus = false;
private boolean isLoginOperationDone = false;
boolean login(Request request) {
final Gson gson = new Gson();
client.newCall(request).enqueue(new Callback() {
UserLogin login;
#Override
public void onFailure(Call call, IOException e) {
}
#Override
public void onResponse(Call call, Response response) throws IOException {
login = gson.fromJson(response.body().charStream(), UserLogin.class);
loginStatus = login.setStatus(login.status);
isLoginOperationDone = true;
}
});
// need to return the boolean response (status) here
while( !isLoginOperationDone )
{
//not to do anything.
}
return loginStatus;
}
}
Note that this might be a little hacky but will do solve your problem.
The way to go is with AsyncTask. Override the doInBackground method to perform the http requests and get the result by overriding the onPostExecute method.
Read more here: http://developer.android.com/reference/android/os/AsyncTask.html
An even better way to go is to run a background Service for all your API calls.