Why Android Studio prompt “Method doesn't override its superclass”? - java

I want to use volley to build http connection with authentication. Following this answer I add the segment
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> params = new HashMap<String, String>();
String creds = String.format("%s:%s","USERNAME","PASSWORD");
String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT);
params.put("Authorization", auth);
return params;
}
in Anonymous Inner Class StringRequest , and it looks like:
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
//the segment below is what I add
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> params = new HashMap<String, String>();
String creds = String.format("%s:%s","USERNAME","PASSWORD");
String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT);
params.put("Authorization", auth);
return params;
}
//the segment above is what I add
#Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
However, IDE hints that getHeaders() doesn't override its superclass.
Why?I found that StringRequest extends class Request<String>, and the latter does have a method called getHeaders().

You are overriding public Map<String, String> getHeaders() inside a new instance of Response.Listener<String> instead of Request<String> and Response.Listener<String> does not have such method (only onResponse(String)).

Related

What's this concept called?

We have this method to make a request using the Volley library:
public void requestWithSomeHttpHeaders() {
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://www.somewebsite.com";
StringRequest getRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>()
{
#Override
public void onResponse(String response) {
// response
Log.d("Response", response);
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
Log.d("ERROR","error => "+error.toString());
}
}
) {
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("User-Agent", "Nintendo Gameboy");
params.put("Accept-Language", "fr");
return params;
}
};
queue.add(getRequest);
}
Notice the part:
{
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("User-Agent", "Nintendo Gameboy");
params.put("Accept-Language", "fr");
return params;
}
};
It confuses me because there's no comma before it, so it's not a function argument. If you look at the method definition:
public StringRequest(
int method,
String url,
Listener<String> listener,
#Nullable ErrorListener errorListener) {
super(method, url, errorListener);
mListener = listener;
}
there's no clear code which explains support for this.
What's this concept called and how do you write code that supports this?
This snippet shows the creation of an instance of an anonymous class that extends StringRequest and overrides the getHeaders() method.

Calling an API with string parametrs in Android with Volley

I'am trying to call an API that adds data to a dynamoDB each time a user creates an account .
The url of the API is in the following format :
https://t3x9lg8utf.execute-api.us-east-2.amazonaws.com/prod? id=""&username=""&numero_passeport=""&decision=""
The problem is that when i call the API using volley i get this error :
06-03 14:32:23.599 13503-14515/com.amazonaws.youruserpools.CognitoYourUserPoolsDemo E/Volley: [14796] BasicNetwork.performRequest: Unexpected response code 400 for https://t3x9lg8utf.execute-api.us-east-2.amazonaws.com/prod
The code i used to call the API :
StringRequest sr = new StringRequest(Request.Method.GET, "https://t3x9lg8utf.execute-api.us-east-2.amazonaws.com/prod",
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.e("HttpClient", "success! response: " + response.toString());
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("HttpClient", "error: " + error.toString());
}
})
{
#Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("id","\"\"");
params.put("username","\"zaeae\"");
params.put("numero_passeport","\"\"");
params.put("decision","\"\"");
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
return params;
}
};
queue.add(sr);
try this
StringRequest sr = new StringRequest(Request.Method.GET, "https://t3x9lg8utf.execute-api.us-east-2.amazonaws.com/prod?id="+Uri.encode(id)+"&username="+Uri.encode(username)+"&numero_passeport="+Uri.encode(numero_passeport)+"&decision="+Uri.encode(decision),
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.e("HttpClient", "success! response: " + response.toString());
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("HttpClient", "error: " + error.toString());
}
})
{
#Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("id","\"\"");
params.put("username","\"zaeae\"");
params.put("numero_passeport","\"\"");
params.put("decision","\"\"");
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
return params;
}
};
queue.add(sr);
also pass values for id, username, numero_passeport and decision
? id=""&
These are URL parameters, which cannot be passed via getParams()
You need to append them to the string after Request.Method.GET

Posting Data to Server with volley library from android App

