I have a page for sending user's data to server by volley, the method is post (it also works with patch ) and i want to send the information that user entered to the server, but i have error and it works correctly with same data in Postman.
And you know i should send my data as params.
This is my function:
public void Update()
{
final String tagsssss = "sdkjfskjdfhksd";
final String url = "https://parastoo.app/api/update-get-driver-info";
String token = G.getString("token");
String name = formEdtName.getText().toString();
String smartNum = formEdtSmartNum.getText().toString();
String sheba = formEdtSheba.getText().toString();
String code = formEdtCode.getText().toString();
String driverDebitNumber = formEdtBank.getText().toString();
Double lat = G.getDouble("latitude", 0);
Double lng = G.getDouble("longitude", 0);
String latitude = String.valueOf(lat);
String longitude = String.valueOf(lng);
String imageData = imagToString(bitmap);
String phoneNum = G.getString("phonenumber");
Toast.makeText(getApplicationContext(), "Start", Toast.LENGTH_SHORT).show();
Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>()
{
#Override
public void onResponse(JSONObject response)
{
startActivity(new Intent(RegisterActivity.this, VerifiedRegisterActivity.class));
Log.i(tagsssss, "Success");
Log.i(tagsssss, response.toString());
}
};
Response.ErrorListener errorListener = new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error)
{
Log.e(tagsssss, error.toString());
//Log.e(tagsssss, error.getCause().toString());
error.printStackTrace();
}
};
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, listener, errorListener)
{
#Override
public Map<String, String> getParams() throws AuthFailureError
{
Map<String, String> params = new HashMap<String, String>();
params.put("driverPhoneNumber", phoneNum);
params.put("driverSmartNumber", smartNum);
params.put("driverNationalCode", code);
params.put("latitude", latitude);
params.put("longitude", longitude);
params.put("driverIbanNumber", sheba);
params.put("driverFailureRate", null);
params.put("driverActive", null);
params.put("driverDebitNumber", driverDebitNumber);
params.put("driverFullName", name);
// params.put("avatar_url", imageData);
params.put("token", token);
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError
{
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "multipart/form-data");
return headers;
}
};
final int socketTimeout = 100000;
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
request.setRetryPolicy(policy);
AppSingleton.getInstance(getApplicationContext()).addToRequestQueue(request);
}
Related
Getting following exception in Spring boot server:
JSON parse error: Unrecognized token was expecting ('true', 'false' or 'null'); nested exception is com.fasterxml.jackson.core.JsonParseException
while receiving POST request from Volley-android into Spring Rest Controller
public class RegistrationCredential {
#NotEmpty(message = "MacId is mandatory")
#JsonProperty("macId")
private String macId;
#NotEmpty(message = "Mobile number is mandatory")
#JsonProperty("mobileNo")
private String mobileNo;
....
}
Added #JsonProperty("name") tag in model
#PostMapping(consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE},
produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
public ResponseEntity<String> vendorMobileRegistration(#Valid #RequestBody final RegistrationCredential credential) {}
Excerpt From Volley implemenettaion:
StringRequest stringRequest = new StringRequest(Request.Method.POST, SpotPollingURLs.REGISTRATION_URL, new Response.Listener <String>() {
#Override
public void onResponse(String response) {
System.out.println("Getting response frome url --- "+response);
try {
JSONObject jsonObject = new JSONObject(response);
String status = jsonObject.getString("status");
String messageCode = jsonObject.getString("MessageCode");
String message = jsonObject.getString("message");
if (status.equals("success") && messageCode.equals("MOBILE_REGISTERED_SUCCESSFULLY")) {
sendOTP(mobile, "send");
customDalog(mobile);
} else if (status.equals("success") && messageCode.equals("MOBILE_NUMBER_ALREADY_REGISTERED")) {
new CustomToast().showToast(getContext(), view, message);
} else if (status.equals("error")) {
new CustomToast().showToast(getContext(), view, message);
} else {
new CustomToast().showToast(getContext(), view, "Something went wrong check your connection");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
System.out.println("Getting error -- "+error);
progressBar.setVisibility(View.GONE);
}
}){
#Override
public Map <String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
return headers;
}
#Override
protected Map <String, String> getParams() throws AuthFailureError {
HashMap<String, String> params = new HashMap<>();
params.put("mobileNo", mobile);
params.put("macId", uniqueId);
return params;
}
};
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
I'm trying to receive raw string response using volley, but I'm not able to get any type of response. Below is my code that I have tried while hiring the user.
I'm able to get key value pair response, but if we are using raw string, it's not throwing any response in log cat.
username = editTextUsername.getText().toString().trim();
password = editTextPassword.getText().toString().trim();
Toast.makeText(this, "Button Hit", Toast.LENGTH_SHORT).show();
StringRequest stringRequest = new StringRequest(Request.Method.POST, LOGIN_URL,
new Response.Listener<String>()
{
#Override
public void onResponse(String response) {
if (response.trim().equals("Message:Valid User")) {
Toast.makeText(PostLogin.this, "Resposne"+response.toString(), Toast.LENGTH_SHORT).show();
Log.d("Response:",""+response.toString());
} else {
Toast.makeText(PostLogin.this, response, Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(PostLogin.this, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<String, String>();
map.put(KEY_USERNAME, username);
map.put(KEY_PASSWORD, password);
map.put("Content-Type","application/json");
return map;
}
#Override
public byte[] getBody() throws com.android.volley.AuthFailureError {
String str = "{\"login\":\""+username+"\",\"password\":\""+password+"\"}";
return str.getBytes();
};
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
I am trying to achieve single function that can be used to do server calls for different URL request.To do this I need to send different parameters for different URL.
I am doing this and it is showing "Invalid Request".
Can anyone suggest me how to achieve this.
#Override
public Map<String, String> getParams()
{
Map<String, String> params = new HashMap<>();
switch (URL)
{
case "REFERRALS_LIST":
params.put("customer_id", data.GetData(SharedDataHandler.CUSTOMER_ID));
params.put("first_name", argslist.get(0));
params.put("last_name", argslist.get(1));
params.put("address", argslist.get(2));
params.put("mobile", argslist.get(3));
params.put("email", argslist.get(4));
params.put("contact_home", argslist.get(5));
Log.d("test", "getParams: "+argslist.get(0)+argslist.get(1)+argslist.get(2)+argslist.get(3)+argslist.get(4)+argslist.get(5));
break;
}
return params;
}
Hello Try this if it helps
1) performVolleyRequest is method for making volley request
2) I maintained Switch Case for Handling the various requests
3) You Have to use requestType variable according to your requirement as I left it blank I didn't know when your required it
public class MainActivity extends AppCompatActivity {
public static final String KEY_USERNAME = "username";
public static final String KEY_PASSWORD = "password";
public static final String KEY_EMAIL = "email";
String requestType="";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switch (requestType){
case "Registration":
Map<String, String> dynamicParams = new HashMap<String, String>();
dynamicParams.put(KEY_USERNAME, "userName");
dynamicParams.put(KEY_PASSWORD, "password");
dynamicParams.put(KEY_EMAIL, "email");
final String REGISTER_URL_REG = "http://foo.com/volleyRegister.php";
performVolleyRequest(dynamicParams,REGISTER_URL_REG);
break;
case "Login":
Map<String, String> dynamicParamsLogin = new HashMap<String, String>();
dynamicParamsLogin.put(KEY_USERNAME, "userName");
dynamicParamsLogin.put(KEY_PASSWORD, "password");
final String REGISTER_URL_LOGIN = "http://foo.com/volleyLogin.php";
performVolleyRequest(dynamicParamsLogin,REGISTER_URL_LOGIN);
break;
}
}
private void performVolleyRequest(final Map<String, String> paramDynamic,final String REGISTER_URL) {
StringRequest stringRequest = new StringRequest(DownloadManager.Request.Method.POST, REGISTER_URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(MainActivity.this, response, Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params=paramDynamic;
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
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.