gpt4 book ai didi

android - 循环内的 Volley 请求

转载 作者:行者123 更新时间:2023-11-30 01:05:56 25 4
gpt4 key购买 nike

好的,这是我的第一篇文章,因为我已经达到了研究的极限。我尝试运行 3 个请求,每个请求在 for 循环中都有不同的 url(我也尝试过 while 和 if)并且代码运行完美,直到它到达请求队列。现在的事情是:在请求队列之后我想得到响应,然后才再次进行 for 循环,但这似乎不是程序想要做的,因为在我添加请求队列之后,类又回去了到 for 循环,并继续整个函数直到结束,直到最后我才得到 3 个后续响应,这让我很生气,因为我每次都想要一个请求。我以前做过很多请求,并认为我知道如何解决这个问题,但我做不到 =s代码:

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 请求是异步的,所以当响应返回时,循环在一段时间前就结束了。要执行请求队列,它必须是递归的 - 运行下一个请求调用以响应上一个请求。

关于android - 循环内的 Volley 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38879010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com