Okay, my first post because I've reached the limits of researching. I try to run 3 requests, each with a different url inside a for loop (I've tried also with while and if) and the code runs perfectly, untiiiiil it gets to the requestqueue. Now the thing is: after the request queue I want to get the response, and only then make the for loop again, but that does not seem to be what the program wants to do, because after I add the requestqueue, the class goes back to the for loop, and continues the whole function until the end, and only in the end I get 3 followed responses, and it is pissing me off, because I want the requests one at each time. I've done many requests before, and thought I knew how to work this out, but I can't =s Code:
public class MainActivity extends FragmentActivity {
private String totalresults, word, results, server, userAgent, quantity, urly, nome;
private int counter,limit;
EditText nome_empresa;
public TextView output;
ProgressDialog PD;
ViewPager viewPager=null;
public ArrayList half_cards, all_cards;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nome_empresa = (EditText) findViewById(R.id.nome_empresa);
output = (TextView) findViewById(R.id.output);
PD = new ProgressDialog(this);
PD.setMessage("Loading....");
PD.setCancelable(false);
//viewPager = (ViewPager) findViewById(R.id.pager);
//FragmentManager fragmentManager = getSupportFragmentManager();
//viewPager.setAdapter(new MyAdapterAlfa(fragmentManager));
}
public void vamos(View view) {
nome = nome_empresa.getText().toString();
PD.show();
googlesearch(nome, 200, 0);
ListView lv = (ListView) findViewById(R.id.custom_list_view);
lv.setAdapter(new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, all_cards));
all_cards = get_cards(totalresults, nome);
PD.dismiss();
}
public void googlesearch(String worde, int limite, int start) {
word = worde;
results = "";
totalresults = "";
server = "www.google.com";
userAgent = "(Mozilla/5.0 (Windows; U; Windows NT 6.0;en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6";
quantity = "100";
counter = start;
limit = limite;
do_search();
}
public void do_search() {
for(int l=counter; l<=limit; l+=100){
urly = "http://" + server + "/search?num=" + quantity + "&start=" + Integer.toString(counter) + "&hl=en&meta=&q=%40\"" + word + "\"";
RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest strReq = new StringRequest(Request.Method.GET, urly, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
half_cards = get_cards(response, word);
all_cards.addAll(half_cards);
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error: " + error.getMessage());
}
});
requestQueue.add(strReq);
}
}
public ArrayList get_cards(String ola, String ole){
myparser rawres = new myparser(ola, ole);
return rawres.cards();
}
}
Volley request is asynchronous so when response comes back loop is over some time ago. To do request queue it must be recursive - run next request call in response of previous one.