I am sending a simple request using Volley everything is fine but after the networking code, some part of code is not executed.
Here's the code:
private String sendMessage(String paymentId){
final StringBuilder response = new StringBuilder();
//url
String url = "testUrl";
//Simple Request
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String res) {
Log.d("pay","MesaageID :"+res);
response.append(res);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("pay",""+error.toString());
}
});
//This Log.d statement is not executed.
Log.d("test","working!"+response.toString());
queue.add(stringRequest);
return response.toString();
}
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);
//URL
String CategoryUrl = "http://lazurd.com/shop/api/rest/custom/categories/";
StringRequest request = new StringRequest(Request.Method.GET,CategoryUrl,new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("CODDE",response );
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(MainActivity.this,"ERROR",Toast.LENGTH_LONG).show();
}
});
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(request);
BasicNetwork.performRequest: Unexpected response code 500 for http://lazurd.com/shop/api/rest/custom/categories/
final TextView mTextView = (TextView) findViewById(R.id.text);
...
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://lazurd.com/shop/api/rest/custom/categories/";
// 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);
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've been using the volley library for consuming services with my android app. I noticed something weird,when I try to post a json object to server, I have to make a response Listener of type JSONObject. But my server returns a String, Success or failure. Is it possible to write a JsonObjectRequest which waits for a String response? this is What I have written. The service runs correctly, but the onErrorResponse is triggered after successful post due to cannot converge String to JSON
JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, url, jsonBody,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Toast.makeText(StartingActivity.this,response.toString(),Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Toast.makeText(StartingActivity.this,"That didn't work",Toast.LENGTH_SHORT).show();
}
});
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(jsonRequest);
Use StringRequest instead of JsonObjectRequest. it will return response in string Like this:
StringRequest stringRequest = new StringRequest(Request.Method.POST,url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.e(TAG,response);
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}) {
#Override
public byte[] getBody() throws AuthFailureError {
//use this to post data.
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
//use this for headers.
}
};
stringRequest.setRetryPolicy(new RetryPolicy() {
#Override
public int getCurrentTimeout() {
return 50000;
}
#Override
public int getCurrentRetryCount() {
return 50000;
}
#Override
public void retry(VolleyError error) throws VolleyError {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(context);
requestQueue.add(stringRequest);
The jsonBody your sending to server it should be in String.
JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, url, jsonBody.toString(),
I hope this will help you. Thanks!
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