我想在 mysql 中选择行值。当我运行代码时,logcat 显示“缺少必填字段”。我不确定 php 或 android 代码中的问题。我希望有一个人可以帮助我。
public class profileActivity extends Activity{
EditText txtName;
EditText inputusername;
EditText answer;
EditText txtPrice;
EditText txtDesc;
EditText txtCreatedAt;
String username;
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_UID = "uid";
private static String KEY_NAME = "name";
private static String KEY_FIRSTNAME = "first_name";
private static String KEY_lASTNAME = "last_name";
private static String KEY_EMAIL = "email";
private DatabaseHandler dbHelper;
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
// single product url
private static final String profileURL = "http://10.0.2.2/android_login_api4/include/profile.php";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
// getting product details from intent
Intent i = getIntent();
// getting product id (pid) from intent
username = i.getStringExtra(KEY_NAME);
// Getting complete product details in background thread
new GetProfileDetails().execute();
}
/**
* Background Async Task to Get complete product details
* */
class GetProfileDetails extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(profileActivity.this);
pDialog.setMessage("Loading product details. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Getting product details in background thread
* */
protected String doInBackground(String... params) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
// Check for success KEY
int success;
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
// getting product details by making HTTP request
// Note that product details url will use GET request
JSONObject json = jsonParser.getJSONFromUrl(
profileURL, params);
// check your log for json response
Log.d("Single Product Details", json.toString());
// json success KEY
success = json.getInt(KEY_SUCCESS);
if (success == 1) {
// successfully received product details
JSONArray productObj = json
.getJSONArray(KEY_NAME); // JSON Array
// get first product object from JSON Array
JSONObject product = productObj.getJSONObject(0);
// product with this uid found
// Edit Text
txtName = (EditText) findViewById(R.id.name);
txtPrice = (EditText) findViewById(R.id.email);
txtDesc = (EditText) findViewById(R.id.tel);
// display product data in EditText
txtName.setText(product.getString(KEY_lASTNAME));
txtPrice.setText(product.getString(KEY_FIRSTNAME));
txtDesc.setText(product.getString(KEY_EMAIL));
}else{
// product with uid not found
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once got all details
pDialog.dismiss();
}
}
}
那是php的代码
<?php
// array for JSON response
$response = array();
// include db connect class
require_once 'DB_Connect.php';
// connecting to db
$db = new DB_CONNECT();
// check for post data
if (isset($_GET["username"])) {
$username = $_GET['username'];
// get a product from products table
$result = mysql_query("SELECT *FROM users WHERE username = $username");
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
$product = array();
$product["username"] = $result["username"];
$product["first_name"] = $result["first_name"];
$product["last_name"] = $result["last_name"];
$product["email"] = $result["email"];
$product["tel"] = $result["tel"];
$product["age"] = $result["age"];
// success
$response["success"] = 1;
// user node
$response["product"] = array();
array_push($response["product"], $product);
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product 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);
}
?>