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
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
i adds the Bearer token using this way api interface
then pass your token to interface using
"Bearer "+yourToken