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);
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
Why is this Volley jsonObjectRequest not working properly in AppWidgetProvider()?
You're doing good, but you have to call updateAppWidget()
after views.setTextViewText(R.id.city, yourCity)
, to update the view on user's screen.
Related Topics
How to Handle Button Clicks Using the Xml Onclick Within Fragments
Android Accelerometer Accuracy (Inertial Navigation)
Maintain/Save/Restore Scroll Position When Returning to a Listview
Sending and Receiving Sms and Mms in Android (Pre Kit Kat Android 4.4)
Getting the Screen Density Programmatically in Android
Comparison of Android Networking Libraries: Okhttp, Retrofit, and Volley
Key Hash For Android-Facebook App
Best Practice For Storing and Protecting Private API Keys in Applications
How to Detect When an Android App Goes to the Background and Come Back to the Foreground
Get Real Path from Uri, Android Kitkat New Storage Access Framework
How to Get Current Time and Date in Android
Onactivityresult Method Is Deprecated, What Is the Alternative
"Arrayadapter Requires the Resource Id to Be a Textview" Xml Problems
How to Start My App When the Phone Starts on Android
How to Set a Custom Font in the Actionbar Title