从mySql中检索数据并使用php和json在android应用程序上显示它

时间:2013-02-01 16:38:19

标签: php android mysql

我正在创建一个Android应用程序然后允许用户在登录后查看他们的个人资料。我完成了我的登录页面,但是,当定向到个人资料页面时,无法检索数据。谁能告诉我出了什么问题?

ProfilePage.java

package com.example.foodTemption;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class profilePage extends Activity{
 private Button btnlogout;
 private ProgressDialog pDialog;
    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();

    ArrayList<HashMap<String, String>> profileList;

    // url to get all products list
    private static String url_retrieve_data = "http://10.0.2.2:8080/foodtemption/get_product_details.php";

    private static final String TAG_SUCCESS = "success";
    private static final String TAG_LOGIN = "login";
    private static final String TAG_USERID = "Userid";
    private static final String TAG_USERNAME = "username";
    private static final String TAG_EMAIL = "email";
    private static final String TAG_ADDRESS = "address";
    private static final String TAG_DOB = "dob";
    private static final String TAG_PASSWORD = "password";

    JSONArray profile = null;
    String userid;
    private Button button1; 
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.profilepage);

    btnlogout = (Button) findViewById(R.id.btnlogout);
   button1 = (Button) findViewById(R.id.button1);
    TextView txt_loggedName = (TextView) findViewById(R.id.txt_loggedName);

    Intent i = getIntent();
    String username = i.getStringExtra("username");
    txt_loggedName.setText(username);
    String userid = i.getStringExtra("Userid");
    profileList = new ArrayList<HashMap<String, String>>();

    new LoadProfile().execute(userid);

    button1.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent myIntent = new Intent(profilePage.this, PurchasedVoucher.class);
             startActivity(myIntent);
                  }

             });

    btnlogout.setOnClickListener(new OnClickListener() {

      public void onClick(View v) {
          Intent myIntent = new Intent(profilePage.this, loginPage.class);
          startActivity(myIntent);

      }
    });
    /* Get values from Intent */
    /*Intent intent = getIntent();
    String name  = intent.getStringExtra("name");
    txt_loggedName.setText(name);*/
}

  class LoadProfile extends AsyncTask<String, String, String> {
        protected void onPreExecute(){
            super.onPreExecute();
            pDialog = new ProgressDialog(profilePage.this);
            pDialog.setMessage("Loading profile");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        protected String doInBackground(String... args) {
            String res=null;
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            JSONObject json = jParser.makeHttpRequest(url_retrieve_data+"?Userid="+args[0], "GET",params);
                    Log.d("Profile:", json.toString());
            try{
                int success = json.getInt(TAG_SUCCESS);

                if(success == 1){

                    JSONObject jo = json.getJSONObject("users");
                    userid = jo.getString("Userid");
                    res = "1";

                }
            else{
                res ="0";
        }
    }catch(JSONException e) {
        e.printStackTrace();
    }
    return res;
        }

        @Override
        protected void onPostExecute(String result) {
            TextView textView5 = (TextView) findViewById(R.id.textView5);
            TextView textView9 = (TextView) findViewById(R.id.textView9);
            TextView textView3 = (TextView) findViewById(R.id.textView3);
            TextView textView11 = (TextView) findViewById(R.id.textView11);
            // TODO Auto-generated method stub
            super.onPostExecute(result);

            for(HashMap<String,String> map:profileList)
            {
                String userid = map.get(TAG_USERID);
                String username = map.get(TAG_USERNAME);
                String password = map.get(TAG_PASSWORD);
                String email = map.get(TAG_EMAIL);
                String address = map.get(TAG_ADDRESS);
                String dob = map.get(TAG_DOB);

                System.out.println(userid+" / "+username+" / " +password+ "/" +email+" / " +address+" / " +dob);
                textView11.setText(password);
                textView5.setText(email);
                textView9.setText(address);
                textView3.setText(dob);

            }
            pDialog.dismiss();
        }   


    } 
}

这是php文件     

/*
 * Following code will get single user details
 */

// array for JSON response
$response = array();


// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// check for post data
if (isset($_GET["Userid"])) {
$Userid = $_GET['Userid'];

// get a user from users table
$result = mysql_query("SELECT *FROM users WHERE Userid = $Userid");

if (!empty($result)) {
    // check for empty result
    if (mysql_num_rows($result) > 0) {

        $result = mysql_fetch_array($result);

        $users = array();
        $users["Userid"] = $result["Userid"];
        $users["username"] = $result["username"];
        $users["password"] = $result["password"];
        $users["email"] = $result["email"];
        $users["address"] = $result["address"];
        $users["dob"] = $result["dob"];
        // success
        $response["success"] = 1;

        // user node
        $response["users"] = array();

        array_push($response["users"], $users);

        // echoing JSON response
        echo json_encode($response);
    } else {
        // no user found
        $response["success"] = 0;
        $response["message"] = "No user found";

        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // no user found
    $response["success"] = 0;
    $response["message"] = "No user found";

    // echo no users JSON
    echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";

// echoing JSON response
echo json_encode($response);
}
?>

这是Xml文件          

<RelativeLayout
    android:id="@+id/txtEmail"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.94" >

    <TextView
        android:id="@+id/txt_loggedName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="haha"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="108dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/txt_loggedName"
        android:text="&apos;s profile"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/btnlogout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Logout" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView4"
        android:layout_alignBottom="@+id/textView4"
        android:layout_toLeftOf="@+id/btnlogout"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView8"
        android:layout_alignBottom="@+id/textView8"
        android:layout_alignRight="@+id/textView5"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView4"
        android:layout_marginTop="32dp"
        android:text="Address:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView10"
        android:layout_alignBottom="@+id/textView10"
        android:layout_alignRight="@+id/textView9"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/btnlogout"
        android:text="Email:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView9"
        android:layout_below="@+id/textView9"
        android:layout_marginTop="42dp"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/txtEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView3"
        android:layout_alignBottom="@+id/textView3"
        android:layout_alignParentLeft="true"
        android:text="Date Of Birth"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/txtEmail"
        android:layout_marginTop="32dp"
        android:text="Password"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView11"
        android:layout_marginTop="27dp"
        android:layout_weight="0.94"
        android:text="View purchased vouchers" />
</RelativeLayout>

</LinearLayout>

0 个答案:

没有答案