当我的数据库中有数据时,我的Android没有检索帖子

时间:2013-12-14 01:18:07

标签: php android json

这是我的UserInfo.java

public class UserInfo extends ActionBarActivity implements OnClickListener {

// Progress Dialog
    private ProgressDialog pDialog;
EditText name, email, password, mobilenumber, address,
        city, postcode, state;
Button update;

private JSONArray userinfo = null;


// JSON parser class
JSONParser jsonParser = new JSONParser();

private static final String USERINFO_URL = "http://192.168.1.10:1234/PMSS/userinfo.php";
private static final String UPDATEUSERINFO_URL = "http://192.168.1.10:1234/PMSS/updateuserinfo.php";
// ids
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
private static final String TAG_POSTS = "posts";
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
private static final String TAG_PASSWORD = "password";
private static final String TAG_MOBILENUMBER = "mobilenumber";
private static final String TAG_ADDRESS = "address";
private static final String TAG_CITY = "city";
private static final String TAG_POSTCODE = "postcode";
private static final String TAG_STATE = "state";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_info);
    // Show the Up button in the action bar.
    update = (Button) findViewById(R.id.update);
    name = (EditText) findViewById(R.id.nametext);
    email = (EditText) findViewById(R.id.useridtext);
    password = (EditText) findViewById(R.id.passwordtext);
    mobilenumber = (EditText) findViewById(R.id.mobilenumbertext);
    address = (EditText) findViewById(R.id.addresstext);
    city = (EditText) findViewById(R.id.citytext);
    postcode = (EditText) findViewById(R.id.postcodetext);
    state = (EditText) findViewById(R.id.statetext);

    update.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String Name = name.getText().toString();
            String Email = email.getText().toString();
            String Password = password.getText().toString();
            String MobileNumber = mobilenumber.getText().toString();
            String Address = address.getText().toString();
            String City = city.getText().toString();
            String PostCode = postcode.getText().toString();
            String State = state.getText().toString();
            new UpdateUser(Name, Email, Password, MobileNumber, Address,
                    City, PostCode, State).execute();
        }
    });
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub

    super.onResume();
    // loading the comments via AsyncTask
    new RetrieveUser().execute();
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:

        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}

String Name, Email, Password, MobileNumber, Address, City, PostCode, State;

public void updateJSONdata() {

    // String post_username = "jiaweitan05@gmail.com";
    JSONParser jsonParser = new JSONParser();

    SharedPreferences sp = PreferenceManager
            .getDefaultSharedPreferences(UserInfo.this);
    String post_username = sp.getString("username", "anon");

    try {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("email", post_username));

        Log.d("request!", "starting");
        // getting product details by making HTTP request
        JSONObject json = jsonParser.makeHttpRequest(USERINFO_URL, "POST",
                params);
        Log.d("Retrieve attempt", json.toString());

        // I know I said we would check if "Posts were Avail."
        // (success==1)
        // before we tried to read the individual posts, but I lied...
        // mComments will tell us how many "posts" or comments are
        // available
        int success =json.getInt(TAG_SUCCESS);
        String message= json.getString(TAG_MESSAGE);
        JSONArray jar = json.getJSONArray(TAG_POSTS);
        if (success == 1){
            Log.d("Retrieve Successful!", "res: " + message);
        JSONArray jr = jar.getJSONArray(0);
        JSONObject jb = jr.getJSONObject(0);
        Name = jb.getString(TAG_NAME);
        Email = jb.getString(TAG_EMAIL);
        Password = jb.getString(TAG_PASSWORD);
        MobileNumber = jb.getString(TAG_MOBILENUMBER);
        Address = jb.getString(TAG_ADDRESS);
        City = jb.getString(TAG_CITY);
        PostCode = jb.getString(TAG_POSTCODE);
        State = jb.getString(TAG_STATE);

        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
}

public class RetrieveUser extends AsyncTask<Void, Void, Boolean> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(UserInfo.this);
        pDialog.setMessage("Loading User Info...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected Boolean doInBackground(Void... arg0) {
        updateJSONdata();
        return null;

    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        pDialog.dismiss();
        name.setText(Name);
        email.setText(Email);
        password.setText(Password);
        mobilenumber.setText(MobileNumber);
        address.setText(Address);
        city.setText(City);
        postcode.setText(PostCode);
        state.setText(State);
    }
}
}

