多个排球请求出现问题

时间:2019-06-09 16:37:02

标签: java android android-volley

每当我要基于用户输入执行Volley请求时,都必须按两次按钮,而不是仅一次单击按钮来获得请求的响应。

我使用了wait()函数,但仍然遇到相同的问题,并且应用程序已损坏,而我希望应用程序能够正常工作。

这是我到目前为止所达到的:

   String URL="https://apifootball.com/api/?action=get_countries&APIkey=b4c74bf2fcf3937f783b752649018a42a3b1bde9d5d7c03ff36f61fc06c00c77";
        RequestQueue rq= Volley.newRequestQueue(this);

        JsonArrayRequest objreq= new JsonArrayRequest(

                Request.Method.GET,
                URL,
                null,
                new Response.Listener<JSONArray>()
                {
                    @Override
                    public void onResponse(JSONArray response) {
                        try {

                            Log.e("result:",response.get(0).toString());
                            JSONObject obj;
                            for (int count = 0; count < response.length(); count++) {
                                obj = response.getJSONObject(count);
                                String name = obj.getString("country_name");
                                Log.e("Country:",name);
                                send(name,true);
                                //  Team t=new Team(2,"mki");
                                //x.insertTeam(t);
                                //so on
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener(){

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("rest response",error.toString());
                    }
                }
        );
        rq.add(objreq);

        btn_send_message.setOnClickListener(new View.OnClickListener() {
            ChatModel model;
public void onClick(View v) {
                String text = editText.getText().toString();
else if(text.contains("result"))
                {
                    ChatModel model = new ChatModel(text, true); // user send message
                    list_chat.add(model);

                    String result="";
                    String head2Head;
                    String input[] = text.split(" ");
                    String[] arr=null ;
                    DBAdapter dbAdapter=new DBAdapter(x);
                    try{
                        result=dbAdapter.getResultfromDB("Bristol City","Reading");

                    }catch (Exception e)
                    {
                        result="error";
                    }

                    if(result.equals("error")==true) {

                        APIAdapter ap = new APIAdapter();
                        head2Head = ap.getResult("Bristol City", "Reading", "kjkn", getApplicationContext());
                        finres = head2Head;
                        Log.e("headto",head2Head);
                        arr = head2Head.split("\n");
                        }
                    model = new ChatModel("First team:"+arr[0]+"\nSecond team:"+arr[1]+"\n"+"Date:"+arr[2], false);
                    list_chat.add(model);
                }
}

1 个答案:

答案 0 :(得分:0)

现在,我知道您的问题了。发生的事情是数据需要花费时间来加载。因此,请使用进度条之类的东西,并在Response.ListenerResponse.ErrorListener中更改其可见性。为了使其正常工作,请将行rq.add(objreq);移到onClickListener内,并在此行之前将进度条的可见性更改为可见。

示例

Layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/mainParentRel"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:background="@drawable/grad_bg_2"
        android:isScrollContainer="true"
        android:scrollbars="vertical">

        <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:fillViewport="true"
            android:scrollbars="vertical">

         <!-- You can use any thing here
              Put all your previous buttons edittext etc here.
              You can replace the scrollview with any layout
              Or You can completely remove the scrollview and 
              directly put your views here. -->

        </ScrollView>

        <!-- This is the progress bar layout. Always remember to set its visibility to GONE.-->

        <RelativeLayout
            android:id="@+id/progressRelLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:visibility="gone">
            <ImageView
                android:id="@+id/company_logo_progress"
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:layout_centerHorizontal="true"
                android:adjustViewBounds="true"
                android:contentDescription="@string/company_logo"
                android:scaleType="fitCenter"
                android:src="@drawable/company_logo" />
            <ProgressBar
                android:id="@+id/progressBar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/company_logo_progress"
                android:layout_marginTop="5dp"
                android:layout_centerHorizontal="true"
                android:theme="@style/WhiteAccent"/>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/progressBar"
                android:text="Loading..."
                android:textColor="@color/white"
                android:textSize="17dp"
                android:layout_centerHorizontal="true"/>
        </RelativeLayout>

    </RelativeLayout>

Example.java

RelativeLayout progressRL; 
//Inside onCreate()
progressRL= findViewById(R.id.progressRelLayout);

//Do rest of your stuff
   String URL="https://apifootball.com/api/?action=get_countries&APIkey=b4c74bf2fcf3937f783b752649018a42a3b1bde9d5d7c03ff36f61fc06c00c77";
        RequestQueue rq= Volley.newRequestQueue(this);

        JsonArrayRequest objreq= new JsonArrayRequest(

                Request.Method.GET,
                URL,
                null,
                new Response.Listener<JSONArray>()
                {
                    @Override
                    public void onResponse(JSONArray response) {
progressRL.setVisibility(View.GONE);
                        try {

                            Log.e("result:",response.get(0).toString());
                            JSONObject obj;
                            for (int count = 0; count < response.length(); count++) {
                                obj = response.getJSONObject(count);
                                String name = obj.getString("country_name");
                                Log.e("Country:",name);
                                send(name,true);
                                //  Team t=new Team(2,"mki");
                                //x.insertTeam(t);
                                //so on
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener(){

                    @Override
                    public void onErrorResponse(VolleyError error) {
progressRL.setVisibility(View.GONE);

                        Log.e("rest response",error.toString());
                    }
                }
        );


        btn_send_message.setOnClickListener(new View.OnClickListener() {
            ChatModel model;
public void onClick(View v) {

rq.add(objreq);
progressRL.setVisibility(View.VISIBLE);



                String text = editText.getText().toString();
else if(text.contains("result"))
                {
                    ChatModel model = new ChatModel(text, true); // user send message
                    list_chat.add(model);

                    String result="";
                    String head2Head;
                    String input[] = text.split(" ");
                    String[] arr=null ;
                    DBAdapter dbAdapter=new DBAdapter(x);
                    try{
                        result=dbAdapter.getResultfromDB("Bristol City","Reading");

                    }catch (Exception e)
                    {
                        result="error";
                    }

                    if(result.equals("error")==true) {

                        APIAdapter ap = new APIAdapter();
                        head2Head = ap.getResult("Bristol City", "Reading", "kjkn", getApplicationContext());
                        finres = head2Head;
                        Log.e("headto",head2Head);
                        arr = head2Head.split("\n");
                        }
                    model = new ChatModel("First team:"+arr[0]+"\nSecond team:"+arr[1]+"\n"+"Date:"+arr[2], false);
                    list_chat.add(model);
                }
}


这样做可能会导致错误。只需移动在Response.Listener中加载数据后将发生变化的事物即可。