Guys i need help am trying to post this data to my server here but it is returning an error, it seems to be working just fine in postman the problem comes in while trying to implement in android app using google's volley library.
Link to server script.
This is the screenshot of a successful post working in postman rest client:2
private void SaveDataToServer() {
StringRequest serverPostRequest = new StringRequest(Request.Method.POST, Config.SAVE_INVENTORY_URL,
new Response.Listener<String>() {
#Override
public void onResponse(String json) {
try {
Toast.makeText(SelectItemsActivity.this, json.toString(), Toast.LENGTH_SHORT).show();
Log.e("RESPONSE FROM SERVER",json);
JSONObject dataJson=new JSONObject(json);
JSONObject myJson=dataJson.getJSONObject("status");
String status=myJson.getString("status_text");
if (status.equalsIgnoreCase("Success.")){
Toast.makeText(SelectItemsActivity.this, "Data saved Successfully", Toast.LENGTH_SHORT).show();
proggressShow.dismiss();
}else {
Toast.makeText(SelectItemsActivity.this, "An error occured while saving data", Toast.LENGTH_SHORT).show();
proggressShow.dismiss();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
}
}){
#Override
protected Map<String, String> getPostParams() {
HashMap<String, String> params = new HashMap<String, String>();
params.put("api_key",Config.API_KEY);
params.put("move_id", "1");
params.put("room_name", "Attic room");
params.put("item_name", "Halloween Broom");
params.put("item_id", "6");
Log.e("datat to server",params.toString());
return params;
}
};
saveDataRequest.add(serverPostRequest);
}
Try adding getHeaders method in your request
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
final HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", "multipart/form-data");
return headers;
}
After adding this lines to my request headers i was able to successfully commit the database to my db.
Also i made sure i am using a stringRequest for a jsonRequest it does not work for reasons i do not know.
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type","application/x-www-form-urlencoded");
return headers;
}

Get Auth AcessToken from Paypal Android sdk

I have tried to get auth Access token using below code.and based on this code ,we will get Full transaction detail of Paypal.
Below code is working fine in Lollipop and above, but when we run my app in lolipop below version then not getting any response from Paypal SDK.I did lot of R&D for this issue.Please give me solution of this.
My Working Code in Lollipop and above:
public void getAcessToken(final String PayID) {
base64 = "Basic " + Base64.encodeToString((URLs.PAYPAL_CLIENTID + ":" + URLs.PAYPAL_SECRET_KEY).getBytes(), Base64.NO_WRAP);
Log.i("TAG", "Base64=" + base64);
if (!CM.isInternetAvailable(this)) {
CM.showPopupCommonValidation(this, getResources().getString(R.string.internet_unavailable_msg), false);
return;
}
showDialog();
String url = "https://api.sandbox.paypal.com/v1/oauth2/token";
Log.i("WebCalls", url);
StringRequest stringRequest = new StringRequest(Request.Method.POST,
url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
KcsLog.i("WebCalls", response.toString());
String AccessToken = CM.getValueFromJson("access_token", response);
String token_type = CM.getValueFromJson("token_type", response);
GetTransactionID(token_type + " " + AccessToken, PayID);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
dismissDialog();
CM.showVolleyErrorMessage(error, ViewGiftCode.this, "", false);
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> post = new HashMap<String, String>();
post.put("grant_type", "client_credentials");
return post;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> post = new HashMap<String, String>();
post.put("Accept", "application/json");
post.put("Accept-Language", "en_US");
post.put("Content-Type", "application/x-www-form-urlencoded");
post.put("Authorization", base64);
return post;
}
};
((MainApplications) getApplicationContext()).volley.addToRequestQueue(stringRequest);
}
Please give me solution for below lollipop.

How can I pass parameters to android volley post method?

I defines a function in an activity class like this:
private void passParam(String pname){
StringRequest compareRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
//handle response code
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError arg0) {}
}) {
#Override
protected Map<String, String> getParams() {
//set post params
Map<String, String> params = new HashMap<String, String>();
params.put("foo", "foo");
params.put("name", pname);//Grammar error, want to use parameter pname
return params;
}
};
requestQ.add(compareRequest);
}
I want to set post parameter "name" value to the parameter pname of function passParam.How to do this conveniently?Thanks!

Categories

Resources