0

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

php代码:

<?php
    //turn off error reporting
    error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);

    //Create fields for the database
    //server, username, password, database
    $dbhost = "mysql8.000webhost.com";
    $dbuser = "a3399702_hassoba";
    $dbpass = "password";
    $dbdb = "a3399702_sa3dni";

    //connect to mySQL
    $connect = mysql_connect($dbhost, $dbuser, $dbpass) or die("connection error");
    //Select the database
    mysql_select_db($dbdb, $connect) or die("database selection error");
    //Query the table android login
    $query = mysql_query("SELECT * FROM signup_db");
    //Create a while loop that places the returned data into an array
    while ($list = mysql_fetch_assoc($query))
    //Store the returned data into a variable
        $output[] = $list;
    //encode the returned data in JSON format
    Print(json_encode($output));
    //close the connection
    mysql_close();
?>

爪哇代码:

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<String>(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();
        }
    }
}
4

2 回答 2

0

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

于 2012-08-14T14:43:24.763 回答
0

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

于 2012-08-14T13:34:37.987 回答