Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android-volley请求内部循环_Android_Loops_Android Volley_Request Queueing - Fatal编程技术网

android-volley请求内部循环

android-volley请求内部循环,android,loops,android-volley,request-queueing,Android,Loops,Android Volley,Request Queueing,好吧,我的第一篇文章,因为我已经达到了研究的极限。 我尝试运行3个请求,每个请求在for循环中都有一个不同的url(我也尝试了while和if),代码运行良好,直到它到达requestqueue。现在的问题是:在请求队列之后,我想要得到响应,然后再进行for循环,但这似乎不是程序想要做的,因为在我添加requestqueue之后,类返回for循环,并继续整个函数直到结束,最后我只得到3个后续响应,这让我很生气,因为我希望每次都有一个请求。我以前做过很多请求,我想我知道如何解决这个问题,但我不能

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

public类MainActivity扩展了FragmentActivity{
私有字符串totalresults、word、results、server、userAgent、quantity、urly、nome;
私人整数计数器,限制;
编辑文本nome_empresa;
公共文本视图输出;
进展性帕金森病;
ViewPager ViewPager=null;
公共阵列列表半张卡,所有卡;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nome_empresa=(编辑文本)findviewbyd(R.id.nome_empresa);
输出=(TextView)findViewById(R.id.output);
PD=新进度对话框(本);
PD.setMessage(“加载…”);
PD.可设置可取消(假);
//viewPager=(viewPager)findViewById(R.id.pager);
//FragmentManager FragmentManager=getSupportFragmentManager();
//setAdapter(新的MyAdapterAfa(fragmentManager));
}
公共无效vamos(视图){
nome=nome_empresa.getText().toString();
PD.show();
谷歌搜索(nome,200,0);
ListView lv=(ListView)findViewById(R.id.custom\u list\u view);
lv.setAdapter(新阵列适配器)(MainActivity.this,
android.R.layout.simple_list_item_1,所有_卡);
所有卡片=获取卡片(总结果,nome);
PD.解散();
}
public void googlesearch(字符串字、整数限制、整数开始){
单词=单词;
结果=”;
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”;
数量=“100”;
计数器=启动;
极限=极限;
dou_search();
}
公共无效do_search(){

对于(int l=counter;lVolley请求是异步的,因此当响应返回时,循环在一段时间前就结束了。要执行请求队列,它必须是递归的-运行下一个请求调用以响应上一个请求调用。

Volley
异步运行,这意味着它将在自己的线程上运行,而另一个代码继续运行。因此它不会等待de>Volley response
它将再次启动for循环。请尝试在
onResponse
方法中使用
if语句调用
do\u search
以确定要使用的
url
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();
}