这是我的PHP代码。
<?php
$con= mysqli_connect("localhost","root","123@123","mysql");
$email=$_POST["email"];
$password=$_POST["password"];
$statement = mysqli_prepare($con,"select * from Userdata where email = ? and password = ? ");
mysqli_stmt_bind_param($statement, "ss", $email, $password);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement,$name,$email,$password,$phone);
$user = array();
while(mysqli_stmt_fetch($statement)){
$user[name]= $name;
$user[email]=$email;
$user[password]=$password;
$user[phone]=$phone;
}
$users = array_values($user);
echo json_encode($user);
mysqli_stmt_close($statement);
mysqli_close($con);
?>
这是我的java代码。
package com.mycompany.nowapp;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import java.util.ArrayList;
public class ServerRequests {
ProgressDialog progressDialog;
public static final int CONNECTION_TIMEOUT = 5000 * 15;
public static final String SERVER_ADDRESS = "http://10.0.2.2/myfiles/";
public ServerRequests(Context context) {
progressDialog = new ProgressDialog(context);
progressDialog.setCancelable(false);
progressDialog.setTitle("Processing...");
progressDialog.setMessage("Please wait...");
}
public void storeUserDataInBackground(User user,
GetUserCallback userCallBack) {
progressDialog.show();
new StoreUserDataAsyncTask(user, userCallBack).execute();
}
public void fetchUserDataAsyncTask(User user, GetUserCallback userCallBack) {
progressDialog.show();
new fetchUserDataAsyncTask(user, userCallBack).execute();
}
/**
* parameter sent to task upon execution progress published during
* background computation result of the background computation
*/
public class StoreUserDataAsyncTask extends AsyncTask<Void, Void, Void> {
User user;
GetUserCallback userCallBack;
public StoreUserDataAsyncTask(User user, GetUserCallback userCallBack) {
this.user = user;
this.userCallBack = userCallBack;
}
@Override
protected Void doInBackground(Void... params) {
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("name", user.name));
dataToSend.add(new BasicNameValuePair("email", user.email));
dataToSend.add(new BasicNameValuePair("password", user.password));
dataToSend.add(new BasicNameValuePair("phone", user.phone + ""));
HttpParams httpRequestParams = getHttpRequestParams();
HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(SERVER_ADDRESS
+ "Register.php");
try {
post.setEntity(new UrlEncodedFormEntity(dataToSend));
client.execute(post);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private HttpParams getHttpRequestParams() {
HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams,
CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpRequestParams,
CONNECTION_TIMEOUT);
return httpRequestParams;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
progressDialog.dismiss();
userCallBack.done(null);
}
}
public class fetchUserDataAsyncTask extends AsyncTask<Void, Void, User> {
User user;
GetUserCallback userCallBack;
public fetchUserDataAsyncTask(User user, GetUserCallback userCallBack) {
this.user = user;
this.userCallBack = userCallBack;
}
@Override
protected User doInBackground(Void... params) {
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("email", user.email));
dataToSend.add(new BasicNameValuePair("password", user.password));
HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams,
CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpRequestParams,
CONNECTION_TIMEOUT);
HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(SERVER_ADDRESS
+ "FetchUserData.php");
User returnedUser = null;
try {
post.setEntity(new UrlEncodedFormEntity(dataToSend));
HttpResponse httpResponse = client.execute(post);
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
JSONObject jObject = new JSONObject(result);
//JSONObject jObject = new JSONObject(result.substring(result.indexOf("{"), result.lastIndexOf("}") + 1));
if (jObject.length() != 0){
Log.v("happened", "2");
String name = jObject.getString("name");
long phone = jObject.getLong("phone");
returnedUser = new User(name, phone, user.email,
user.password);
}
} catch (Exception e) {
e.printStackTrace();
}
return returnedUser;
}
@Override
protected void onPostExecute(User returnedUser) {
super.onPostExecute(returnedUser);
progressDialog.dismiss();
userCallBack.done(returnedUser);
}
}
}
这是我得到的错误。
01-18 23:36:10.990 21408-21433/com.mycompany.nowapp W/System.err: org.json.JSONException: Expected ':' after main at character 6 of {main}( )</td><td title='C:\wamp\www\myfiles\FetchUserData.php' bgcolor='#eeeeec'>..\FetchUserData.php<b>:</b>0</td></tr>
01-18 23:36:10.990 21408-21433/com.mycompany.nowapp W/System.err: </table></font>
01-18 23:36:10.990 21408-21433/com.mycompany.nowapp W/System.err: <br />
01-18 23:36:10.990 21408-21433/com.mycompany.nowapp W/System.err: <font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
01-18 23:36:10.990 21408-21433/com.mycompany.nowapp W/System.err: <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Use of undefined constant email - assumed 'email' in C:\wamp\www\myfiles\FetchUserData.php on line <i>17</i></th></tr>
01-18 23:36:10.990 21408-21433/com.mycompany.nowapp W/System.err: <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
01-18 23:36:10.990 21408-21433/com.mycompany.nowapp W/System.err: <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
01-18 23:36:10.990 21408-21433/com.mycompany.nowapp W/System.err: <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0010</td><td bgcolor='#eeeeec' align='right'>243544</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\wamp\www\myfiles\FetchUserData.php' bgcolor='#eeeeec'>..\FetchUserData.php<b>:</b>0</td></tr>
01-18 23:36:10.990 21408-21433/com.mycompany.nowapp W/System.err: </table></font>
01-18 23:36:10.990 21408-21433/com.mycompany.nowapp W/System.err: <br />
01-18 23:36:10.990 21408-21433/com.mycompany.nowapp W/System.err: <font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
01-18 23:36:10.990 21408-21433/com.mycompany.nowapp W/System.err: <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Use of undefined constant password - assumed 'password' in C:\wamp\www\myfiles\FetchUserData.php on line <i>18</i></th></tr>
01-18 23:36:10.990 21408-21433/com.mycompany.nowapp W/System.err: <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
01-18 23:36:10.990 21408-21433/com.mycompany.nowapp W/System.err: <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
01-18 23:36:10.990 21408-21433/com.mycompany.nowapp W/System.err: <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0010</td><td bgcolor='#eeeeec' align='right'>243544</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\wamp\www\myfiles\FetchUserData.php' bgcolor='#eeeeec'>..\FetchUserData.php<b>:</b>0</td></tr>
01-18 23:36:10.990 21408-21433/com.mycompany.nowapp W/System.err: </table></font>
01-18 23:36:10.990 21408-21433/com.mycompany.nowapp W/System.err: <br />
01-18 23:36:11.000 21408-21433/com.mycompany.nowapp W/System.err: <font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
01-18 23:36:11.000 21408-21433/com.mycompany.nowapp W/System.err: <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Use of undefined constant phone - assumed 'phone' in C:\wamp\www\myfiles\FetchUserData.php on line <i>19</i></th></tr>
01-18 23:36:11.000 21408-21433/com.mycompany.nowapp W/System.err: <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
01-18 23:36:11.000 21408-21433/com.mycompany.nowapp W/System.err: <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
01-18 23:36:11.000 21408-21433/com.mycompany.nowapp W/System.err: <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0010</td><td bgcolor='#eeeeec' align='right'>243544</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\wamp\www\myfiles\FetchUserData.php' bgcolor='#eeeeec'>..\FetchUserData.php<b>:</b>0</td></tr>
01-18 23:36:11.000 21408-21433/com.mycompany.nowapp W/System.err: </table></font>
01-18 23:36:11.000 21408-21433/com.mycompany.nowapp W/System.err: {"name":"qwe","email":"qwe","password":"qwe","phone":123123123}
01-18 23:36:11.000 21408-21433/com.mycompany.nowapp W/System.err: at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
01-18 23:36:11.000 21408-21433/com.mycompany.nowapp W/System.err: at org.json.JSONTokener.readObject(JSONTokener.java:379)
01-18 23:36:11.000 21408-21433/com.mycompany.nowapp W/System.err: at org.json.JSONTokener.nextValue(JSONTokener.java:100)
01-18 23:36:11.000 21408-21433/com.mycompany.nowapp W/System.err: at org.json.JSONObject.<init>(JSONObject.java:155)
01-18 23:36:11.000 21408-21433/com.mycompany.nowapp W/System.err: at org.json.JSONObject.<init>(JSONObject.java:172)
01-18 23:36:11.000 21408-21433/com.mycompany.nowapp W/System.err: at com.mycompany.nowapp.ServerRequests$fetchUserDataAsyncTask.doInBackground(ServerRequests.java:140)
01-18 23:36:11.000 21408-21433/com.mycompany.nowapp W/System.err: at com.mycompany.nowapp.ServerRequests$fetchUserDataAsyncTask.doInBackground(ServerRequests.java:105)
01-18 23:36:11.000 21408-21433/com.mycompany.nowapp W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:288)
01-18 23:36:11.000 21408-21433/com.mycompany.nowapp W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
01-18 23:36:11.000 21408-21433/com.mycompany.nowapp W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
01-18 23:36:11.000 21408-21433/com.mycompany.nowapp W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
01-18 23:36:11.000 21408-21433/com.mycompany.nowapp W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
01-18 23:36:11.000 21408-21433/com.mycompany.nowapp W/System.err: at java.lang.Thread.run(Thread.java:841)
我是php新手,对Android有一点了解。我已完成此注册应用程序,数据存储在数据库中,但当我尝试使用相同的详细信息登录时,我收到此错误。
答案 0 :(得分:0)
String result = EntityUtils.toString(entity);
JSONObject jObject = new JSONObject(result);
确保您的变量结果是正确的JSON格式(JSON Viewer)
这应该来自你的PHP代码。如果您看到HTML格式的文本字符串,可能来自您的php服务器代码,请调试它并检查返回结果。
答案 1 :(得分:0)
我认为问题出在FetchUserData.php中 你必须改变这些行:
while(mysqli_stmt_fetch($statement)){
$user[name]= $name;
$user[email]=$email;
$user[password]=$password;
$user[phone]=$phone;
}
到
while(mysqli_stmt_fetch($statement)){
$user['name']= $name;
$user['email']=$email;
$user['password']=$password;
$user['phone']=$phone;
}
所以只需添加&#39; ....&#39;