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);
Related
I'm having a problem with java volley when I click login
private void Login(){
String url = "api.matraindonesia.com/login";
RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,new Response.Listener<String>() {
#Override
public void onResponse(String response) {
if (response.trim().equals("success")){
Toast.makeText(getApplicationContext(),"Login Successfully!",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),"Login Failed!",Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),"this error:"+ error.toString(),Toast.LENGTH_LONG).show();
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("email",etEmail.getText().toString().trim());
params.put("password",etPassword.getText().toString().trim());
return super.getParams();
}
};
requestQueue.add(stringRequest);
}
com.android.volley.VolleyError: java.lang.RuntimeException: Bad URL
you need to use http:// or https:// at the beginning or the url
String url = "http://api.matraindonesia.com/login";
or if your server is on SSL
String url = "https://api.matraindonesia.com/login";
Edit (after your comment) :
typically MethodNotAllowedHttpException happen when, route method is not match.
Suppose you define POST request route file, but you sending GET Request to the route.
Having trouble on VolleyRequest getting always error response when loading it onCreate. I want to do is when the fragment loads. but when I try it, the Logcat gives me an error 400. on this Java class, i have another function that has sending data to API. I just copied my code :). here is the code that getting a error response.
String url = "MYLINK.com";
try {
RequestQueue queue = Volley.newRequestQueue(getActivity());
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(getContext(), "Successful send pending data", Toast.LENGTH_SHORT).show();
String qu = ("update tickets set is_send = '1' where ticket_tick_no = '" + ticket_tick_no_delayed + "'");
sqldb.execSQL(qu);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), "Error Response here", Toast.LENGTH_SHORT).show();
Log.e("------", String.valueOf(error.networkResponse.statusCode));
}
}
) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("control_no", trip_no_delayed);
params.put("trip_no", ticket_control_no_delayed);
params.put("ticket_no", ticket_tick_no_delayed);
params.put("ticket_datetime", ticket_datetime_delayed);
params.put("ticket_kmfrom", ticket_kmfrom_delayed);
params.put("ticket_kmto", ticket_kmto_delayed);
params.put("ticket_placefrom", ticket_placefrom_delayed);
params.put("ticket_placeto", ticket_placeto_delayed);
params.put("amount", ticket_amount_delayed);
params.put("discount", ticket_discount_delayed);
params.put("trans_type", transaction_type_delayed);
params.put("passenger_type", passenger_type_delayed);
params.put("lat", ticket_lat_delayed);
params.put("long", ticket_long_delayed);
params.put("device_serial", device_serial_delayed);
return params;
}
};
queue.add(postRequest);
} catch (Exception e) {
e.printStackTrace();
}
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'm trying to send JSON parameter but server is receiving them as nulls values,
i tried to request it from Postman and it works perfect, i don't know what the problem is with volley i followed the instructions here but it didn't make sense
here is my code
String url = "http://10.10.10.166:8080/SystemManagement/api/Profile/Login";
JSONObject jsonObject=new JSONObject();
try {
jsonObject.put("user_id","Test user name");
jsonObject.put("user_password","test password");
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println(jsonObject.toString());
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest( Request.Method.POST, url, jsonObject,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Toast.makeText(Login.this, response.toString(),Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
System.out.println(error.toString());
}
});
//add request to queue
queue.add(jsonObjectRequest);
I faced same problem, solved by overriding the getParams() method
Here is my login request using Volley.
private void loginRequeset() {
showpDialog();
StringRequest jsonObjReq = new StringRequest(Request.Method.POST, Constants.LOGIN_URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(Login.this, response.toString(),Toast.LENGTH_SHORT).show();
hidepDialog();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
// hide the progress dialog
hidepDialog();
}
})
{
#Override
protected Map<String, String> getParams() {
Map<String, String> signup_param = new HashMap<String, String>();
signup_param.put(Constants.USERNAME, _emailText.getText().toString().trim());
signup_param.put(Constants.PASSWORD, _passwordText.getText().toString().trim());
return signup_param;
}
};
// Adding request to request queue
queue.getInstance().addToRequestQueue(jsonObjReq);
}