douju2599 2016-01-19 06:07
浏览 110

org.json.JSONException:在{main}()的字符6的main之后预期':'

This is my php code.

<?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);
?>

This is my java code.

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);
    }
}
}

This is the error I got.

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)

I am new to php and have a little knowledge about Android. I've done this registration app, the data is storing in the database but when I try to login with same details I am getting this error.

  • 写回答

2条回答 默认 最新

  • dsgfdgh14569 2016-01-19 06:42
    关注
           String result = EntityUtils.toString(entity);
           JSONObject jObject = new JSONObject(result);
    

    Make sure your variable result is correct JSON format (JSON Viewer)

    This should be echo from your php code. If you see HTML format text string, which probably something goes from from your php server code, debug into it and check the return results.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c