我需要從我的數據庫中獲取返回值並將其顯示在屏幕上。所以我寫了一個php文件和一個使用asynctask函數的類文件將數據返回到屏幕,但它沒有工作!使用asynctask從數據庫中獲取數據
試圖寫入使用的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();
?>
哪裏我去錯了嗎?
*它沒有工作!!! *:請不要讓我們猜測。告訴我們它是如何工作的,實際發生了什麼以及你期望發生什麼。 – adelphus
在'params'初始化後立即調用'params.put(「user」,args [0]);''。你基本上不用「用戶」就可以創建一個'JSONObject'。 –
@RakeebRajbhandari那麼我該如何解決它? –