Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.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
Java 试图在android上解析json以列出视图,但出现错误_Java_Android_Android Layout_Android Listview - Fatal编程技术网

Java 试图在android上解析json以列出视图,但出现错误

Java 试图在android上解析json以列出视图,但出现错误,java,android,android-layout,android-listview,Java,Android,Android Layout,Android Listview,我正在制作一个应用程序,从我的网站获取json,并将其解析到android上的listview 我使用http请求获取json,然后使2个数组网站不包含所有网站名称和链接以包含链接。我希望listview显示网站名称,然后单击,在浏览器中打开网站 谁能帮帮我吗?我已经尽力了。请帮我解决这个问题,或者告诉我另一种方法,谢谢 尝试获取json并将其解析为下面的listView是我的代码: public class GetWebsiteList extends AsyncTask<String,

我正在制作一个应用程序,从我的网站获取json,并将其解析到android上的listview

我使用http请求获取json,然后使2个数组网站不包含所有网站名称和链接以包含链接。我希望listview显示网站名称,然后单击,在浏览器中打开网站

谁能帮帮我吗?我已经尽力了。请帮我解决这个问题,或者告诉我另一种方法,谢谢

尝试获取json并将其解析为下面的listView是我的代码:

public class GetWebsiteList extends AsyncTask<String, String, String> {
        // Creating JSON Parser object

        ArrayList<HashMap<String, String>> productsList;
        String websites[]=new String[10];
        String links[]=new String[10];
        // url to get all products list
        private String url_all_products = "http://androidtest.cu.cc/getwebsites.php";

        // JSON Node names
        private static final String TAG_SUCCESS = "success";
        private static final String TAG_WEBSITES = "websites";
        private static final String TAG_SNO = "sno";
        private static final String TAG_NAME = "name";
        private static final String TAG_LINK = "link";
        // products JSONArray
        JSONArray products = null;
        // Progress Dialog
        private ProgressDialog pDialog;

        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(mainpage.this);
            pDialog.setMessage("Loading Website List.Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... arg0) {
// Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            JSONParser jParser = new JSONParser();
            // getting JSON string from URL
            JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
            //ArrayAdapter adapter = ArrayAdapter.
            // Check your log cat for JSON reponse
            Log.d("nitrek All Products: ", json.toString());

            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);
                String suc;
                switch(success)
                {
                    case 1:
                        suc="True";
                        break;
                    case 0:
                        suc="False";
                        break;
                    default:
                        suc="unkonwn";
                        break;
                }

                if (success == 1) {
                    // products found
                    // Getting Array of Products
                    Log.d(" nitrek success",suc);
                    products = json.getJSONArray(TAG_WEBSITES);
                    // looping through All Products
                    Log.d(" nitrek websites", products.toString());
                    for (int i = 0; i < products.length(); i++) {
                        JSONObject c = products.getJSONObject(i);
                        // Storing each json item in variable
                        String id = c.getString(TAG_SNO);
                        String name = c.getString(TAG_NAME);
                        String link= c.getString(TAG_LINK);
                        Log.d("nitrek website",id+name+link);
                       // websites[i]=new String();
                        websites[i]=name;
                        links[i]=link;
                        Log.d("nitrek web",websites[i]+links[i]);
                        /* creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();
                        // adding each child node to HashMap key => value
                       try {
                           map.put(TAG_SNO, id);
                           map.put(TAG_NAME, name);
                           productsList.add(map);
                       }
                       catch (Exception e)
                       {
                           e.printStackTrace();
                       }
                        // adding HashList to ArrayList
                        */
                    }
                } else {
                    // no products found
                    // Launch Add New product Activity
                    Toast.makeText(mainpage.this,"no website found", Toast.LENGTH_LONG).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(final String result) {
            pDialog.dismiss();

            runOnUiThread(new Runnable() {
                public void run() {
                    /**
                     * Updating parsed JSON data into ListView
                     */
                    //ListAdapter adapter = new SimpleAdapter(mainpage.this, productsList, R.layout.listitem, new String[]{TAG_SNO, TAG_NAME},new int[]{R.id.sno, R.id.name});
                    // updating listview
                    try {
                        //ArrayAdapter<String> adapter = new ArrayAdapter<String>(mainpage.this, R.layout.listitem, R.id.name, websites);

                        final ArrayList<String> list = new ArrayList<String>();
                        for (int i = 0; i < websites.length; ++i) {
                            list.add(websites[i]);
                        }
                        final StableArrayAdapter adapter = new StableArrayAdapter(mainpage.this, R.layout.listitem, list);
                        //ArrayAdapter<String> ad = new ArrayAdapter<String>(mainpage.this, R.layout.listitem, websites);
                        ListView lv = (ListView) findViewById(R.id.list);
                        lv.setAdapter(adapter);
                        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                //Intent intent = new Intent(this, homepage.class);
                                count = 0;
                                String url = links[position];
                                Toast.makeText(mainpage.this, "Opening: " + url, Toast.LENGTH_SHORT).show();
                                Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                                //notify_test(url);
                                startActivity(i);

                            }
                        });
                    } catch (Exception e) {
                        Log.d("error list nitrekerror", "below");
                        e.printStackTrace();
                    }
                    Toast.makeText(mainpage.this, result, Toast.LENGTH_LONG).show();

                    int i = 0;
                    while (i < websites.length && i < 3) {
                        Toast.makeText(mainpage.this, websites[i] + " " + links[i], Toast.LENGTH_LONG).show();
                        i++;
                    }
                }
            });
        }
    }
    private class StableArrayAdapter extends ArrayAdapter<String> {

        HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

        public StableArrayAdapter(Context context, int textViewResourceId,
                                  List<String> objects) {
            super(context, textViewResourceId, objects);
            for (int i = 0; i < objects.size(); ++i) {
                mIdMap.put(objects.get(i), i);
            }
        }

        @Override
        public long getItemId(int position) {
            String item = getItem(position);
            return mIdMap.get(item);
        }

        @Override
        public boolean hasStableIds() {
            return true;
        }

    }

您忘了覆盖ArrayAdapter之外的getView。并同步viewHolder以获得更高的性能。你可以读这个

您需要重写此方法以膨胀视图,否则适配器将创建任何视图并引发null异常

覆盖的示例:

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
   // Get the data item for this position
   Object data = yourTabObj[position] 
   // Check if an existing view is being reused, otherwise inflate the view
   if (convertView == null) {
      convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_listView, parent, false);
   }
   // Lookup view for data population
   TextView title = (TextView) convertView.findViewById(R.id.title);
   title.setText(data.name);
   // Return the completed view to render on screen
   return convertView;
}


理解后,考虑viewHolder以提高性能。

您已声明大小为10的网站数组,但根据作为响应的网站数量,您仅使用2-3个条目填充,因此将整个数组传递给arrayadapter时,您将获得一个空指针异常。因此,您获得了网站数量收到响应并动态声明仅该大小的数组。

不明白您想说什么?已经阅读了该页我完成了我的回答LookThank@timo,但这不是问题所在
 @Override
public View getView(int position, View convertView, ViewGroup parent) {
   // Get the data item for this position
   Object data = yourTabObj[position] 
   // Check if an existing view is being reused, otherwise inflate the view
   if (convertView == null) {
      convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_listView, parent, false);
   }
   // Lookup view for data population
   TextView title = (TextView) convertView.findViewById(R.id.title);
   title.setText(data.name);
   // Return the completed view to render on screen
   return convertView;