Volley PUT StringRequest not sent - android

I am sending a StringRequest using Volley to update a single record in mysql.The url is fine when i am sending the same request from postman i am getting result but when i send the request from android its not sent. I am not getting whats the issue.I am sendin two values that too iam sending it in url.Here is the code
StringRequest req = new StringRequest(Request.Method.PUT, url,new Response.Listener<String>() {
#Override
public void onResponse(String response) {
resp = Integer.parseInt(response.toString());
hidepDialog();
if(resp == 1){
Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(),"Failure",Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});

Related

What's the same in Retrofit POST like StringRequest in Volley?

For example, I have Volley code for StringRequest here.
What do we use to achieve the same result for Retrofit and how does interface look like in this case?
String url = getString(R.string.API_URL) + "/social/revoke-token";
StringRequest postRequest = new StringRequest
(Request.Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("RESPONSE FROM SERVER", response.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error
}
}){

Upload some string to server and download with volley

In my app, I need some string to be downloaded from server to use in the app. How can I upload the strings to the server?.
I've uploaded a text file at first but when sending request I received this error in logcat:
Unexpected response code 307 for:
Also, I uploaded my text in a web page body same error happened.
Please help me how to upload some text or an ArrayList to the server and download with volley and use in the app.
This is my volley request method:
private void getOnlinePrice (){
StringRequest request=new StringRequest(Request.Method.GET,URI_SHOW_PARAMS, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
String s=response;
txtinfo.setText(s);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(request);
}
You can do this:
String HTTP_URL = "YOUR URL";
RequestQueue requestQueue = Volley.newRequestQueue(your_activity.this);
// sends data using POST method
StringRequest postRequest = new StringRequest(Request.Method.POST, HTTP_URL,
new Response.Listener<String>()
{
#Override
public void onResponse(String response) {
String resp = response;
if (!TextUtils.isEmpty(resp))
{
Toast.makeText(getApplicationContext(), "my response is" + resp,
Toast.LENGTH_SHORT).show();
}
else{
// if the response if empty
Toast.makeText(getApplicationContext(), "my response is empty",
Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
// error
}
}
) {
#Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
params.put("POST_VARIABLE_1", YourNamestring);
params.put("POST_VARIABLE_2", YourNameString2);
return params;
}
};
requestQueue.add(postRequest);

volley not getting the string?

I have a php with red text on it only.
I made a volley request but it is not getting my red word.
what is wrong?
private void getColor() {
final String url = "http://190.128.0.1/color.php";
StringRequest strReq = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
String color = response; //it is not red
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//Log.d("Error.Response", response);
}
}
);
}
Make sure you add the request to the request queue. Uncomment the logging in your Response listener

How to get JSON from server without send JSON request

I need help.
I use volley for sending json object to my rest API server. And i get data from this API to my app (json). Its work fine:
JsonObjectRequest mJsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, JSONDATA, JSONListener, errorListener)
...
And now I want to send request without JSONDATA (i can't set null). It is for global values. There is not necessary send some data. And i dunno how to send this request. Can you help me?
till i understand ur problem my answer is this
StringRequest distRequest=new StringRequest(Request.Method.POST, YOUR_URL, new Response.Listener<String>() {
#Override public void onResponse(String response) {
Toast.makeText(MainActivity.this, " "+response.toString, Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(MainActivity.this, " "+error.toString, Toast.LENGTH_SHORT).show();
}
});
RequestQueue distQueue=Volley.newRequestQueue(this);
distQueue.add(distRequest);
}
Try this:
// prepare the Request
JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>()
{
#Override
public void onResponse(JSONObject response) {
// display response
Log.d("Response", response.toString());
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
Log.d("Error.Response", response);
}
}
);
// add it to the RequestQueue
queue.add(getRequest);

Volley Error 500

I am trying to parse a url that i found online.
However when i try to use Volley standard way of sending a request, it keeps giving me the error of
BasicNetwork.performRequest: Unexpected response code 500 for http://www.mytransport.sg/content/mytransport/home/myconcierge/trafficcameras/jcr:content/par/cameras_slideshow.singleresult.html?cameraid=2704
However, when i try to access the url via my google chrome, it works fine without any problem.
This is my code:
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.e("RESPONSE", response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// Error handling
error.printStackTrace();
}
});
// Add the request to the queue
Volley.newRequestQueue(this).add(stringRequest);

Categories

Resources