Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.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/4/json/13.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
android中不显示json url_Android_Json - Fatal编程技术网

android中不显示json url

android中不显示json url,android,json,Android,Json,您好,在我的应用程序中,我使用jsonurl来显示带有图像的文本。我想解析图像和文本,但它显示了错误,我的应用程序崩溃了。有人能帮我吗 CustomizedListView.java: public class CustomizedListView extends Activity { // All static variables static final String URL = "http://indianpoliticalleadersmap.com/a

您好,在我的应用程序中,我使用jsonurl来显示带有图像的文本。我想解析图像和文本,但它显示了错误,我的应用程序崩溃了。有人能帮我吗

CustomizedListView.java:

 public class CustomizedListView extends Activity {
        // All static variables
        static final String URL = "http://indianpoliticalleadersmap.com/android/DemoSchool/json/json_item.php";
        // XML node keys
        static final String TAG_SCHEDULES = "veg_food"; // parent node
        static final String TAG_ID = "id";
        static final String TAG_TITLE = "itemname";

        static final String TAG_THUMB_URL = "image";

        ListView list;
        LazyAdapter adapter;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);


            ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

            XMLParser parser = new XMLParser();
            String xml = parser.getXmlFromUrl(URL); // getting XML from URL
            Document doc = parser.getDomElement(xml); // getting DOM element

            NodeList nl = doc.getElementsByTagName(TAG_SCHEDULES);
            // looping through all song nodes <song>
            for (int i = 0; i < nl.getLength(); i++) {
                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();
                Element e = (Element) nl.item(i);
                // adding each child node to HashMap key => value
                map.put(TAG_ID, parser.getValue(e, TAG_ID));
                map.put(TAG_TITLE, parser.getValue(e, TAG_TITLE));

                map.put(TAG_THUMB_URL, parser.getValue(e, TAG_THUMB_URL));

                // adding HashList to ArrayList
                songsList.add(map);
            }


            list=(ListView)findViewById(R.id.list);

            // Getting adapter by passing xml data ArrayList
            adapter=new LazyAdapter(this, songsList);        
            list.setAdapter(adapter);


            // Click event for single list row
            list.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {


                }
            });     
        }   
    }

JSON和XML解析之间存在差异。请看下面的代码。在这里,我将把来自数据库的日期解析为listview

活动类别:

public static class EventFragment extends ListFragment {

        ArrayList<HashMap<String, String>> eventsList;
        private String url_all_events = //url goes here;
        private ProgressDialog pDialog;

        JSONParser jParser = new JSONParser();

        // JSON Node names
        private static final String CONNECTION_STATUS = "success";
        private static final String TABLE_EVENT = "Event";
        private static final String pid = "pid";
        private static final String COL_GROUP = "Group";
        private static final String COL_NAME = "Event_Name";
        private static final String COL_DESC = "Event_Desc";
        private static final String COL_DATE = "Event_Date";
        private static final String COL_TIME = "Event_Time";

        JSONArray Events = null;

        public static final String ARG_SECTION_NUMBER = "section_number";

        public EventFragment() {
        }

        public void onStart() {
            super.onStart();

            eventsList = new ArrayList<HashMap<String, String>>();
            new LoadAllEvents().execute();

            // selecting single ListView item
            ListView lv = getListView();

            // Lauching the Event details screen on selecting a single event
            lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // getting values from selected ListItem
                    String ID = ((TextView) view.findViewById(R.id.pid))
                            .getText().toString();

                    Intent intent = new Intent(view.getContext(),
                            EventDetails.class);
                    intent.putExtra(pid, ID);
                    view.getContext().startActivity(intent);
                }
            });
        }

        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_events,
                    container, false);

            return rootView;
        }

        class LoadAllEvents extends AsyncTask<String, String, String> {

            /**
             * Before starting background thread Show Progress Dialog
             * */
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(getActivity());
                pDialog.setMessage("Just a moment...");
                pDialog.setIndeterminate(true);
                pDialog.setCancelable(true);
                pDialog.show();
            }

            protected String doInBackground(String... args) {
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                // getting JSON string from URL
                JSONObject json = jParser.makeHttpRequest(url_all_events,
                        "GET", params);

                try {
                    // Checking for SUCCESS TAG
                    int success = json.getInt(CONNECTION_STATUS);

                    if (success == 1) {
                        // products found
                        // Getting Array of Products
                        Events = json.getJSONArray(TABLE_EVENT);
                        // looping through All Contacts
                        for (int i = 0; i < Events.length(); i++) {
                            JSONObject evt = Events.getJSONObject(i);

                            // Storing each json item in variable
                            id = evt.getString(pid);
                            group = evt.getString(COL_GROUP);
                            name = evt.getString(COL_NAME);
                            desc = evt.getString(COL_DESC);
                            date = evt.getString(COL_DATE);
                            time = evt.getString(COL_TIME);

                            // creating new HashMap
                            HashMap<String, String> map = new HashMap<String, String>();

                            // adding each child node to HashMap key => value
                            map.put(pid, id);
                            map.put(COL_GROUP, group);
                            map.put(COL_NAME, name);
                            map.put(COL_DESC, desc);
                            map.put(COL_DATE, date);
                            map.put(COL_TIME, time);

                            // adding HashList to ArrayList
                            eventsList.add(map);
                        }
                    } else {
                        // Options are not available or server is down.
                        // Dismiss the loading dialog and display an alert
                        // onPostExecute
                        pDialog.dismiss();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                return null;
            }

            protected void onPostExecute(String file_url) {
                // dismiss the dialog after getting all products
                pDialog.dismiss();
                // updating UI from Background Thread
                getActivity().runOnUiThread(new Runnable() {
                    public void run() {
                        ListAdapter adapter = new SimpleAdapter(getActivity(),
                                eventsList, R.layout.list_item, new String[] {
                                        pid, COL_GROUP, COL_NAME, COL_DATE, COL_TIME },
                                new int[] { R.id.pid, R.id.group, R.id.name, R.id.header,
                                        R.id.title2 });

                        setListAdapter(adapter);
                    }
                });

            }

        }
    }
JSON解析器类

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    // function get json from url
    // by making HTTP POST or GET mehtod
    public JSONObject makeHttpRequest(String url, String method,
            List<NameValuePair> params) {

        // Making HTTP request
        try {

            // check for request method
            if (method == "POST") {
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            } else if (method == "GET") {
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}

希望这有帮助:

xml解析器解析Json?为什么?也许这可以帮助,而不是使用XMLPARSER使用JSONParser…访问这个不错的教程:在我上面的代码中我所犯的错误,请告诉我你使用XMLPARSE获取JSON值……它不有效。否则我会非常乐意为你提供更多的帮助