gpt4 book ai didi

java - 使用检索到的 HTTP URL 填充 ListView

转载 作者:行者123 更新时间:2023-12-02 02:13:46 25 4
gpt4 key购买 nike

我正在尝试在 android 上创建一个搜索栏,其中的 URL 是从 azure blob 存储中获取的。我想在搜索查询后显示视频列表。但我做不到。

下面是代码以及错误

class SearchMovies extends AsyncTask<String, String, ArrayList<String>> {

String query;
String url;
String urlOne;
String itemOne;
ArrayList<String> list;
ArrayAdapter<String> adapter;
Context context;
ListView listView;
@Override
protected void onPostExecute(ArrayList<String> s) {


Log.i("###",s.toString());

runOnUiThread(new Runnable() {
@Override
public void run() {

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("###", "BEFORE");
itemOne = (String) parent.getItemAtPosition(position); //null
Log.i("###AFTER", itemOne);
}
});

}
});

/* Intent intent = new Intent(MainActivity.this,Video.class);
intent.setData(Uri.parse("https://moviestarstorage.blob.core.windows.net/action-movies/ant.mp4"));
startActivity(intent);*/
/*Intent intent = new Intent(context,Video.class);
intent.setData(Uri.parse(itemOne));
startActivity(intent);*/
super.onPostExecute(s);
}

public SearchMovies(String passQuery, ListView passListView, Context passContext) {
this.query = passQuery;
this.context = passContext;
this.listView = passListView;



}

@Override
protected ArrayList<String> doInBackground(String... params) {


try {

HttpHandler httpHandler = new HttpHandler();
Log.i("###URLFromAPI", "Entered");
url = "http://13.71.112.116/?keyword=" + query; // This is the API call
String jsonstr = httpHandler.makeServiceCall(url); //
Log.i("###URLFromAPI", "Entered");
JSONObject jsonObject = new JSONObject(jsonstr);
urlOne = jsonObject.getString("urls"); // urls are being fetched and logged on the screen

String[] urlArray = urlOne.split(","); //seperating the url
list = new ArrayList<String>();

for(int i=0;i<3;i++)
{
list.add(i,urlArray[i]);
Log.i("###FROMLOOP",list.get(i));
}

adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_expandable_list_item_1,list);
listview.setAdapter(adapter);
/*runOnUiThread(new Runnable() {
//String item;
@Override
public void run() {

adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_expandable_list_item_1,list);
Log.i("###","hello");
listview.setAdapter(adapter);
Log.i("###","hello");
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("###","BEFORE");
itemOne = (String) parent.getItemAtPosition(position);
Log.i("###AFTER", itemOne);
}
});


}
});*/


/* Log.i("###",urlOne);
if (urlOne == "") {
Log.i("###", "URL is null");
}
else
Log.i("###URLFromAPI", urlOne.toString());*/
}
catch (Exception e){
e.printStackTrace();
}

return list;
}

}

下面是错误。

com.base.vrushali.collapsingtoolbar W/System.err: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 03-31 11:50:37.005 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7988) 03-31 11:50:37.005 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1392) 03-31 11:50:37.005 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at android.view.ViewGroup.invalidateChild(ViewGroup.java:5426) 03-31 11:50:37.005 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at android.view.View.invalidateInternal(View.java:14959) 03-31 11:50:37.006 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at android.view.View.invalidate(View.java:14923) 03-31 11:50:37.006 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at android.view.View.invalidate(View.java:14907) 03-31 11:50:37.006 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at android.widget.AbsListView.resetList(AbsListView.java:2791) 03-31 11:50:37.006 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at android.widget.ListView.resetList(ListView.java:592) 03-31 11:50:37.006 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at android.widget.ListView.setAdapter(ListView.java:533) 03-31 11:50:37.006 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at com.base.vrushali.collapsingtoolbar.MainActivity$SearchMovies.doInBackground(MainActivity.java:356) 03-31 11:50:37.007 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at com.base.vrushali.collapsingtoolbar.MainActivity$SearchMovies.doInBackground(MainActivity.java:282) 03-31 11:50:37.007 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:305) 03-31 11:50:37.007 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237) 03-31 11:50:37.007 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 03-31 11:50:37.007 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 03-31 11:50:37.007 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 03-31 11:50:37.007 26602-27033/com.base.vrushali.collapsingtoolbar W/System.err: at java.lang.Thread.run(Thread.java:762) 03-31 11:51:36.672 26602-26602/com.base.vrushali.collapsingtoolbar W/IInputConnectionWrapper: finishComposingText on inactive InputConnection 03-31 11:51:36.795 26602-26602/com.base.vrushali.collapsingtoolbar W/IInputConnectionWrapper: getCursorCapsMode on inactive InputConnection 03-31 11:51:36.865 26602-26602/com.base.vrushali.collapsingtoolbar W/IInputConnectionWrapper: getCursorCapsMode on inactive InputConnection 03-31 11:51:36.958 26602-26602/com.base.vrushali.collapsingtoolbar W/IInputConnectionWrapper: getSelectedText on inactive InputConnection 03-31 11:51:36.959 26602-26602/com.base.vrushali.collapsingtoolbar W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection 03-31 11:51:36.960 26602-26602/com.base.vrushali.collapsingtoolbar W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection 03-31 11:52:04.413 1678-8196/? W/ActivityManager: Duplicate finish request for ActivityRecord{947dd63d0 u0 com.base.vrushali.collapsingtoolbar/.MainActivity t22054 f}

最佳答案

runOnUiThreadonPostExecute 中不需要,仅当您从 doInBackground 更新 View 时才需要

protected void onPostExecute(ArrayList<String> s) {
super.onPostExecute(s);

Log.i("###",s.toString());
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("###", "BEFORE");
itemOne = (String) parent.getItemAtPosition(position); //null
Log.i("###AFTER", itemOne);
}
});


}

如果您要在 doInBackground 中更新 View

@Override
protected ArrayList<String> doInBackground(String... params) {


try {

HttpHandler httpHandler = new HttpHandler();
Log.i("###URLFromAPI", "Entered");
url = "http://13.71.112.116/?keyword=" + query; // This is the API call
String jsonstr = httpHandler.makeServiceCall(url); //
Log.i("###URLFromAPI", "Entered");
JSONObject jsonObject = new JSONObject(jsonstr);
urlOne = jsonObject.getString("urls"); // urls are being fetched and logged on the screen

String[] urlArray = urlOne.split(","); //seperating the url
list = new ArrayList<String>();

for(int i=0;i<3;i++)
{
list.add(i,urlArray[i]);
Log.i("###FROMLOOP",list.get(i));
}

runOnUiThread(new Runnable() {
//String item;
@Override
public void run() {

adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_expandable_list_item_1,list);
Log.i("###","hello");
listview.setAdapter(adapter);
Log.i("###","hello");
});


/* Log.i("###",urlOne);
if (urlOne == "") {
Log.i("###", "URL is null");
}
else
Log.i("###URLFromAPI", urlOne.toString());*/
}
catch (Exception e){
e.printStackTrace();
}

return list;
}

关于java - 使用检索到的 HTTP URL 填充 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49585042/

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