Cannot resolve createSuccessListner in custom request - android

Im using volley library in my project and i need to use post method it turns out i have to use a custom request class so i did but in my activity some methods cannot be resolved like this.createRequestSuccessListener(), this.createRequestErrorListener();
here is my code
enter code here
String register_url = "xxxxxxx";
RequestQueue requestQueue = Volley.newRequestQueue(MyActivity.this);
CustomRequest jsObjRequest = new CustomRequest(Request.Method.POST, register_url, params, this.createRequestSuccessListener(), this.createRequestErrorListener());
requestQueue.add(jsObjRequest);
how can this be resolved any help would be appreciated .

Related

POST request with JSON body in Volley Android

I am using volley library. I have the following API url http://example.com/project/contriller/ and need to post the json request as body {"function":"getList","parameters":{"latitude":"10.0086575","longitude":"76.3187739"},"token":""}to it.
How can send it using Volley?
Please check below two options for that.
Option1
Try to send data in Map variable as below, and put this code just above you are calling request using Post as below.
Map<String, String> postParam= new HashMap<String, String>();
postParam.put("function", "getList");
postParam.put("latitude", "10.0086575");
postParam.put("token", "");
new JsonObjectRequest(url, postParam, new Response.Listener<JSONObject>() { ... });
option2
You can use below to send direct JSON.
final JSONObject jsonData = new JSONObject("{\"function\":\"getList\",\"parameters\":{\"latitude\":\"10.0086575\",\"longitude\":\"76.3187739\"},\"token\":\"\"}");
new JsonObjectRequest(url, jsonData, new Response.Listener<JSONObject>() { ... });

Post JSON Array to web server using jsonRquestObject in android

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
Const.URL_LOGIN, null,
new Response.Listener<JSONObject>() {
Does the above code send JSON Object to a particular URL?
Ok, the JSONObject class from Volley, normally has two conditions:
the third param is the json you want to send.
If you use:
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
Const.URL_LOGIN, null,
new Response.Listener<JSONObject>() {
}
.
.
.
You you are using the first constructor, that means you want to send some object in your body request, and if you send null, volley in the constructor do this param.toString(), so if you send null, imagine, null.toString(), obviously will crash, that is not possible operation of a null object.
So the other option is to use the second constructor:
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
Const.URL_LOGIN,
new Response.Listener<JSONObject>() {
}
.
.
.
You dont have to send the third param (the param/json body), so this constructor auatomatically has the condition that you dont want to send nothing to the server. I think you have to use the second constructor, otherwise could fail.
Regards.

Volley, BasicNetwork.performRequest: Unexpected response code 405, while making StringRequest

Using Volley, I POST StringRequest and i am getting error when i am accessing the url, as it is
https://www.google.com/?gfe_rd=cr&ei=vX8jVdTvOsOq8weigYHICA&gws_rd=cr&fg=1
But when is use http instead of https above, it won't give error and working well.
cookie and client code is below,
DefaultHttpClient client_R = new DefaultHttpClient();
RequestQueue queue_R = Volley.newRequestQueue(this, new HttpClientStack(client_R));
CookieStore store_R = client_R.getCookieStore();
Cookie cookie_R = new BasicClientCookie("Example_Cookie", "80");
store_R.addCookie(cookie_R);
below is logcat output,
[199] BasicNetwork.performRequest: Unexpected response code 405 for https://www.google.com/?gfe_rd=cr&ei=TX4jVcChFaTj8wehzoCgCw&gws_rd=cr&fg=1
Why it is giving error ? With some URLs having https, it is working instead.
The problem is that you are using a POST api request for a GET request.
In your StringRequest Method use GET instead of POST.
StringRequest sr = new StringRequest(Request.Method.GET, String url, Listener<String> listener, ErrorListener errorListener);
Do you use an SSL client for your https request.
Your 405 error code is a "Method Not Allowed" error: see more here
To use https, i use an OkHttpClient with a TrustManager.

How send multipart/fom-data with Volley and get a response in a JSON Object?

I have a functionnal Android app send request with Volley and get response in JsonObject. Now, I need to include an image in my request and I don't have any idea how I can do that, and still receive my response in a JsonObject.
Thank you for helping.
Fabien.
JsonMultipartRequest<Upload> request = new JsonMultipartRequest<Upload>(Method.POST, apiUrl, mListener, mErrorListener);
request.addFile("photo", image_path);
RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext());
mRequestQueue.addRequest(request);
mRequestQueue.start();
JsonMulitpartRequest is extended class of MultipartRequest where Override the below method to make it JSON object parseNetworkResponse
This is using library VolleyPlus

How to pass HttpPost( URI ) with Volley Request ?

How to pass HttpPost( URI ) with StringRequest using Volley ? I need to pass URI, with Request, like ,
HttpPost localHttpPost = new HttpPost(" some URI ");
How to do using Volley request ?
Well, first of all you need a requestQueue.
requestQueue = Volley.newRequestQueue(context);
Then, having this requestQueue defined in some place on your code (A global variable) the work is defining a Request. It could be for example a StringRequest. Notice the Method.POST in the code.
StringRequest req = new StringRequest(Method.POST, getBaseUrl() + POST_OPPORTUNITIES_URL, requestListener, new Response.ErrorListener(){
#Override
public void onErrorResponse(VolleyError volleyError) {
//Manage the error
}
});
And finally add the request to de Queue
requestQueue.add(req);

Categories

Resources