Volley JsonObjectRequest Post parameters no longer work
I ended up using Volley's StringRequest instead, because I was using too much valuable time trying to make JsonObjectRequest work.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://myserveraddress";
StringRequest strRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>()
{
@Override
public void onResponse(String response)
{
Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
Toast.makeText(getApplicationContext(), error.toString(), Toast.LENGTH_SHORT).show();
}
})
{
@Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
params.put("tag", "test");
return params;
}
};
queue.add(strRequest);
This worked for me. Its just as simple as JsonObjectRequest, but uses a String instead.
Volley jsonObjectRequest with parameter not working
Fixed my problem by adding this under the errorResponse
@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;
}
And also changing my php to this
$inputJSON = file_get_contents('php://input');
$input= json_decode( $inputJSON );
$var = $input->user; //user is from the parameters jsonobject
Volley JsonObjectRequest Post request not working
try to use this helper class
import java.io.UnsupportedEncodingException;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;
public class CustomRequest extends Request<JSONObject> {
private Listener<JSONObject> listener;
private Map<String, String> params;
public CustomRequest(String url, Map<String, String> params,
Listener<JSONObject> reponseListener, ErrorListener errorListener) {
super(Method.GET, url, errorListener);
this.listener = reponseListener;
this.params = params;
}
public CustomRequest(int method, String url, Map<String, String> params,
Listener<JSONObject> reponseListener, ErrorListener errorListener) {
super(method, url, errorListener);
this.listener = reponseListener;
this.params = params;
}
protected Map<String, String> getParams()
throws com.android.volley.AuthFailureError {
return params;
};
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONObject(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
@Override
protected void deliverResponse(JSONObject response) {
// TODO Auto-generated method stub
listener.onResponse(response);
}
}
In activity/fragment do use this
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
CustomRequest jsObjRequest = new CustomRequest(Method.POST, url, params, this.createRequestSuccessListener(), this.createRequestErrorListener());
requestQueue.add(jsObjRequest);
Sending post parameters with JsonObjectRequest volley to active campaign api
Try this using StringRequest
StringRequest jsonObjRequest = new StringRequest(Request.Method.POST,
"https://brumano.api-us1.com/admin/api.php?api_key=key&api_action=contact_add&api_output=json",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("url",response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("volley", "Error: " + error.getMessage());
}
}) {
@Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded; charset=UTF-8";
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("email", "w@sss.com");
params.put("p[1]", "1");
return params;
}
};
queue.add(jsonObjRequest);
Volley JSONObjectRequest Request.Method.POST but received GET
I solved my own problem
Sent POST request but server says GET request in Android volley, what I am doing wrong here?
Solution:
Due to i'm requesting to PHP server, i need to point out the filename.
Currently i point the request to a URL without Filename extension, and i just add index.php
to my end of URL and it worked.
Volley not sending a post request with parameters.
After spending some more time looking into this problem, I found out that Volley does not properly work with JSON request with POST requests. User @SMR suggested that in the comment section of my question. I also saw a similar answers on Google groups and the mian repo on git. I ended up using GET requests to pass the information to the server and retrieve a JSON feed.
Related Topics
Android Sdk 22 - Searchview Rendering Problems
How to Use an Arrayadapter in Android of Custom Objects
Android Persistence Room: "Cannot Figure Out How to Read This Field from a Cursor"
Memory Leaks with Custom Font for Set Custom Font
How to Use Getsharedpreferences in Android
How to Move the Layout Up When the Soft Keyboard Is Shown Android
How to Use View.Ontouchlistener Instead of Onclick
Android.Database.Cursorindexoutofboundsexception: Index 0 Requested, with a Size of 0
Android Studio Gradle Dsl Method Not Found: 'Android()' -- Error(17,0)
Retrieving a List of Users Who Have Registered Using Firebase Auth
How to Copy Text to Clip Board in Android
"Failure Delivering Result " - Onactivityforresult
Android - How to Achieve Setonclicklistener in Kotlin
Encode and Decode Bitmap Object in Base64 String in Android
Android Webview with Garbled Utf-8 Characters
Adding Opencv to Native C Code Through Cmake on Android Studio