I have created an activity in which i insert some records into a mysql database. I declared a global variable named lastInsertId. When i try to println the variable inside the onResponse method, works fine but when i try to println outside the method returns null. I need to use this variable also outside the method. What can be done?
Here is my code:
String insertUrl = "http://localhost/file.php";
String lastInsertId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
StringRequest request = new StringRequest(Request.Method.POST, insertUrl, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
lastInsertId = response.toString();
System.out.println(lastInsertId); // returns the lastInsertId
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<String, String>();
// parameters
return parameters;
}
};
requestQueue.add(request);
System.out.println(lastInsertId); // get's null
}
Thanks!
They are different results because although you are firing your request to your local HTTP server, your last println fires BEFORE you set the lastInsertId.
You need to think multi-threaded. Your HTTP request is running in the background and the UI thread is continuing. So the order of execution is not in the order the code appears in your sample.
Request queue created
Request created
Request added to queue (presumably starting HTTP call also)
Prints out lastInsertId (null)
lastInsertId is set from your response
Prints out lastInsertId
user a setter method of your variable inside onResponse
This is happening because the lastInsertId is getting declared, but never initialized,
then the StringRequest request is using a callback through anonym interfaces to print the value, that is happening asynchronous way, but your code is going forward and tryinh to print the value before that happens.
you don net extra setters o getters, you need to validate / that the string is not empty, OR only print its value inside that callack.
I figure it out. I answered this question after about a year, beacuse i saw that this post had a few hundred visitor. Hope my answer will help other feature visitors to get data out from onResponse method. Here is the code:
String insertUrl = "http://localhost/file.php";
String lastInsertId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
StringRequest request = new StringRequest(Request.Method.POST, insertUrl, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
lastInsertId = response.toString();
System.out.println(lastInsertId); // returns the lastInsertId
callback.onSuccess(lastInsertId);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<String, String>();
// parameters
return parameters;
}
};
requestQueue.add(request);
}
public interface VolleyCallback{
void onSuccess(ArrayList<Data> dataArrayList);
}
And this is the code we need inside the Activity.
public void onResume(){
super.onResume();
getString(new VolleyCallback(){
#Override
public void onSuccess(String result){
System.out.println(result); // returns the value of lastInsertId
}
});
}
Related
I can get very strange issue in my project. I can get the response from volley over the internet and after reposne I want to store it in sharedpref, but issue is that when I get the response and showed up within resonse function then it shows correct data, but when I used to save it outside the response function sharedpref it gives 0. I declared the string public and top of the class but got no luck. Am very strange whats the issue.
SharedPreferences savelogin = getSharedPreferences("login",MODE_PRIVATE);
final SharedPreferences.Editor slogin = savelogin.edit();
String url = "https://datafinderdatabase.xyz/dfapi/FetchId.php?username="+fuser;
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
userid = response.toString();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toasty.error(getApplicationContext(), "Network Issue", Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(stringRequest);
slogin.putString("username",fuser);
slogin.putString("password",fpass);
slogin.putString("userid",userid);
slogin.commit();
This is because your call to the API is asynchronous. Therefore, the result you get back may be processed after that function finishes. To solve this, you can take a Interface approach, explained here for a similar issue:
wait until firebase retrieves data
OR: you need to save the variable to the shared preferences inside the onResponse function.
These:
slogin.putString("username",fuser);
slogin.putString("password",fpass);
slogin.putString("userid",userid);
slogin.commit();
must be inside this:
#Override
public void onResponse(String response) {
userid = response.toString();
//here
}
I have a fragment that sends a Get request to server multiple times but with different arguments expecting different results.
This function accepts arguments and sends request successfully but there is a problem:
First execution of this function returns null but second execution returns first execution's result! and the problem is in execution sequence:
I added some toast comments to clarify: "Toast A" is in get request function and "Toast B" is after the function call. I figured out that "Toast A" is being executed AFTER "Toast B".
As you know by now, I am new to Java and answering this question helps me understand it better. My real question is not about sending Get request. I just want to know How java handles function's execution sequence. Thanks for guiding me.
This is how I call request function:
Map<String, String> header = new HashMap<>();
header.put("user_id", "123");
String[] responseString = get_request(header);
//Toast B
Here is my request function:
public String[] get_request(final Map<String, String> header) {
final String[] responseString = new String[1];
StringRequest stringRequest = new StringRequest(Request.Method.POST, getString(R.string.server) + "query.php", new Response.Listener<String>()
{
#Override
public void onResponse(String response) {
responseString[0] = response;
//Toast A
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError e) {
responseString[0] = "";
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
return header;
}
};
Volley.newRequestQueue(getActivity().getApplicationContext()).add(stringRequest);
return responseString;
}
I am trying to make a web-service call using volley and printing the response in the logcat. But I don't know why I am not getting response. Not even any error message.
Below is my code. I know I am missing something. Please correct me.
private void syncData() {
mProgressDialog.showProgressDialog("Initializing Please Wait...");
RequestQueue requestQueue = Volley.newRequestQueue(SalesDashboard.this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, SYNC_DATA_SALES, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
mProgressDialog.dismissProgressDialog();
Log.e("TAG", response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
mProgressDialog.dismissProgressDialog();
Log.e("TAG", error.toString());
}
}){
#Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String , String> params = new HashMap<>();
String userrole = mSessionManagement.getLoggedInUser().get(SESSION_USER_ROLE);
params.put("branch", mSessionManagement.getSelectedBranch());
params.put("staff_id", mSessionManagement.getLoggedInUser().get(SESSION_EMP_ID));
params.put("user_role", mSessionManagement.getLoggedInUser().get(SESSION_USER_ROLE));
params.put("user_dept", mSessionManagement.getLoggedInUser().get(SESSION_DEPT_IDS));
params.put("principal", mSessionManagement.getLoggedInUser().get(SESSION_PRINC_IDS));
if (userrole.equals(ADMIN) || userrole.equals(COUNTRY_MANAGER)) {
params.put("user_div", mSessionManagement.getSelectedDivision());
} else {
params.put("user_div", mSessionManagement.getLoggedInUser().get(SESSION_DIV_IDS));
}
return super.getParams();
}
};
requestQueue.add(stringRequest);
}
I am not receiving any error message also or any kind of exception. I have checked the webservice by hitting it on browser the response is displayed correctly on the browser but not able to fetch it in android.
Why you call super.getParams() on return instead params? Maybe it turn wrong on your request. Then try as follow
#Override
protected Map<String, String> getParams() throws AuthFailureError {
...
return params;
}
Bonus: use getMessage instead do toString() your error object
Print the stringRequest and check the url and cross verify with ur url
Before that check have u added Internet permissions in manifest file.
I'm using this simple code :
JsonObjectRequest req = new JsonObjectRequest(Request.Method.PUT, url, null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
delegate.postNotificationSucced();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Log.d(TAG, ""+error.getMessage()+","+error.toString());
delegate.postNotificationFailed((APIErrors) error);
}
}){
#Override
public Map<String, String> getParams() throws AuthFailureError {
Map<String,String> headers = new HashMap<String, String>();
headers.put("Content-Type","application/x-www-form-urlencoded");
headers.put("state", Boolean.toString(isChecked));
return headers;
}
#Override
protected VolleyError parseNetworkError(VolleyError volleyError){
APIErrors error = null;
if(volleyError.networkResponse != null && volleyError.networkResponse.data != null){
error = new APIErrors(new String(volleyError.networkResponse.data));
}
return error;
}
};
When I use the Request.Method.GET or POST instead of PUT, the getParams() method is called but not with the PUT method.
My problem is that my API requires a PUT method.
How can I pass my parameters using the PUT method.
Edit :
With the PUT method the getHeaders() method is called, so can I pass my arguments through it ?
Solution :
In my case I solved the problem changing my JsonObjecRequest to StringRequest like suggested by #ishmaelMakitla. And now I pass inside getParams().
I noticed that you are not using the response object for anything - so
perhaps change the request from JsonObjectRequest to a StringRequest -
remember that you then need to change Response.Listener()
to Response.Listener() and the onResponse(JSONObject response)
to onResponse(String response)....shout if you need further help. I
hope this leads to some workable solution.
The reason is that with JsonObjectRequest, parameters must be pass through the third parameters of the constructor
jsonRequest - A JSONObject to post with the request. Null is allowed
and indicates no parameters will be posted along with request.
you can read the doc for more informations.
Since you are not using the response object for anything - you should change the request from JsonObjectRequest to a StringRequest - remember that you then need to change Response.Listener<JSONObject>() to Response.Listener<String>() and the onResponse(JSONObject response) to onResponse(String response).... here is StringRequest Gist I created - you can simply "plug-and-play" in your code and replace the JsonObjectRequest code.
String name;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getData();
this.setText(name);
}
private void getData(){
name = object.getString("name");
}
I'm just making my line of code short, anyway the idea is already there. So on the method getData() I'm retrieving data from the web. I am able to test and was able to get the result however when I put the name on the onCreate() it gives me a null value. How to do some work arounds on this?
public class Testing extends NavigationLiveo{
String name;
#Override
public void onInt(Bundle savedInstanceState) {
getData();
Log.wtf("name0", name);
this.username.setText(name);
}
private void getData() {
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
StringRequest sr = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
#Override
public void onResponse(String s) {
try {
name = object.getString("name");
Log.wtf("name1", name);
} catch (Exception e) {
Log.wtf("testing1", e.getMessage());
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError volleyError) {
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("user", user_pid);
return params;
}
};queue.add(sr);
}
}
Once again, I've made my coding simple. I can really get the data from the web server. It's just that whenever the I'll try to access the name on the onInt(Bundle savedInstanceState) it returns a null value. The log says it all as well. "name1" returns a value while "name0" returns null.
The only explanation is that the object.getString("name") returns null, or you are setting it to null somewhere afterwards. By the way, when retreiving data from the web, you should use AsyncTask, for more details refer here.
Can you put the getData() code?
I think that since the method uses AsyncTask or so to download the data from URL asynchronously. That means the method hasn't completed downloading, the setText(name) code already executed. So it displays null.