gpt4 book ai didi

java - Android 应用程序上传到远程服务器后崩溃?

转载 作者:行者123 更新时间:2023-11-29 23:18:37 26 4
gpt4 key购买 nike

我制作了一个具有登录功能的应用程序,它将显示用户的信息。使用 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)

Java

/**
* 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);
}
?>

最佳答案

从 logCat 中你得到一个空指针(java.lang.NullPointerException)

如果这是一行:

Log.d("Single Contact Details", json.toString());

那么这意味着您的服务器没有返回 JSON 数据。

还有一件事是错误的,您不应该在 doInBackground 中的控件上设置文本,而应该在主线程 onPostExecute() 中完成。因此,您实际上不需要使用 runOnUiThread,因为 AsyncTask 已经是一个具有自己的线程回调的类。

最后一件事,您正在使用已弃用的库,甚至没有转义。更糟糕的是它是一个 GET 参数。使用准备好的语句来避免 SQL 注入(inject)。 (PDOmysqli)

关于java - Android 应用程序上传到远程服务器后崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27548809/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com