gpt4 book ai didi

java - Android向php发送数据并接收问题nullpointerException

转载 作者:行者123 更新时间:2023-12-01 11:02:46 28 4
gpt4 key购买 nike

我是 Android 开发新手,我不确定我缺少什么来让代码正常工作。尝试创建登录应用程序。我使用 dataoutputstream 接收数据或发送数据的方式是否有问题?有没有更好的方法,既不被弃用,也不复杂。寻找最简约、易于理解的解决方案。

在后台代码中执行

@Override
protected Boolean doInBackground(String... params) {


DataOutputStream printout;
HttpURLConnection urlConn;
BufferedReader reader;
URL url;
String json;
JSONObject jObj;


//send POST login details to server
try {
url = new URL(SERVER_ADDRESS + "login.php");
urlConn = (HttpURLConnection) url.openConnection();
urlConn.setRequestMethod("POST");
urlConn.setDoOutput(true);
urlConn.setRequestProperty("Accept-Charset", "UTF-8");
urlConn.setReadTimeout(10000);
urlConn.setConnectTimeout(15000);

HashMap<String, String> map = new HashMap<String, String>();
map.put("username", params[0]);
map.put("password", params[1]);

StringBuilder sb = new StringBuilder();
for(HashMap.Entry<String, String> e : map.entrySet()){
if(sb.length() > 0){
sb.append("&");
}
sb.append(URLEncoder.encode(e.getKey(), "UTF-8")).append("=").append(URLEncoder.encode(e.getValue(), "UTF-8"));
}


String loginDetails = sb.toString();

printout = new DataOutputStream(urlConn.getOutputStream());
printout.writeBytes(loginDetails);
printout.flush();
printout.close();

reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
StringBuilder sb2 = new StringBuilder();
String result = null;
while((result=reader.readLine())!=null) {
sb2.append(result + '\n');
Log.d("Results", sb2.toString());
};

json = result.toString();

jObj = new JSONObject(json);

JSONLogin = jObj.getBoolean(TAG_LOGINSTATUS);
Log.d("LoginStatus!!!!", JSONLogin.toString());

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
Log.d("JsonConvertion", "FAILED TO CONVERT JSON");
}


return JSONLogin;
}

堆栈错误

10-18 22:43:34.169 2459-2523/? E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
10-18 22:43:34.169 2459-2523/? E/AndroidRuntime: Process: com.httpkhronolog.khronolog, PID: 2459
10-18 22:43:34.169 2459-2523/? E/AndroidRuntime: java.lang.RuntimeException: An error occurred while executing doInBackground()
10-18 22:43:34.169 2459-2523/? E/AndroidRuntime: at android.os.AsyncTask$3.done(AsyncTask.java:309)
10-18 22:43:34.169 2459-2523/? E/AndroidRuntime: at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
10-18 22:43:34.169 2459-2523/? E/AndroidRuntime: at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
10-18 22:43:34.169 2459-2523/? E/AndroidRuntime: at java.util.concurrent.FutureTask.run(FutureTask.java:242)
10-18 22:43:34.169 2459-2523/? E/AndroidRuntime: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
10-18 22:43:34.169 2459-2523/? E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
10-18 22:43:34.169 2459-2523/? E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
10-18 22:43:34.169 2459-2523/? E/AndroidRuntime: at java.lang.Thread.run(Thread.java:818)
10-18 22:43:34.169 2459-2523/? E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
10-18 22:43:34.169 2459-2523/? E/AndroidRuntime: at com.httpkhronolog.khronolog.Login_Activity$checkUserDetails.doInBackground(Login_Activity.java:137)
10-18 22:43:34.169 2459-2523/? E/AndroidRuntime: at com.httpkhronolog.khronolog.Login_Activity$checkUserDetails.doInBackground(Login_Activity.java:79)
10-18 22:43:34.169 2459-2523/? E/AndroidRuntime: at android.os.AsyncTask$2.call(AsyncTask.java:295)
10-18 22:43:34.169 2459-2523/? E/AndroidRuntime: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-18 22:43:34.169 2459-2523/? E/AndroidRuntime: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
10-18 22:43:34.169 2459-2523/? E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
10-18 22:43:34.169 2459-2523/? E/AndroidRuntime: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
10-18 22:43:34.169 2459-2523/? E/AndroidRuntime: at java.lang.Thread.run(Thread.java:818) 

login.php

<?php

if($_SERVER["REQUEST_METHOD"] == "POST"){
// Establishing Connection with Server by passing server_name, user_id and password as a parameter
$connection = mysql_connect("localhost", "someusername", "Password");
// Selecting Database
$db = mysql_select_db("khronolo_systemdb", $connection);

$username = $_POST["username"];
$password = $_POST["password"];

$query = "SELECT * FROM user_table WHERE password='$password' AND email='$username';";
$result = mysql_query($query, $connection) or die (mysql_error($connection));
$rows = mysql_num_rows($result);
if ($rows == 1) {
$returnValue[] = array("loginStatus"=> "true");
echo json_encode($returnValue);
}
else {
$returnValue[] = array("loginStatus"=> "false");
echo json_encode($returnValue);
}

mysql_close($connection);
}
?>

最佳答案

使用Volley Google 的网络库。关注此tutorial对于相同的。要了解有关 Volley 的更多信息,请参阅此 link

还有其他网络库,例如 okHttp .

到目前为止,您的代码已被考虑:

将第 137 行的 json = result.toString(); 替换为 json = sb2.toString();

关于java - Android向php发送数据并接收问题nullpointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33209002/

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