2013-11-27 2 views
1

데이터베이스에서 데이터를 읽는 방법을 설명하는 예제를 찾았지만 질문이 있습니다. 이 예는 두 개의 C 럼으로 구성된 테이블에 대한 메소드를 설명합니다. 많은 수의 열을 가지고 작업 할 수 있도록 무엇을 어디서 순서를 변경해야합니까? 코드의 부 :Android 및 MySQL에서 읽기

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_lala); 
listView = (ListView) findViewById(R.id.listView1); 
accessWebService(); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
// Inflate the menu; this adds items to the action bar if it is present. 
getMenuInflater().inflate(R.menu.main, menu); 
return true; 
} 

// Async Task to access the web 
private class JsonReadTask extends AsyncTask<String, Void, String> { 
@Override 
protected String doInBackground(String... params) { 
HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost(params[0]); 
    try { 
HttpResponse response = httpclient.execute(httppost); 
jsonResult = inputStreamToString(
    response.getEntity().getContent()).toString(); 
} 

catch (ClientProtocolException e) { 
e.printStackTrace(); 
} catch (IOException e) { 
e.printStackTrace(); 
} 
return null; 
} 

private StringBuilder inputStreamToString(InputStream is) { 
String rLine = ""; 
StringBuilder answer = new StringBuilder(); 
BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 

try { 
    while ((rLine = rd.readLine()) != null) { 
    answer.append(rLine); 
    } 
} 

catch (IOException e) { 
    // e.printStackTrace(); 
    Toast.makeText(getApplicationContext(), 
    "Error..." + e.toString(), Toast.LENGTH_LONG).show(); 
    } 
    return answer; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
    ListDrwaer(); 
    } 
}// end async task 

public void accessWebService() { 
    JsonReadTask task = new JsonReadTask(); 
    // passes values for the urls string array 
    task.execute(new String[] { url }); 
} 

// build hash set for list view 
public void ListDrwaer() { 
    List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>(); 

    try { 
    JSONObject jsonResponse = new JSONObject(jsonResult); 
    JSONArray jsonMainNode = jsonResponse.optJSONArray("PenzGTU"); 

    for (int i = 0; i < jsonMainNode.length(); i++) { 
    JSONObject jsonChildNode = jsonMainNode.getJSONObject(i); 
    String number = jsonChildNode.optString("Number"); 
    String name = jsonChildNode.optString("FIO"); 
    String outPut = "№ " + number + " " + "FIO:" + name; 
    employeeList.add(createEmployee("employees", outPut)); 
    } 
    } catch (JSONException e) { 
    Toast.makeText(getApplicationContext(), "Error" + e.toString(), 
    Toast.LENGTH_SHORT).show(); 
    } 

    SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList, 
    android.R.layout.simple_list_item_1, 
    new String[] { "employees" }, new int[] { android.R.id.text1 }); 
    listView.setAdapter(simpleAdapter); 
} 
//*****MAYBE HERE?***** 
private HashMap<String, String> createEmployee(String number, String name) { 
    HashMap<String, String> employeeNameNo = new HashMap<String, String>(); 
    employeeNameNo.put(number, name); 
    return employeeNameNo; 
} 
} 
+1

아무 곳이나. MySQL에서 데이터를 가져 오는 역할을하는 PHP 스크립트를 변경해야하므로 테이블에 더 많은 열이 있으면 리턴 된 JSON이 더 커지고 복잡해집니다. – Sajmon

답변

0

글쎄, 난 당신이 MySQL의에서 광산을하는 REST 기반의 웹 응용 프로그램을 소비 읽을 수 있습니다, 당신이 앱은 잘 불려갑니다 출력 JSON을 읽습니다.

이렇게 말하면 DB에서 2 열 이상을 말했기 때문에 원하는 결과를 반환하도록 webapp 로직을 수정해야합니다.

그런 다음 JSON 구문 분석기를 수정하여 위에서 붙여 넣은 코드의 출력에서 ​​값을 가져와야합니다.

try { 
    JSONObject jsonResponse = new JSONObject(jsonResult); 
    JSONArray jsonMainNode = jsonResponse.optJSONArray("PenzGTU"); 

    for (int i = 0; i < jsonMainNode.length(); i++) { 
     JSONObject jsonChildNode = jsonMainNode.getJSONObject(i); 
     String number = jsonChildNode.optString("Number"); 
     String name = jsonChildNode.optString("FIO"); 
     String outPut = "№ " + number + " " + "FIO:" + name; 

     .... 

     //For example 'surname' if your webapp returned this DB Column 
     String surname = jsonChildNode.optString("Surname"); 

     .... 

     employeeList.add(createEmployee("employees", outPut)); 
    } 
} catch (JSONException e) { 
    Toast.makeText(getApplicationContext(), "Error" + e.toString(), 
    Toast.LENGTH_SHORT).show(); 
} 

희망적입니다. 의심 스럽다면 그냥 물어보십시오.

행운을 빈다.