这是我的logcat(当我点击此UserInfo活动的UserInfo按钮时)​​

12-14 09:11:18.960: D/request!(3860): starting
12-14 09:11:19.320: D/Retrieve attempt(3860): {"message":"No Post Available!","success":0}
12-14 09:11:19.320: W/System.err(3860): org.json.JSONException: No value for posts
12-14 09:11:19.320: W/System.err(3860):     at org.json.JSONObject.get(JSONObject.java:354)
12-14 09:11:19.320: W/System.err(3860):     at org.json.JSONObject.getJSONArray(JSONObject.java:544)
12-14 09:11:19.320: W/System.err(3860):     at com.pmss.UserInfo.updateJSONdata(UserInfo.java:162)
12-14 09:11:19.320: W/System.err(3860):     at com.pmss.UserInfo$RetrieveUser.doInBackground(UserInfo.java:211)
12-14 09:11:19.320: W/System.err(3860):     at com.pmss.UserInfo$RetrieveUser.doInBackground(UserInfo.java:1)
12-14 09:11:19.320: W/System.err(3860):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-14 09:11:19.320: W/System.err(3860):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
12-14 09:11:19.320: W/System.err(3860):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
12-14 09:11:19.320: W/System.err(3860):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
12-14 09:11:19.320: W/System.err(3860):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
12-14 09:11:19.320: W/System.err(3860):     at java.lang.Thread.run(Thread.java:1019)

为什么当我的数据库收到包含所有这些帖子的电子邮件时,该消息说我的帖子不可用..

这是我的userinfo.php

<?php

require("config.inc.php");

if (!empty($_POST)) {
//initial query
    $query = "Select name, email, password, mobilenumber, address, city, postcode, state  FROM user WHERE email = :email ";

    $query_params = array(':email' => $_POST['email']);
//execute query
    try {
        $stmt   = $db->prepare($query);
        $result = $stmt->execute($query_params);
    }
    catch (PDOException $ex) {
        $response["success"] = 0;
        $response["message"] = "Database Error!";
        die(json_encode($response));
    }

// Finally, we can retrieve all of the found rows into an array using fetchAll 
    $row = $stmt->fetchAll();


    if ($row) {
        $response["success"] = 1;
        $response["message"] = "Post Available!";
        $response["posts"]   = array();

                $post = array();
                do {

                    // $post = $row["position"];
                    // $post = $row["state"];
                    // array_push($response["posts"], $post);
                    array_push($response["posts"], $row);
                } while ($row = $stmt->fetch()); 

            die(json_encode($response));
    }

    else {
            $response["success"] = 0;
            $response["message"] = "No Post Available!";
            die(json_encode($response));
    }

} 
else {
        ?>
        <h1>User Info</h1> 
        <form action="userinfo.php" method="post"> 
            Email:<br /> 
            <input type="email" name="email" value="" /> 
            <br /><br />
            <input type="submit" value="Search User" /> 
        </form>
        <?php
}
?>

为什么我在文本框中尝试键入我的电子邮件,我的php给了我这个我想要的回复..

{"success":1,"message":"Post Available!",
 "posts":[
             [
               {"name":"Jiawei",
               "email":"jiaweitan05@gmail.com",
               "password":"123456abcd",
               "mobilenumber":"0124331292",
               "address":"Bunga Raya",
               "city":"Bukit Beruang",
               "postcode":"75450",
               "state":"Melaka"
               }
             ]
         ]
}

但我的logcat说No Post available ...我不知道为什么会发生这种情况,即使它应该能够为我检索数据..

0 个答案:

没有答案