-2

I have a backend app in Django for which we need to send some data by the frontend. Using cURL with php we do it in the following way:

<!DOCTYPE html>
<html>
 
<body>
    <?php
        $ch = curl_init("http://localhost:8000/auth/convert-token");
        $s = 'some string';
        curl_setopt($ch,CURLOPT_HTTPHEADER,array('Authorization: Bearer facebook '."$s"));
        curl_setopt($ch,CURL_RETURNTRANSFER, False);

        curl_exec($ch);
        curl_close($ch);
    ?>
</body>

</html>

where $s is a variable. I want to know what can be the equivalent code in android to implement the same thing.

Apurva Jha
  • 25
  • 1
  • 6

1 Answers1

1

You will need a tool like Retrofit for the job. So have a MyService interface as such:

public interface MyService {    
    @Headers("Authorization: Bearer facebook")
    @GET("/auth/convert-token")
    SomeResponseObject auth();
}

And you'd build it and call it:

RestAdapter restAdapter = new RestAdapter.Builder()
            .setEndpoint("http://localhost:8000")
            .build();

MyService service = restAdapter.create(MyService.class);

SomeResponseObject response = service.auth();

Edit: To add a string variable, you'll have to use RequestInterceptor as explained here: Android Retrofit Parameterized @Headers

  • Nowhere in your code I see a addition of the string to the header. @Jay Ammar – Apurva Jha Aug 02 '15 at 08:46
  • Answer updated with how to incorporrate string. @ApurvaJha – Jay Alammar Aug 02 '15 at 13:08
  • If you could please look at this post as well and tell me the solution then I would be highly grateful to you https://stackoverflow.com/questions/31821112/header-not-getting-set-properly-in-android-retrofit @JayAmmar – Apurva Jha Aug 04 '15 at 23:58