2014-03-30 4 views
-1

// json에 URL을 전달하고 android에서 listview에 응답하는 방법, listview에 표시하는 방법 public class MainActivity는 활동 {당신은 서버의 응답을 분석 한 다음 목록보기 표시를 사용할 필요가json에 URL을 전달하고 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

내가 JSON 수단을 통해 UR 질문 URL을 이해할 수 없었다? json 객체를 서버에 전달하고 있습니까? – KOTIOS

+0

그 url에 대한 요청을 보내고 싶습니다. listview 형식으로 json 응답을 표시하려고합니다. – user3114723

+0

ok json에서 URL을 인코딩 할 수 있습니다. – KOTIOS

답변

0

을 확장합니다.

이 작업을 수행하는 가장 좋은 방법은 JSON 데이터를 구문 분석하고 나중에 ListView의 어댑터로 전달할 수있는 ArrayList를 만드는 것입니다.

매우 동일한 주제에 대해 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); 
     } 

    } 
관련 문제