Post XML request and XML response Volley Library - android

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.

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);

Delete request params URL Volley android

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!");
}
});

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 send Json POST request using Volley in android in the Given Tutorial?

Hi I am a beginner in Android , I am learning to make Api Calls. I got a tutorial of Volley which uses GET to Receive response. Now I want to Send Post request Using Volley. I don't know how to do that, what would be the code for POST in the given Tutorial. Please guide me to send Post Request.The link to the tutorial that I am learning is http://www.truiton.com/2015/02/android-volley-example/
you have to used below code to send request
// 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.POST, 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);
try {
/** json object parameter**/
JSONObject jsonObject = new JSONObject();
jsonObject.put("hello", "hello");
Log.e("jsonObject params", jsonObject.toString() + "");
/**URL */
String url ="http://google.com"
progress.setVisibility(View.VISIBLE);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject jsonObject) {
progress.setVisibility(View.GONE);
Log.e(TAG, "Response " + jsonObject.toString());
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
progress.setVisibility(View.GONE);
Log.e(TAG, volleyError);
Util.showToast(activity, "Please try again");
}
});
requestQueue.add(jsonObjectRequest);
} catch (JSONException e) {
progress.setVisibility(View.GONE);
Log.e(TAG, e);
}
catch (Exception e) {
progress.setVisibility(View.GONE);
Log.e(TAG, e);
}
}
}
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(jsonObjectRequest);
You can follow this :http://www.androidhive.info/2014/05/android-working-with-volley-library-1/
StringRequest request = new StringRequest(Method.POST,
"post url",
new ResponseListener() {
#Override
public void onResponse(String response) {
Log.d("response", response);
} catch (Exception e) {
e.printStackTrace();
}
}
}, new ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("response","error");
}
}) {
// post params
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("param", "param");
return params;
}
};
//2.Use your custom volley manager send request or like this
Volley.newRequestQueue(mCtx).add(request);

Categories

Resources