skip to Main Content

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

}

2

Answers


  1. Chosen as BEST ANSWER

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

  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search