I have an API with a delete function like this:
http://localhost/v1/deletePost/:id
when i try in postman succeed by entering param in url ":id" like ../deletePost/37".
how to implement the "/:id" request in android using Volley library?
Here is a guide about how to use Volley Library
On the page Sending a Simple Request:
final TextView mTextView = (TextView) findViewById(R.id.text);
//...
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.queue.add(stringRequest);
Then you need to change GET to DELETE Request.Method.DELETE and the url for your url with the id http://localhost/v1/deletePost/37
Will be something like this:
String baseUrl ="http://localhost/v1/deletePost/";
String url = baseUrl + "3" //Here you change the ID, can put as variable
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.DELETE, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
});
Related
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);
I have already integrated volley library working good with JSON. Now am trying to access WCF SOAP I do not know how to pass XML string as request and how to get XML string as response.
// Tag used to cancel the request
String tag_string_req = "string_req";
//String url = "URL......";
final ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();
StringRequest strReq = new StringRequest(Request.Method.POST,
url, new Response.Listener<String>() {
#Override
public void onResponse(String 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
public String getBodyContentType() {
return "application/xml; charset=" +
getParamsEncoding();
}
#Override
public byte[] getBody() throws AuthFailureError {
String postData = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<sampletag>\n" +
"\t<sampletag>data</sampletag>\n" +
"</sampletag>"; // TODO get your final output
try {
return postData == null ? null :
postData.getBytes(getParamsEncoding());
} catch (UnsupportedEncodingException uee) {
// TODO consider if some other action should be taken
return null;
}
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
You can use the String request of Volley. The String request returns a string which can you parse using XMLReader
StringRequest req = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>()
{
#Override
public void onResponse(String response) {
processDataUsingXMLReader(reponse);
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
// handle error response
}
}
);
Volley does not have any XML Request, checkout the official documentation: https://android.googlesource.com/platform/frameworks/volley/+/android-4.3_r0.9/src/com/android/volley/toolbox/
There are two ways you can do it:
Method 1
So, you will have to fetch the raw String using StringRequest and then parse it.
Code:
StringRequest request = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>()
{
#Override
public void onResponse(String response) {
// now, convert response (String) to XML
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
//error
}
}
);
queue.add(request);
To convert String to XML:
You can try Simple-XML, it is a XML Marshaling Tool. Link : http://simple.sourceforge.net/download.php
Sample code (string to xml using Simple) :
Serializer serializer = new Persister();
serializer.read(Pojo.class, response);
Method 2
Try this Volley Adapter Class : https://gist.github.com/itsalif/6149365
It also does the same, using Simple-XML to serialize XML Objects without your coding effort.
Assuming you want to parse XML data using Android Volley from the String response, you can achieve this by converting the response to an InputStream using ByteArrayInputStream, as follows:
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
XMLParser xmlParser = new XMLParser();
InputStream inputStream = new
ByteArrayInputStream(response.getBytes("UTF-8"));
List<YourObject> object = new ArrayList<>();
object.addAll(xmlParser.parseFeed(inputStream));
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}
);
This example simply demonstrates parsing an RSS feed and storing an array of objects in a List, which is demonstrated in greater detail here.
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
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);
I was going through volley library documentation with the following example
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
My question is if i add an other request(similar request i.e. StringRequest) request to the queue, how can i map my request to response since the response is asynchronous.
1request --> 1response.
2request --> 2response.
I don't have any priorities for request and should be done concurrently (not sequential requests).