使用asynctask从数据库中获取数据

时间:2015-08-22 16:29:02

标签: php android android-asynctask

我需要从我的数据库中获取返回的值并在屏幕上显示它。所以我用asynctask函数编写了一个php文件和一个类文件,将数据返回到屏幕但是没有用!!!!

尝试使用asynctask编写此类以与数据库进行通信:

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by QingYong on 21/08/2015.
 */
public class EditProfile extends Activity {

    String user,password,name_entered,username_returned;
    String handphone_no;
    EditText username,password_entered,name,handphone;
    JSONParser jsonParser = new JSONParser();
    private static final String LOGIN_URL = "http://powerbankk.16mb.com/profile.php";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.editprofile);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            user = extras.getString("username");
        }
        new AttemptLogin().execute(user);

        username = (EditText) findViewById(R.id.username_signup);
        password_entered = (EditText) findViewById(R.id.password);
        name = (EditText) findViewById(R.id.name);
        handphone = (EditText) findViewById(R.id.handphone);

        String messageString = username_returned;
        username.setText(messageString);
        String messageString1 = password;
        password_entered.setText(messageString1);
        String messageString2 = handphone_no;
        handphone.setText(messageString2);
        String messageString3 = name_entered;
        name.setText(messageString3);
    }

    class AttemptLogin extends AsyncTask<String, String, JSONObject> {
        protected JSONObject doInBackground(String... args) {
            // TODO Auto-generated method stub
            // here Check for success tag
            try {
                HashMap<String, String> params = new HashMap<>();
                JSONObject json = jsonParser.makeHttpRequest(
                        LOGIN_URL, "POST", params);

                if (json != null) {
                    Log.d("JSON result", json.toString());
                    params.put("user", args[0]);
                    return json;
                }

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

            return null;
        }

        protected void onPostExecute(JSONObject json) {

            if (json != null) {
                Toast.makeText(EditProfile.this, json.toString(),
                        Toast.LENGTH_LONG).show();

                try {
                    username_returned = json.getString("username");
                    password = json.getString("password");
                    handphone_no = json.getString("handphone");
                    name_entered = json.getString("name");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

这是我的php文件:

<?php

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

  $username=$_POST["user"];

  $query = " SELECT name,username,password,handphone FROM Login WHERE username='$username'";
  $sql=mysqli_query($conn, $query);

  if (!$sql) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}

  $row = mysqli_fetch_row($sql);
  $response["name"] = $row[0];
  $response["username"] = $row[1];
  $response["password"] = $row[2];
  $response["handphone"] = $row[3];
  die(json_encode($response));

  mysql_close();
  ?>

我哪里出错了?

1 个答案:

答案 0 :(得分:0)

初始化params.put("user", args[0]);后立即致电params。你基本上没有“用户”就是JSONObject

HashMap<String, String> params = new HashMap<>();
params.put("user", args[0]);
// now finally make the http request
JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST", params);

如果有效,请告诉我。代码看起来不错,这是我注意到的缺陷。