简体   繁体   中英

Fetching data from JSON is not returning data in list form. progress bar keeps running

I am not getting any error but when i am trying to get data from json and view it in list form, the progress bar keeps running.. though it works successfully when i try to show data in text view form.

Code:main class

 private static String url = "http://api.androidhive.info/contacts/";    
    private static final String TAG_CONTACTS = "contacts";;
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "name";
    private static final String TAG_EMAIL = "email";
    private static final String TAG_GENDER = "gender";      
    JSONArray contacts = null;     
    ArrayList <HashMap <String,String>> contactList;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_of_occupation);

    contactList = new ArrayList <HashMap <String,String>>();
    ListView lv= getListView();
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // TODO Auto-generated method stub
             String name = ((TextView) view.findViewById(R.id.identity)).getText().toString();
             Intent in = new Intent(getApplicationContext(), SingleContactActivity.class);
                in.putExtra(TAG_NAME, name);
                startActivity(in);
        }
    });        
    Button b1= (Button) findViewById(R.id.button1);
    b1.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
             new ProgressTask().execute();
        }
    });     
}
public class ProgressTask extends AsyncTask<Void, Void, Void> {

    private ProgressDialog Dialog= new ProgressDialog(ListOfOccupation.this);       
private Context context;
    protected void onPreExecute() {

          Dialog.setMessage("Please wait..");
          Dialog.show();
      } 

      protected void onPostExecute() {
          if (Dialog.isShowing()) {
              Dialog.dismiss();
          }           
          ListAdapter adapter = new SimpleAdapter(context, contactList, R.layout.list_item,
             new String[] {TAG_ID }, new int[] {R.id.identity});              
       setListAdapter (adapter);          
      } 

    protected Void doInBackground(Void... params) {
        ServiceHandler sh = new ServiceHandler();
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

        if (jsonStr !=null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);
                 contacts = jsonObj.getJSONArray(TAG_CONTACTS);

                    for (int i = 0; i < contacts.length(); i++) {
                        JSONObject c = contacts.getJSONObject(i);

                        String id = c.getString(TAG_ID);
                        String name = c.getString(TAG_NAME);
                        String email = c.getString(TAG_EMAIL);                        
                        String gender = c.getString(TAG_GENDER);

               HashMap<String, String> contact = new HashMap<String, String>();                         
                        contact.put(TAG_ID, id);
                        contact.put(TAG_NAME, name);
                        contact.put(TAG_EMAIL, email);
                        contact.put(TAG_GENDER, gender);                            
                        contactList.add(contact);
                    }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }               
        } 
        // TODO Auto-generated method stub
        return null;
    }       
}

Code:servicehandler.java

public class ServiceHandler {
 public static final int GET = 0;
static String response = null;  
 public ServiceHandler() {             

 }   
public String makeServiceCall(String url, int method) {
    try {    
     DefaultHttpClient httpClient = new DefaultHttpClient();
     HttpGet httpget = new HttpGet(url);
     HttpResponse httpResponse = httpClient.execute(httpget);
     HttpEntity entity = httpResponse.getEntity();
     response = EntityUtils.toString(entity);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }        
    return response;

} }

after putting void:

 03-31 18:30:53.406: E/AndroidRuntime(1159): FATAL EXCEPTION: main
03-31 18:30:53.406: E/AndroidRuntime(1159): java.lang.NullPointerException
03-31 18:30:53.406: E/AndroidRuntime(1159):     at android.widget.SimpleAdapter.<init>(SimpleAdapter.java:85)
03-31 18:30:53.406: E/AndroidRuntime(1159):     at com.example.listofoccupation.ListOfOccupation$ProgressTask.onPostExecute(ListOfOccupation.java:77)
03-31 18:30:53.406: E/AndroidRuntime(1159):     at com.example.listofoccupation.ListOfOccupation$ProgressTask.onPostExecute(ListOfOccupation.java:1)
03-31 18:30:53.406: E/AndroidRuntime(1159):     at android.os.AsyncTask.finish(AsyncTask.java:631)
03-31 18:30:53.406: E/AndroidRuntime(1159):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-31 18:30:53.406: E/AndroidRuntime(1159):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
03-31 18:30:53.406: E/AndroidRuntime(1159):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-31 18:30:53.406: E/AndroidRuntime(1159):     at android.os.Looper.loop(Looper.java:137)
03-31 18:30:53.406: E/AndroidRuntime(1159):     at android.app.ActivityThread.main(ActivityThread.java:4745)
03-31 18:30:53.406: E/AndroidRuntime(1159):     at java.lang.reflect.Method.invokeNative(Native Method)
03-31 18:30:53.406: E/AndroidRuntime(1159):     at java.lang.reflect.Method.invoke(Method.java:511)
03-31 18:30:53.406: E/AndroidRuntime(1159):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-31 18:30:53.406: E/AndroidRuntime(1159):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-31 18:30:53.406: E/AndroidRuntime(1159):     at dalvik.system.NativeStart.main(Native Method)

the signature of you onPostExecute is wrong. In your case it has to be

 protected void onPostExecute(Void v)

and you'll have realized immediately if you had the @Override annotation.

Edit, you did not initialized the context member. Change

 ListAdapter adapter = new SimpleAdapter(context, contactList, R.layout.list_item,
             new String[] {TAG_ID }, new int[] {R.id.identity});         

with

  ListAdapter adapter = new SimpleAdapter(ListOfOccupation.this, contactList, R.layout.list_item,
             new String[] {TAG_ID }, new int[] {R.id.identity});         

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM