0

i'm trying to retrieve data from database and list them in android but i was confused how to do this so i used the listavtivity and some other php codes but it didn't work as the app. crash i just want to ask is this a right way?

php code:

<?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();
?>

java code:

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();
        }
    }
}
code-jaff
  • 9,230
  • 4
  • 35
  • 56

2 Answers2

0

To display data in a ListView you need to use a ListAdapter. Here is a tutorial that might help you: http://www.ezzylearning.com/tutorial.aspx?tid=1763429

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
0

The approach seems fine to me. Another way can be implement SOAP Web Service instead of JSON, however, in that case you need to either parse the SOAP response manually or use some other tool/library to parse the SOAP response. These tools are not mature enough for Android right now. I would personally prefer the JSON way and the reason for your application crash seems to be some other issue specific to Android framework not related to your approach for fetching data from database.

vikas
  • 1,535
  • 1
  • 13
  • 22