Reputation: 31
I'm a beginner in android studio. I have a problem how can i send token id to a remote server with GET method request to get information, because i must be authenticated to get those information. Below the GET method and the error msg, help please ! 1error msg GET method
Upvotes: 0
Views: 655
Reputation: 31
I figure it out , it was this one :
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
headers.put("x-access-token", TokenHandler.getToken());
return headers;
}
Upvotes: 0
Reputation: 1036
You can try with OkHttp3. Add this to your GRADLE:
compile 'com.squareup.okhttp3:okhttp:3.10.0'
Create a class and extends it from AsyncTask
Then call it from onCreate
method
new DataToServer().execute();
DataToServer
class implementation
private class DataToServer extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... strings) {
GetDataFromUrl getData = new GetDataFromUrl();
String response = null;
try {
response = getData.run(URL_of_your_server);
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//you got the server response on the result variable
}
}
And this is the implementation of OkHttp
private class GetDataFromUrl {
OkHttpClient client = new OkHttpClient();
String run(String url) throws IOException {
RequestBody formBody = new FormBody.Builder()
.build();
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
Response response = null;
try {
response = client.newCall(request).execute();
return response.body().string();
} finally {
if (response != null) {
response.close();
}
}
}
}
Upvotes: 1
Reputation: 122
You can use the support of Retrofit Library
Firstly, in your gradle, add this line to download.
compile 'com.squareup.retrofit2:retrofit:2.4.0'
Secondly, Select the type of RequestBody in @Body
Gson: com.squareup.retrofit2:converter-gson
Jackson: com.squareup.retrofit2:converter-jackson
Moshi: com.squareup.retrofit2:converter-moshi
Protobuf: com.squareup.retrofit2:converter-protobuf
Wire: com.squareup.retrofit2:converter-wire
Simple XML: com.squareup.retrofit2:converter-simplexml
Scalars (primitives, boxed, and String): com.squareup.retrofit2:converter-scalars
then add it in your gradle:
compile 'com.squareup.retrofit2:converter-gson:2.4.0'
Thirdly, Create a interface class to use @GET method or others, for example:
public interface CallMethodService {
@GET("search")
Call<ResponseModel> getResponseModel();
}
Lastly, In your MainActivity:
create a Retrofit
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("base server link")
.addConverterFactory(RequestBody type)
.build();
for example:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://api.giphy.com/v1/gifs/")
.addConverterFactory(GsonConverterFactory.create())
.build();
then call to get data:
retrofit.create(CallMethodService.class).getResponseModel()
.enqueue(new Callback<ResponseModel>() {
@Override
public void onResponse(Call<ResponseModel> call, Response<ResponseModel> response) {
//get data
}
@Override
public void onFailure(Call<GifResponse> call, Throwable t) {
// No internet
}
});
Upvotes: 1