Send a text(or string) to server and get a response - android

I want to make a register/login app. I found a few examples but the most of this doesn't work for me. So I will do everything step by step. So first for the login I want to send the Server the username and the password of the user. So here is everything working, but as you can see I didn't send anything to the Server. So, where and how to send the "username" and "password" to the Server.
private void userLogin() {
username = usernameField.getText().toString().trim();
password = passwordField.getText().toString().trim();
RequestQueue queue = Volley.newRequestQueue(this);
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, LOGIN_URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast toast = Toast.makeText(getApplicationContext(), (response), Toast.LENGTH_SHORT);
toast.show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast toast = Toast.makeText(getApplicationContext(), "Anything doesn't work!", Toast.LENGTH_SHORT);
toast.show();
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
}

try the following got if your request is GET request.
private void userLogin() {
username = usernameField.getText().toString().trim();
password = passwordField.getText().toString().trim();
RequestQueue queue = Volley.newRequestQueue(this);
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, LOGIN_URL+"?username="+username+"&password="+password, // here you have to add parameters of webservice
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast toast = Toast.makeText(getApplicationContext(), (response), Toast.LENGTH_SHORT);
toast.show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast toast = Toast.makeText(getApplicationContext(), "Anything doesn't work!", Toast.LENGTH_SHORT);
toast.show();
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
}

You can't send Data with a GET request HTTP body.
For that, you use a POST request.
Then, look at the other StringRequest constructors for the one that has more than 4 parameters for (method, url, onSuccess, onFailure).
OR... I assume you are using a JSON REST API, so you need a JSONObjectRequest

Technically it is not forbidden to use a body in a get request.
It is however a very bad idea. (more info here: HTTP GET with request body)
This is why many http APIs do not allow you to do it.

Related

How to make a synchronous volley StringRequest?

How to make Volley StringRequest should wait for the one response to complete and after completing the first response it should start another request.
you have to do if your first response successfully then call these second method your answers is into your question
Inside onResponse of your StringRquest make the second request you want.
StringRequest stringRequest = new StringRequest(Request.Method.POST, recieveMessageUrl, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
//create second request here
//and add it to queue
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
Note: you will have to make sure that your response is valid otherwise the onErrorResponse will be called.

Pass details to volley inner class

This answer should be simple but I need help.
I add a url call to Volley and on error response I need to know what the URL was that the error is for, I may have 50 different URL's in a single queue for example so I want to know which one of the 50 returned the error.
Here is my code:
public void upload(String passurl) {
StringRequest stringRequest;
// Request a string response
stringRequest = new StringRequest(Request.Method.GET, passurl,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d(TAG,"Send successful");
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//***I want the URL that failed here!
error.printStackTrace();
}
});
stringRequest.setRetryPolicy(new DefaultRetryPolicy(30000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
// Add the request to the queue
stringRequest.setShouldCache(false);
stringRequest.setTag(TAG);
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(this.getApplicationContext());
mRequestQueue.start();
}
mRequestQueue.getCache().clear();
mRequestQueue.add(stringRequest);
}
To expand on #Levon's answer make passurl final for example method(final String passurl)

Volley sending multiple requests Android

I am sending a request to server and it causes errors. I was able to trace the error to the fact that Volley is sending the request more than once. I searched the internet for solutions, I tried all what I came across but none of them seem to solve the problem
Below is my code:
public void btnLogOut(View view) {
final ProgressDialog loading = ProgressDialog.show(this, "Logging Out", "Please wait...", false, false);
//cover.setVisibility(View.GONE);
String token = dbHelper.getAuth().getString(0);
String IP = helperFunctions.getAppUrl();
final String url = IP + "/deregister?token=" + token+ "&appVersion=" + versionCode;
JsonObjectRequest sr = new JsonObjectRequest(Request.Method.POST, url,null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
loading.dismiss();
logOut.LogOutUser();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
if (VolleyErrorHelper.getMessage(error, Settings.this).equalsIgnoreCase("401")){
logOut.MakeUserLogin();
}else{
cover.setVisibility(View.VISIBLE);
}
//VolleyLog.e("Deregister GCM", "Error: " + error.getMessage());
loading.dismiss();
Toast.makeText(Settings.this, "Process not completed, try again!", Toast.LENGTH_LONG).show();;
}
});
sr.setRetryPolicy(new DefaultRetryPolicy(0, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(sr);
}
Try this:
sr.setRetryPolicy(new DefaultRetryPolicy(0, 0, DefaultRetryPolicy.DEFAULT_TIMEOUT_MS));
I used on the POST requests so it can do a retry

Volley pass a response outside of the class

I want to pass a response outside of my classes (many classes)
public static void userLocation()
{
RequestQueue queue = Volley.newRequestQueue(context);
String url = "http://www.jobdiagnosis.com/iphone/userlocation.php";
StringRequest dr = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>()
{
#Override
public void onResponse(String response) {
// response
//Toast.makeText(context, ""+response, Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
// error.
// Toast.makeText(getApplicationContext(), "error"+error, Toast.LENGTH_LONG).show();
Log.d("error", ""+error);
}
}
);
queue.add(dr);
}
Please suggest how I can pass a response outside of the class
In volly its difficult to return response outside the class.because request on server run in background and if we return value followed by queue.add(url) then it will return null.So there is no solution till now.Thanks!!

method return null value

Here's my layout code;
static String userLocation(String url) {
RequestQueue queue = Volley.newRequestQueue(context);
// String url = "http://www.jobdiagnosis.com/iphone/userlocation.php";
StringRequest dr = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>()
{
#Override
public void onResponse(String response) {
// response
res=response;
//edit_location.setText(response);
// Toast.makeText(context, response, Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
// error.
Toast.makeText(getApplicationContext(), "error"+error, Toast.LENGTH_LONG).show();
Log.d("error", ""+error);
}
}
);
queue.add(dr);
return res;
}
When I toast the response on same method then response prints fine
and when I call method and print return statement then res prints null.
Any help is appreciated. Thank you in advance.
Please suggest me how we can share response within a one or more activity
And sorry for my bad English..
this is an asynchronous request, the volley processes the request in background thread and delivers the response in onResponse()
if you're looking for synchronous response try this
Try to declare res like global variable. Not return value. Because you didn't know, when request will finished. And use this global variable.

Categories

Resources