0

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

Techno Cracker
  • 453
  • 8
  • 26
loucha
  • 31
  • 1
  • 5
  • First you need to get token from server. That usually goes by user login/sign up when token is generated. – Yupi May 16 '18 at 00:25
  • okay, and after that how i implemented ? – loucha May 16 '18 at 00:30
  • You need to provide token inside `header` I think there are some examples how to do that. Like: https://stackoverflow.com/questions/44000212/how-to-send-authorization-header-in-android-using-volley-library Where ACESS_TOKEN is your token from server. – Yupi May 16 '18 at 00:34
  • so i provide token inside header and put it inside GET method like the example ? – loucha May 16 '18 at 00:43
  • Did you try okHttp3? it's an awesome library – Sudarshan May 16 '18 at 03:03

3 Answers3

1

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
                }
            });
tr4n
  • 122
  • 8
  • i got this msg "Error:Failed to resolve: com.squareup.retrofit2:converter-gson" any help ? – loucha May 16 '18 at 01:19
  • ok, you add the version after com.squareup.retrofit2:converter-gson:2.4.0 – tr4n May 16 '18 at 01:47
  • read more : https://stackoverflow.com/questions/25381710/how-to-call-simple-get-method-using-retrofit?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – tr4n May 16 '18 at 01:50
1

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();
                }
            }
        }


    }
Sudarshan
  • 938
  • 14
  • 27
0

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;
}
loucha
  • 31
  • 1
  • 5