我在尝试使用 JSON 通过 Servlet 将数据检索到 Android 时遇到了一些问题。所以基本上这是我的 Controller 类:
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我是&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: 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
在索引 110 及以上我可以看到:
your pet name&securityAnswer=hiha
您的无效字符只是一个空格,这是网址中不允许的。
确保URL encode .
关于java - Android Servlet AsyncTask #1 致命异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30079251/