2012-07-13 2 views
1

나는 목록보기로 가져 가고 싶지만 그렇게하는 법을 모르며 자습서의 메서드는 분명하지 않습니다. 어떤 사람이 밖으로 나를 도울 수android 어떻게 목록보기 json 배열을 얻으려면

희망 ...

당신에게

<?php 
include "db_config.php"; 

$sql=mysql_query("SELECT`uname` FROM `user`"); 
while($row=mysql_fetch_assoc($sql)) $output[]=$row; 
print(json_encode($output)); 
mysql_close(); 
?> 

활동을 위의 결과를

이 제공

PHP 감사는 하나를 제외한 목록 정정 보기에만 pwd 열을 보여줍니다 .... 왜 그게?

package hopscriber.com; 


public class Menu extends Activity { 

    TextView result; 
    Intent intent; 

    // JSON Node names 
    private static final String TAG_Name = null; 
    private static final String TAG_Pass = null; 

    // contacts JSONArray 
    JSONArray contacts = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_menu); 

     // Name of the User 
     result = (TextView) findViewById(R.id.result); 
     intent = getIntent(); 
     String Naming = intent.getStringExtra("name1"); 
     result.setText("Hey " + Naming); 

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

     try { 
      HttpParams params = new BasicHttpParams(); 
      HttpConnectionParams.setSoTimeout(params, 0); 
      HttpClient httpClient = new DefaultHttpClient(params); 

      // prepare the HTTP GET call 
      HttpGet httpget = new HttpGet("http://hopscriber.com/log.php"); 
      // get the response entity 
      HttpEntity entity = httpClient.execute(httpget).getEntity(); 

      if (entity != null) { 
       // get the response content as a string 
       String response = EntityUtils.toString(entity); 
       // consume the entity 
       entity.consumeContent(); 

       // When HttpClient instance is no longer needed, shut down the 
       // connection manager to ensure immediate deallocation of all 
       // system resources 
       httpClient.getConnectionManager().shutdown(); 

       // return the JSON response 
       ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>(); 

       JSONArray jsonArray = new JSONArray(response.trim()); 
       if (jsonArray != null) { 
        for (int i = 0; i < jsonArray.length(); i++) { 
         JSONObject object = (JSONObject) jsonArray.get(i); 

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

         map.put(TAG_Name, object.getString("uname")); 
         map.put(TAG_Pass, object.getString("pwd")); 

         contactList.add(map); 

        } 
       } 

       ListAdapter adapter = new SimpleAdapter(this, contactList, 
         R.layout.menu_list_row, new String[] { TAG_Name, 
           TAG_Pass }, new int[] { R.id.LR_Name, 
           R.id.LR_date }); 
       list.setAdapter(adapter); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     // Launching new screen on Selecting Single ListItem 
     list.setOnItemClickListener(new OnItemClickListener() { 

      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
        long arg3) { 
       Toast.makeText(getBaseContext(), "Booyah", Toast.LENGTH_SHORT) 
         .show(); 
      } 
     }); 
    } 

} 

답변

1

이 링크를 따라

try { 
     HttpParams params = new BasicHttpParams(); 
     HttpConnectionParams.setSoTimeout(params, 0); 
     HttpClient httpClient = new DefaultHttpClient(params); 

     //prepare the HTTP GET call 
     HttpGet httpget = new HttpGet(urlString); 
     //get the response entity 
     HttpEntity entity = httpClient.execute(httpget).getEntity(); 

     if (entity != null) { 
      //get the response content as a string 
      String response = EntityUtils.toString(entity); 
      //consume the entity 
      entity.consumeContent(); 

      // When HttpClient instance is no longer needed, shut down the connection manager to ensure immediate deallocation of all system resources 
      httpClient.getConnectionManager().shutdown(); 

      //return the JSON response 
      JSONArray jsonArray = new JSONArray(response.trim()); 
      if(jsonArray != null) { 
       String[] unames = new String[jsonArray.length()]; 
       for(int i = 0 ; i < jsonArray.length() ; i++) { 
        JSONObject object = (JSONObject) jsonArray.get(i); 
        unames[i] = object.getString("uname"); 
       } 
       ListAdapter adapter = new SimpleAdapter(this, contactList, 
        R.layout.menu_list_row, unames, new int[] { R.id.LR_Name }); 
       list.setAdapter(adapter); 
      } 
     } 
    }catch (Exception e) { 
     e.printStackTrace(); 
    } 

ListView에 결과를 추가하려면 .. 내가 그것을 테스트하지 않았다,이 시도,하지만 난 다른 데이터와 동일한 코드를 사용하고 있는데 작동 http://www.mkyong.com/android/android-listview-example/ String[] unames

+0

감사합니다. 그리고 hasel이 아닌 경우 목록에 추가하는 방법도 알려주세요. – Loshi

+0

당신은'ListView'를 의미합니까? 그렇다면 대답을 업데이트했습니다 .. – Nermeen

+0

yah buddy 나는 게시물을 다시 업데이트했습니다 ... 목록보기를 추가했지만 빈 목록을 보여줍니다 ... 왜 그런가요? – Loshi