I have made a splash screen where im syncing data from server to sqlite. I have to make few requests (lets say 5 request for 5 tables), and then the splash screen goes off and main activity starts. Please share some ideas as Im clueless how to achieve this. I have left no stone unturned in stackoverflow but could not find a perfect workaround.
Add Volley https://stackoverflow.com/questions/16659620/volley-android-networking-library?rq=1
Then you can make calls from volley
JsonObjectRequest jsonObjReq = new JsonObjectRequest(com.android.volley.Request.Method.POST, Url__, ipObject,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
listener.resultJson(null, response);
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
listener.resultJson(null, response); will get the responce from server and save this responce to your Sqlite.Finally close your Spalsh .
Related
I have url that has a json Array which contains a large amount of data.
I call the url with volly jsonArrayRequest like this
public void makeJsonArrayReq(){
showProgressDialog();
JsonArrayRequest req = new JsonArrayRequest(Const.URL_IPD_ADMITED,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d("response ================", response.toString());
textView.setText(response.toString());
hideProgressDialog();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Error", "Error: " + error.getMessage());
textView.setText("Error Occurs ");
hideProgressDialog();
}
});
AppController.getInstance().addToRequestQueue(req, "array");
}
My devise shows progressDialog for some time after that time it hang out the app then after a few minute it shows the response in the texview but it very lengthy process and it same when internet connection off and extract from volley cache. How can I handle the URL in my apps?
You need to change your api a bit to support pagination. There are several pagination techniques available, you have to choose one that suites your case.
In Android you can save the last fetched pageNumber(if that is what your API returns as page identifier) and your api should accept this variable in request (mostly through query params)
http://43.255.22.123:3000/android/mis/get/ipdAdmitPatMd?pageId=1
And in the next request it should update the pageId to 2.
Since the question is too broad hence providing exact code solution is not possible therefore I have explained the concept.
I am using multiple volley request in TabLayout, Tab1(1st req) and Tab2(2nd req).Problem is both requesting at the same time,which made App froze during swapping in TabLayout from Tab1 to Tab2.I want a specific delay,requesting the second.I hope you understood the scenario,feel free to ask more suggestions/clarification.
I highly doubt that the app is freezing due to sending a request through volley.
Anyway if want to send the second request after the first request, put it in the response listener of first request like
JsonObjectRequest firstRequest = new JsonObjectRequest(Request.Method.GET, url, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
//Send 2nd request here
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//Handle Error case here
}
});
mRequestQueue.add(firstRequest);
If you want to send the second request after x seconds after switching to the tab 2, you can use postDelayed where you can detect the tab is changed (e.g., onResume if you are using fragments as tabs) like
new android.os.Handler().postDelayed(
new Runnable() {
public void run() {
//Your second request goes here
}
},
1000); //Will execute after one second.
For some advanced usages you can also configure request queue size of volley. For your specific scenario you can set number of maximum parallel request to one as described here.
I'm trying to use Volley to execute multiple HTTP request where each one of them relies on the result of the previous one, what is the best option as a design?
1-Firing the next request in the onResponse callback of the previous request?
2-Writing some coordinator class that have callbacks that get called in the onResponse method of a request and fires the next request
skeleton code for the second option
coodrinator = new Coordinator();
JsonObjectRequest firstRequest = new JSONObjectRequest(Request.Method.GET,firstURL),new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
//handle the responsee
coordinator.onFirstRequestRecieved();
}
},
errorListener);
private void doSecondRequest(){
JsonObjectRequest secondRequest = new JSONObjectRequest(Request.Method.GET,secondURL),new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
//handle the responsee
coordinator.onSecondRequestRecieved();
}
},
errorListener);
}
private class Coordinator{
public void onFirstReequestRecieved(){
doSecondRequest();
}
public void onSecondRequestRecieved(){
//do Something
}
}
If first request response parameters are needed for making second request and so on then you can go for synchronous way. That can be achieved by making second request in onResponse on First request can there is no good or bad practise for it.
The thing is volley is asynchronous and request what is added in the queue execute without depending on other request and we are going to make it synchronous request and it can be achieved by many ways seeing your requirement.
I want to send two different requests and handle two different responses in one Activity using Volley library.
My activity implements onResponseListener, so i have only one onResponse method and both responses are handled here. As they are completely same in structure i cant tell which is which.
How can i tell from which request i have received the response so i can handle them differently? Is there a way to "tag" a request or something like that?
I could set some kind of check variable, e.g. boolean firstRequestIsSent when i send the request, and then check it in the onResponse method, but its a pretty ugly solution.
Many thanks
Instead of implementing onResponse as part of the class, you can instantiate a new Response.Listener with the request. This way you will have a separate listener for each request.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener() {
#Override
public void onResponse(String response) {
// individual response here
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// error here
}
});
I've seen answer to both of these questions, however, when I tried to put them together, I couldn't make it work. The problem itself is pretty simple: I want to get a string from one site and use it in a post request. That means I can only make the post request after I've finished parsing the GET request. The main ideas I'm using are these ones:
How to return response header field to main method using Google Volley for HTTP GET request in Android / Java?
Can I do a synchronous request with volley?
However the synchronous request is blocked and doesn't go on, and the first one is Async.
I believe this to be a simple thing to do, but still, I haven't be able to do it...
Thanks for any help!
Why not do something like this:
// send first request
requestQueue.add(firstRequest, null, new Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
// ** code to parse response **
// send second request
requestQueue.add(secondRequest, null, new Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
// ** code to parse response **
}
}, new ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// ** code to handle errors **
}
}));
}
}, new ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// ** code to handle errors **
}
}));