有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java组织。json。JSONException:在{main}()的第6个字符处的main后面应该是“:”

这是我的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 安卓.app.ProgressDialog;
import 安卓.content.Context;
import 安卓.os.AsyncTask;
import 安卓.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 安卓.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 安卓.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有一点了解。我已经完成了这个注册应用程序,数据存储在数据库中,但当我尝试使用相同的详细信息登录时,我收到了这个错误


共 (1) 个答案

  1. # 1 楼答案

    我认为问题在于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;        
    

    }

    所以只要加上“…”