尝试使用JSON通过Servlet将数据检索到Android时出现问题。所以基本上这是我的控制器类:

public String GetPwd(SoccerUserModel userModel) throws JSONException {
    String page, pwd = null;
    JSONArray jsonArray;

    try {
        String userName = userModel.getUserName();
        String securityQuestion = userModel.getSecurityQuestion();
        String securityAnswer = userModel.getSecurityAnswer();

        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(MainActivity.URL
                + "/SoccerWebService?action=GetPwd&userName="
                + userName + "&securityQuestion=" + securityQuestion + "&securityAnswer=" + securityAnswer + "");

        HttpResponse response = client.execute(request);
        HttpEntity entity = response.getEntity();
        String responseString = EntityUtils.toString(entity, "UTF-8");
        page = "{\'Pwd\':" + responseString + "}";
        try {
            JSONObject jsonObject = new JSONObject(page);
            jsonArray = jsonObject.getJSONArray("Pwd");
            int length = jsonArray.length();
            for (int i = 0; i < length; i++) {
                JSONObject attribute = jsonArray.getJSONObject(i);
                pwd = attribute.getString("pwd");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return pwd;
}


还有我的AsyncTaskClass:

public class GetPwdAsyncTask extends AsyncTask<SoccerUserModel, Integer, Double> {
public static String pwd;
UserController userCtrl = new UserController();
Context context;

public interface OnRoutineFinished {
    void onFinish();
}

private OnRoutineFinished mCallbacks;

public GetPwdAsyncTask(OnRoutineFinished callback) {
    mCallbacks = callback;
}

public GetPwdAsyncTask() {
}

@Override
protected Double doInBackground(SoccerUserModel... params) {
    if (params.length == 1) {
        try {
            pwd = userCtrl.GetPwd(params[0]);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    return null;
}

protected void onPostExecute(Double result) {
    if (mCallbacks != null)
        mCallbacks.onFinish();
}

protected void onProgressUpdate(Integer... progress) {
}

}


然后我的SQL部分位于servlet中:

else if (action.equalsIgnoreCase("GetPwd")) {
        String userName = request.getParameter("userName");
        String securityQuestion = request.getParameter("securityQuestion");
        String securityAnswer = request.getParameter("securityAnswer");

        try {
            PreparedStatement statement = db
                    .getStatement("SELECT * FROM soccerUser WHERE userName = '"
                            + userName + "' AND securityQuestion = '" + securityQuestion + "'" +
                                    " AND securityAnswer = '" + securityAnswer + "'");
            ResultSet result = statement.executeQuery();
            while (result.next()) {
                JSONObject jsonResult = new JSONObject();
                jsonResult.put("pwd", result.getString("pwd"));

                jsonArray.put(jsonResult);
            }
        }

        catch (JSONException je) {
            System.out.println(je.getMessage());
        } catch (Exception exc) {
            System.out.println(exc.getMessage());
        }

        out.println(jsonArray.toString());

    }


我已经使用以下URL测试过:http://localhost:8080/SoccerWebService/SoccerWebService?action=GetPwd&userName=user&securityQuestion=Who是I&securityAnswer = user,它向我返回此结果:

[{"pwd":"123"}]


但是,当我尝试通过手机运行时,出现以下错误消息:

05-06 22:05:35.990: W/dalvikvm(5113): threadid=12: thread exiting with uncaught exception (group=0x40c4b1f8)
05-06 22:05:36.005: E/AndroidRuntime(5113): FATAL EXCEPTION: AsyncTask #1
05-06 22:05:36.005: E/AndroidRuntime(5113): java.lang.RuntimeException: An error occured while executing doInBackground()
05-06 22:05:36.005: E/AndroidRuntime(5113):     at android.os.AsyncTask$3.done(AsyncTask.java:278)
05-06 22:05:36.005: E/AndroidRuntime(5113):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
05-06 22:05:36.005: E/AndroidRuntime(5113):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
05-06 22:05:36.005: E/AndroidRuntime(5113):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
05-06 22:05:36.005: E/AndroidRuntime(5113):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-06 22:05:36.005: E/AndroidRuntime(5113):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
05-06 22:05:36.005: E/AndroidRuntime(5113):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-06 22:05:36.005: E/AndroidRuntime(5113):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-06 22:05:36.005: E/AndroidRuntime(5113):     at java.lang.Thread.run(Thread.java:856)
05-06 22:05:36.005: E/AndroidRuntime(5113): Caused by: java.lang.IllegalArgumentException: Illegal character in query at index 110: http://192.168.0.25:8080/SoccerWebService/SoccerWebService?action=GetPwd&userName=user3&securityQuestion=Whats your pet name&securityAnswer=hiha
05-06 22:05:36.005: E/AndroidRuntime(5113):     at java.net.URI.create(URI.java:727)
05-06 22:05:36.005: E/AndroidRuntime(5113):     at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
05-06 22:05:36.005: E/AndroidRuntime(5113):     at Controller.UserController.GetPwd(UserController.java:111)
05-06 22:05:36.005: E/AndroidRuntime(5113):     at AsyncTask.GetPwdAsyncTask.doInBackground(GetPwdAsyncTask.java:32)
05-06 22:05:36.005: E/AndroidRuntime(5113):     at AsyncTask.GetPwdAsyncTask.doInBackground(GetPwdAsyncTask.java:1)
05-06 22:05:36.005: E/AndroidRuntime(5113):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
05-06 22:05:36.005: E/AndroidRuntime(5113):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-06 22:05:36.005: E/AndroidRuntime(5113):     ... 5 more


我不确定哪一部分出错了。提前致谢。

最佳答案

IllegalArgumentException:索引为110的查询中的非法字符...

http://192.168.0.25:8080/SoccerWebService/SoccerWebService?action=GetPwd&userName=user3&securityQuestion=Whats您的宠物名和安全性Answer = hiha


在索引110和更高的索引中,我可以看到:


您的宠物名称和安全性Answer = hiha


您的无效字符只是一个空格,URL不允许使用。

确保URL encode

10-01 03:31