Android Servlet AsyncTask#1致命异常

时间:2022-12-27 21:11:58

I was having some problem when trying to retrieve data via Servlet into Android using JSON. So basically here is my controller class:

尝试使用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;
}

And my AsyncTaskClass:

而我的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) {
}

}

And then my SQL part located inside the servlet:

然后我的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());

    }

I have tested with this URL: http://localhost:8080/SoccerWebService/SoccerWebService?action=GetPwd&userName=user&securityQuestion=Who am I&securityAnswer=user which returning me this result:

我已经使用此URL进行了测试:http:// localhost:8080 / SoccerWebService / SoccerWebService?action = GetPwd&userName = user&securityQuestion =我是谁&securityAnswer =用户返回此结果:

[{"pwd":"123"}] 

However, when I try to run from my phone, I am getting these error message:

但是,当我尝试从手机运行时,我收到以下错误消息:

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

I not sure which part went wrong. Thanks in advance.

我不确定哪个部分出了问题。提前致谢。

1 个解决方案

#1


IllegalArgumentException: Illegal character in query at index 110...

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

http://192.168.0.25:8080/SoccerWebService/SoccerWebService?action=GetPwd&userName=user3&securityQuestion=Whats your pet name&securityAnswer=hiha

http://192.168.0.25:8080/SoccerWebService/SoccerWebService?action=GetPwd&userName=user3&securityQuestion=Whats your pet name&securityAnswer = hiha

At index 110 and more I can see:

在索引110以及更多我可以看到:

your pet name&securityAnswer=hiha

你的宠物名称和安全答案= hiha

Your invalid character is simply a space, which is not allowed in URLs.

您的无效字符只是一个空格,这在URL中是不允许的。

Make sure to URL encode.

确保URL编码。

#1


IllegalArgumentException: Illegal character in query at index 110...

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

http://192.168.0.25:8080/SoccerWebService/SoccerWebService?action=GetPwd&userName=user3&securityQuestion=Whats your pet name&securityAnswer=hiha

http://192.168.0.25:8080/SoccerWebService/SoccerWebService?action=GetPwd&userName=user3&securityQuestion=Whats your pet name&securityAnswer = hiha

At index 110 and more I can see:

在索引110以及更多我可以看到:

your pet name&securityAnswer=hiha

你的宠物名称和安全答案= hiha

Your invalid character is simply a space, which is not allowed in URLs.

您的无效字符只是一个空格,这在URL中是不允许的。

Make sure to URL encode.

确保URL编码。