Ver Mensaje Individual
  #1  
Viejo 29/04/15, 00:48:00
Array

[xs_avatar]
portal47 portal47 no está en línea
Miembro del foro
 
Fecha de registro: jul 2012
Mensajes: 215
Modelo de smartphone: Nexus 4

Leer JSON Wordpress

Hola, en la app que estoy haciendo quiero que aparesca los proximos eventos de la empresa y estos para no estar actualizando la app al agregar un evento quiero obtener los datos de la web, pero no me muestra ningun dato.

Estoy obteniendo los datos desde aqui:
http://cmx.org.mx/json/get_posts

pero si intento con este si aparecen los datos:
http://api.androidhive.info/contacts

este es mi codigo
Código:
package info.androidhive.jsonparsing;

import info.androidhive.jsonparsing.R;
import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class MainActivity extends ListActivity {

    private ProgressDialog pDialog;

    // URL to get contacts JSON
    private static String url = "http://cmx.org.mx/json/get_posts/";

    // JSON Node names
    private static final String TAG_CONTACTS = "get_posts";
    private static final String TAG_ID = "id";
    private static final String TAG_TITLE = "slug";

    // contacts JSONArray
    JSONArray get_posts = null;

    // Hashmap for ListView
    ArrayList<HashMap<String, String>> contactList;

    @override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        ListView lv = getListView();

        // Listview on item click listener
        lv.setOnItemClickListener(new OnItemClickListener() {

            @override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // getting values from selected ListItem

                String title = ((TextView) view.findViewById(R.id.title))
                        .getText().toString();

                // Starting single contact activity
                Intent in = new Intent(getApplicationContext(),
                        MainActivity.class);
                in.putExtra(TAG_TITLE, title);
                startActivity(in);

            }
        });

        // Calling async task to get json
        new GetContacts().execute();
    }

    /**
     * Async task class to get json by making HTTP call
     * */
    private class GetContacts extends AsyncTask<Void, Void, Void> {

        @override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Espera...");
            pDialog.setCancelable(false);
            pDialog.show();

        }

        @override
        protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            ServiceHandler sh = new ServiceHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

            Log.d("Response: ", "> " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);

                    // Getting JSON Array node
                    get_posts = jsonObj.getJSONArray(TAG_CONTACTS);

                    // looping through All Contacts
                    for (int i = 0; i < get_posts.length(); i++) {
                        JSONObject c = get_posts.getJSONObject(i);

                        String id = c.getString(TAG_ID);
                        String title = c.getString(TAG_TITLE);

                        // tmp hashmap for single contact
                        HashMap<String, String> contact = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        contact.put(TAG_ID, id);
                        contact.put(TAG_TITLE, title);

                        // adding contact to contact list
                        contactList.add(contact);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }

            return null;
        }

        @override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();
            /**
             * Updating parsed JSON data into ListView
             * */
            ListAdapter adapter = new SimpleAdapter(
                    MainActivity.this, contactList,
                    R.layout.list_item, new String[] { TAG_TITLE }, new int[] { R.id.title});

            setListAdapter(adapter);
        }

    }

}
Responder Con Cita