When i call URL on postman, it returns expected result. I use PUT method using Volley, but it does not work.
StringRequest reg=new StringRequest(Request.Method.PUT, AppConfig.Registration, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("USER_REG",response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("USER_REG","-------------"+error);
}
}){
#Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put(name1,Name);
params.put(client_id1,CID);
params.put(email1,Email);
params.put(mobile1,Mobile);
params.put(password1,Password);
params.put(device_id1,"123456");
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(reg);
Here's the postman output.
Currently you are sending the data from your Android using Volley as path parameters which should be sent as JSON like the following.
RequestQueue queue = Volley.newRequestQueue(this);
private void makeJsonObjReq() {
Map<String, String> postParam= new HashMap<String, String>();
postParam.put(name1,Name);
postParam.put(client_id1,CID);
postParam.put(email1,Email);
postParam.put(mobile1,Mobile);
postParam.put(password1,Password);
postParam.put(device_id1,"123456");
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.PUT,
Const.YOUR_URL, new JSONObject(postParam),
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
msgResponse.setText(response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
}) {
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
jsonObjReq.setTag(TAG);
queue.add(jsonObjReq);
You need to add the Content-Type as application/json in the header as well.
Related
I'm passing a key-value pair in String request using post request method I'm getting error 400.
The Error is:E/Volley: [1120] NetworkUtility.shouldRetryException: Unexpected response code 400 for http://11.1.1.86:9000/cef/investmentStrategies/android
RequestQueue MyRequestQueue = Volley.newRequestQueue(getContext());
url5 = "http://11.1.1.86:9000/cef/investmentStrategies/android";
StringRequest stringRequest5 = new StringRequest(Request.Method.POST, url5, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
System.out.println("No Error");
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
System.out.println("Error is here..");
}
}) {
#Nullable
#Override
protected Map<String, String> getParams() {
Map<String, String> MyData = new HashMap<String, String>();
MyData.put("Field",id);
return MyData;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
return params;
}
};
MyRequestQueue.add(stringRequest5);
}
I am going to use volley to get data
RequestQueue queue = Volley.newRequestQueue(SummaryPhoneActivity.this);
String url = "https://covid19storageservice.table.core.windows.net/COVID19Survey?sv=2019-02-02&ss=bfqt&srt=sco&sp=rwdlacup&se=2020-06-30T18:04:08Z&st=2020-03-30T10:04:08Z&spr=https&sig=Ei3Kcr6xT5e7znK4ebwx6%2FVS09Ia5pbD1lnWRBh6bm4%3D";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("response:", response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("error:", error.toString());
}
}){
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
return params;
}
};
queue.add(stringRequest);
I can get error: com.android.volley.ClientError.
Above url is working on postman.
How can I use volley for above case. Thanks for your advance.
Am trying to send String and int as params in volley. But it show error on String if i use Map<String,Integer> params = new HashMap<String, String>(); and show error on int if i use Map<String,String> params = new HashMap<String, String>(); So how can i send int and String as a param in post request.
StringRequest stringRequest = new StringRequest(Request.Method.POST, "http://www.aaa.com/insert/signup" ,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.i("sign_up_res", response);
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("error" , error.toString());
}
}){
#Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("firstname" , PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString(Local_Preference.FIRSTNAME, "Not Set") );
params.put("number" , "123");
return params;
}
};
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
1000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue requestQueue = Volley.newRequestQueue(SignUp2Activity.this);
requestQueue.add(stringRequest);
In Sql table is like firstname (varchar) and number(int). I don't want to change the int type to varchar in sql. Is there any way to send int and String in a single params.
you can do this create a
Map params = new HashMap();
then
params.put(key,value); // for string
params.put(key,String.valueOf(value)); // for int As suggested
Please follow the code using this my problem is solved
String tag_json_obj = "json_obj_req";
String url = "https://api.androidhive.info/volley/person_object.json";
ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
url, null,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
pDialog.hide();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
pDialog.hide();
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("name", "Androidhive");
params.put("email", "abc#androidhive.info");
params.put("password", "password123");
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
I am sending a volley request like this
btnSignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//submitForm();
JsonObjectRequest jsonobjectRequest = new JsonObjectRequest(Request.Method.POST, URL, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
//errorlabel.setText(response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
errorlabel.setText("Invalid username / password");
}
}){
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("email", "asd#asd.com");
params.put("password", "asd");
return params;
}
};
errorlabel.setText(jsonobjectRequest.toString());
requestQueue.add(jsonobjectRequest);
}
});
}
But I get an error message from the server saying invalid email/password.
I have set up the correct params. I tested it out on Postman and it works in there. Here is a screenshot.
Screenshot
I had same problem and i tried with String request, its working
StringRequest jsonObjRequest = new StringRequest(Request.Method.POST,
URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}) {
#Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded; charset=UTF-8";
}
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> postParam = new HashMap<String, String>();
postParam.put("email", "asd#asd.com");
postParam.put("password", "asd");
return postParam;
}
};
requestQueue.add(jsonObjRequest);
I try to use GET on Volley , but i need send request to application/json .
I take a look for some answers , i try to use jsonBody , but it shows error:
null com.android.volley.ServerError
Here is my code:
public class MainActivity extends AppCompatActivity {
String url = "http://114.35.246.42:2212/MobileApp/DEST_WebService.asmx/GetNews";
JSONObject jsonBody;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
//I try to use this for send Header is application/json
jsonBody = new JSONObject("{\"type\":\"example\"}");
} catch (JSONException ex) {
ex.printStackTrace();
}
RequestQueue mQueue = Volley.newRequestQueue(getApplicationContext());
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, jsonBody,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d("TAG", response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("TAG", error.getMessage(), error);
}
});
mQueue.add(jsonObjectRequest);
}
}
Is any one can teach me how to fix this , any help would be appreciated.
Here is my url:
String url = "http://114.35.246.42:2212/MobileApp/DEST_WebService.asmx/GetNews";
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
return params;
}
Implementation in your's
public class MainActivity extends AppCompatActivity {
String url = "http://114.35.246.42:2212/MobileApp/DEST_WebService.asmx/GetNews";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RequestQueue mQueue = Volley.newRequestQueue(getApplicationContext());
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, jsonBody,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d("TAG", response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("TAG", error.getMessage(), error);
}
}) { //no semicolon or coma
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
return params;
}
};
mQueue.add(jsonObjectRequest);
}
}
In general for setting a custom header you need to override getHeaders and set the custom header manually. However, volley handles content type headers differently and getHeaders way does not always work.
So for your case you need to override getBodyContentType. So your code will look like
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, jsonBody,new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Log.d("TAG", response.toString());
}, new Response.ErrorListener(){
#Override
public void onErrorResponse(VolleyError error) {
Log.e("TAG", error.getMessage(), error);
}
}){
#Override
public String getBodyContentType(){
return "application/json";
}
};
I try to use GET on Volley
The docs for the method you are calling says this
Constructor which defaults to GET if jsonRequest is null, POST otherwise
You can't GET with an HTTP JSON body. Maybe that's the error.
//I try to use this for send Header is application/json
jsonBody = new JSONObject("{\"type\":\"example\"}");
That's not the header, so pass in null here to do GET
new JsonObjectRequest(url, null,
And towards the end of your request, override a method to request JSON
...
#Override
public void onErrorResponse(VolleyError error) {
Log.e("TAG", error.getMessage(), error);
}
}) { // Notice no semi-colon here
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
return params;
}
};
Use String request instead of jsonrequest like this
StringRequest loginMe = new StringRequest(Request.Method.GET, "http://114.35.246.42:2212/MobileApp/DEST_WebService.asmx/GetNews", new Response.Listener<String>() {
#Override
public void onResponse(String response) {
System.out.println("LoginActivity -- onResponse --> " + response);
if (progressDialog != null) {
progressDialog.dismiss();
}
try {
JSONObject jsonObject = new JSONObject(response);
} catch (Exception e) {
CommonUtility.somethingWentWrongDialog(activity,
"LoginActivity -- onResponse-- Exception --> ".concat(e.getMessage()));
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
if (progressDialog != null) {
progressDialog.dismiss();
}
CommonUtility.somethingWentWrongDialog(activity,
"LoginActivity -- onErrorResponse --> ".concat(error.getMessage()));
}
}) {
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
System.out.println("LoginActivity -- LoginParams --> " + params.toString());
return params;
}
};
loginMe.setRetryPolicy(new DefaultRetryPolicy(60000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Volley.newRequestQueue(activity).add(loginMe);