gpt4 book ai didi

java - 多个 Volley 请求的问题

转载 作者:太空宇宙 更新时间:2023-11-04 09:36:01 33 4
gpt4 key购买 nike

每当我想根据用户输入执行 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);
}
}

最佳答案

现在我明白你的问题了。正在发生的事情是数据需要一些时间来加载。因此,请使用进度条之类的东西,并更改其在 Response.Listener 和 Response.ErrorListener 中的可见性。为了使这项工作正常工作,请移动 onClickListener 内的 rq.add(objreq); 行,并在该行之前将进度条的可见性更改为可见。

示例

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 内即可。

关于java - 多个 Volley 请求的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56516440/

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