使用检索到的HTTP URL填充列表视图

时间:2018-03-31 06:45:12

标签: java android http httpconnection

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

以下是代码和错误

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:只有创建视图层次结构的原始线程才能触及其视图。   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:在非活动InputConnection上的finishComposingText   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:非活动InputConnection上的getSelectedText   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:ActivityRecord的重复完成请求{947dd63d0 u0 com.base.vrushali.collapsingtoolbar / .MainActivity t22054 f}

1 个答案:

答案 0 :(得分:2)

runOnUiThread中不需要

onPostExecute只需要从View

更新doInBackground时才需要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

如果您要更新@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; }

中的视图
import pandas as pd

dates = ['28/02/2018', '01/03/2018', '16/03/2018', '17/05/2018'] 

s = pd.to_datetime(pd.Series(dates), format='%d/%m/%Y')
s.index = s.dt.to_period('m')
s = s.groupby(level=0).size()

s = s.reindex(pd.period_range(s.index.min(), s.index.max(), freq='m'), fill_value=0)
print (s)
2018-02    1
2018-03    2
2018-04    0
2018-05    1
Freq: M, dtype: int64

s.plot.bar()