简体   繁体   中英

Android Networking API add Bearer token

I am trying to use the Twitter API. I have tested the Endpoint in postman and it gives me the correct response I need. The problem I am having in the code is that it is returning a null as the response. I believe this is due to me adding the bearer token incorrectly to this Android networking API. Can anyone help me

public class TwitterUserLookUp extends AppCompatActivity {
RecyclerView recyclerView;
EditText userName;
Button search;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_twitter_user_look_up);
    recyclerView = findViewById(R.id.twitterRCV);
    userName = findViewById(R.id.twitterHandle);
    search = findViewById(R.id.userLookUp);
    AndroidNetworking.initialize(getApplicationContext());
    search.setOnClickListener(v -> {
        if (TextUtils.isEmpty(userName.getText())) {
            userName.setError("Twitter Handle is required.");
            userName.requestFocus();
        } else {
            searchFunctionality(userName.getText().toString().trim());
        }
    });
}

public void searchFunctionality(String name) {
    AndroidNetworking.get("https://api.twitter.com/2/users/by/username/" + name)
            .addPathParameter("pageNumber", "0")
            .addQueryParameter("limit", "3")
            .addHeaders("Authorization", "xx")
            .setTag("test")
            .setPriority(Priority.LOW)
            .build()
            .getAsString(new StringRequestListener() {
                @Override
                public void onResponse(String response) {
                    Log.i("Testing", response);
                    Toast.makeText(TwitterUserLookUp.this, "User details: " + response, Toast.LENGTH_LONG).show();
                }

                @Override
                public void onError(ANError anError) {
                    Log.i("test", "test" + anError.getMessage());
                    Toast.makeText(TwitterUserLookUp.this, "Error Occurred: " + anError.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
}

}

i adds the Bearer token using this way api interface

@POST("..............")
Call<PostModel> postModel(............,
                          @Header("Authorization") String token);

then pass your token to interface using

"Bearer "+yourToken

I was able to solve my problem by using a API library called volley. I personally prefer Volley over Android networking library as it is easier to understand what is going on. Below I was able to add a parameter with my bearer token

public void searchFunctionality(String name) {
    final String url = "https://api.twitter.com/2/users/by/username/" + name;
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url, response -> Log.i("The Response ", response), error -> {
        Log.i("fail", "error" + error.getMessage());
    }) {
        @Override
        public Map<String, String> getHeaders() {
            Map<String, String> params = new HashMap<>();
            params.put("Authorization","Bearer " + "XX");
            return params;
        }
    };
    queue.add(stringRequest);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM