如何使用ListActivity列出android中数据库的post?

我正在尝试从数据库中检索数据并将它们列在android中,但我很困惑如何做到这一点所以我使用了listavtivity和其他一些PHP代码,但它没有作为应用程序工作。 崩溃我只是想问这是正确的方法吗?

php代码:

 

java代码:

 package com.listposts; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.ListActivity; import android.content.Intent; import android.net.ParseException; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class SeeRequests extends ListActivity { InputStream is = null; StringBuilder sb = null; String result = null; String classes[]; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); getData(); setListAdapter(new ArrayAdapter(SeeRequests.this, android.R.layout.simple_list_item_1, classes)); } private void getData() { // TODO Auto-generated method stub try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost( "http://sa3dnishokran.netne.net/getrequests.php"); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); } catch (Exception e) { Log.e("log_tag", "Error: " + e.toString()); } try { BufferedReader reader = new BufferedReader(new InputStreamReader( is, "iso-8859-1"), 8); sb = new StringBuilder(); String line = "0"; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); result = sb.toString(); } catch (Exception e) { Log.e("log_tag", "Error converting result " + e.toString()); } try { JSONArray jArray = new JSONArray(result); JSONObject json_data = null; for (int i = 0; i < jArray.length(); i++) { json_data = jArray.getJSONObject(i); classes[i] = json_data.getString("firstname"); } } catch (JSONException e1) { Toast.makeText(getBaseContext(), "Wrong ID or Password", Toast.LENGTH_LONG).show(); } catch (ParseException e1) { e1.printStackTrace(); } } @Override protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); String cheese = classes[position]; try { Class ourClass = Class.forName("com.sa3dnishokran." + cheese); Intent ourIntent = new Intent(AddProfileActivity.this, ourClass); startActivity(ourIntent); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } 

要在ListView显示数据,您需要使用ListAdapter 。 这是一个可能对您有帮助的教程: http : //www.ezzylearning.com/tutorial.aspx?tid = 1763429

这种方法对我来说似乎很好。 另一种方法可以实现SOAP Web Service而不是JSON,但是,在这种情况下,您需要手动解析SOAP响应或使用其他工具/库来解析SOAP响应。 这些工具目前还不够成熟。 我个人更喜欢JSON方式,你的应用程序崩溃的原因似乎是Android框架特有的一些其他问题与你从数据库中获取数据的方法无关。