2014-03-30 37 views
-1

//如何將url傳遞給json並在android中顯示對listview的響應,如何在listview中顯示 public class MainActivity擴展活動{如何將網址傳遞給JSON並在Android中顯示對listview的響應,我如何在listview中顯示

@SuppressLint("NewApi") 
    @TargetApi(Build.VERSION_CODES.GINGERBREAD) 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
     InputStream is = null; 
     StrictMode.setThreadPolicy(policy); 
     InputStream inputStream = null; 
      String result = ""; 
     DefaultHttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://api.learn2crack.com/android/jsonos/"); 
     try { 
      HttpResponse httpresponse = httpclient.execute(httppost); 
      HttpEntity httpEntity = httpresponse.getEntity(); 
       is = httpEntity.getContent(); 
     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch blockt 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 


} 
+0

我無法理解烏爾問題的URL JSON手段?您是否將json對象傳遞給服務器? – KOTIOS

+0

我想發送一個請求到該網址後,我想以列表視圖格式顯示json響應 – user3114723

+0

好吧你可以編碼URL在json – KOTIOS

回答

0

您需要解析來自服務器的響應,然後使用它來顯示ListView。

做到這一點的最好方法是解析JSON數據並創建一個ArrayList,稍後可以傳遞給ListView的適配器。

關於這個非常相同的話題,有一個很好的tutorial (with full working code)

段從the tutorial

private class GetContacts extends AsyncTask<Void, Void, Void> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      // Showing progress dialog 
      pDialog = new ProgressDialog(MainActivity.this); 
      pDialog.setMessage("Please wait..."); 
      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 
        contacts = jsonObj.getJSONArray(TAG_CONTACTS); 

        // looping through All 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 address = c.getString(TAG_ADDRESS); 
         String gender = c.getString(TAG_GENDER); 

         // Phone node is JSON Object 
         JSONObject phone = c.getJSONObject(TAG_PHONE); 
         String mobile = phone.getString(TAG_PHONE_MOBILE); 
         String home = phone.getString(TAG_PHONE_HOME); 
         String office = phone.getString(TAG_PHONE_OFFICE); 

         // 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_NAME, name); 
         contact.put(TAG_EMAIL, email); 
         contact.put(TAG_PHONE_MOBILE, mobile); 

         // 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_NAME, TAG_EMAIL, 
          TAG_PHONE_MOBILE }, new int[] { R.id.name, 
          R.id.email, R.id.mobile }); 

      setListAdapter(adapter); 
     } 

    }