我创建了一个带有登录功能的应用程序,该功能将显示用户的信息。它使用localhost工作正常,但当我在托管上传PHP时,配置文件页面崩溃了吗?
崩溃报告
12-18 13:36:38.023: E/AndroidRuntime(1332): FATAL EXCEPTION: main
12-18 13:36:38.023: E/AndroidRuntime(1332): java.lang.NullPointerException
12-18 13:36:38.023: E/AndroidRuntime(1332): at com.example.itmaproject.EditContactsActivity$GetProductDetails$1.run(EditContactsActivity.java:141)
12-18 13:36:38.023: E/AndroidRuntime(1332): at android.os.Handler.handleCallback(Handler.java:725)
12-18 13:36:38.023: E/AndroidRuntime(1332): at android.os.Handler.dispatchMessage(Handler.java:92)
12-18 13:36:38.023: E/AndroidRuntime(1332): at android.os.Looper.loop(Looper.java:137)
12-18 13:36:38.023: E/AndroidRuntime(1332): at android.app.ActivityThread.main(ActivityThread.java:5041)
12-18 13:36:38.023: E/AndroidRuntime(1332): at java.lang.reflect.Method.invokeNative(Native Method)
12-18 13:36:38.023: E/AndroidRuntime(1332): at java.lang.reflect.Method.invoke(Method.java:511)
12-18 13:36:38.023: E/AndroidRuntime(1332): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-18 13:36:38.023: E/AndroidRuntime(1332): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-18 13:36:38.023: E/AndroidRuntime(1332): at dalvik.system.NativeStart.main(Native Method)
爪哇
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(EditContactsActivity.this);
pDialog.setMessage("Loading contact 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 tag
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.makeHttpRequest(
url_contact_details, "GET", params);
// check your log for json response
Log.d("Single Contact Details", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received product details
JSONArray contactObj = json.getJSONArray(TAG_CONTACT); // JSON Array
// get first product object from JSON Array
JSONObject contact = contactObj.getJSONObject(0);
// product with this pid found
// Edit Text
txtName = (TextView) findViewById(R.id.inputName);
txtDepart = (TextView) findViewById(R.id.department);
//txtAbout = (TextView) findViewById(R.id.inputDesc);
txtUser = (TextView) findViewById(R.id.inputPrice);
// display product data in EditText
String fullname = contact.getString(TAG_LASTNAME)+' '+ contact.getString(TAG_FIRSTNAME);
txtName.setText(fullname);
txtDepart.setText(contact.getString(TAG_DEPART));
//txtAbout.setText(contact.getString(TAG_LASTNAME));
txtUser.setText(contact.getString(TAG_ID));
Log.v("blahhhhh", "blah hhhhblah");
}else{
// product with pid not found
Log.v("blah", "blah blah");
}
} 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();
}
}
第141行
Log.d("Single Contact Details", json.toString());
PHP
text/x-generic get_contact_details.php
PHP script text
<?php
/*
* Following code will get single product details
* A product is identified by product id (pid)
*/
// 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["username"])) {
$username = $_GET['username'];
// get a product from products table
$result = mysql_query("SELECT * FROM accounts WHERE username = '".$username."' ");
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
$contacts = array();
$contacts["firstname"] = $result["firstname"];
$contacts["middlename"] = $result["middlename"];
$contacts["lastname"] = $result["lastname"];
$contacts["email"] = $result["email"];
$contacts["user_id"] = $result["user_id"];
$contacts["department"] = $result["department"];
// success
$response["success"] = 1;
// user node
$response["contacts"] = array();
array_push($response["contacts"], $contacts);
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No contact found";
// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No contact 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);
}
?>
答案 0 :(得分:0)
从你的logCat中你得到一个空指针(java.lang.NullPointerException
)
如果是这一行:
Log.d("Single Contact Details", json.toString());
然后,这意味着您的服务器未返回JSON
数据。
而另一件事是错误的,你不应该在doInBackground
中的控件上设置应该在作为主线程的onPostExecute()
中完成的文本。所以你真的不需要使用runOnUiThread
,因为AsyncTask
已经是一个有自己的线程回调的类。
最后一件事,你使用的是已弃用的库,甚至没有转义。更糟糕的是它是GET
参数。
使用预准备语句来避免SQL注入。 (PDO
或mysqli
)