title: Android Servlet AsyncTask #1 Fatal Exception
body: <p>I was having some problem when trying to retrieve data via Servlet into Android using JSON. So basically here is my controller class:</p>
  
  <pre><code>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&amp;userName="
                  + userName + "&amp;securityQuestion=" + securityQuestion + "&amp;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 &lt; length; i++) {
                  JSONObject attribute = jsonArray.getJSONObject(i);
                  pwd = attribute.getString("pwd");
              }
          } catch (JSONException e) {
              e.printStackTrace();
          }
      } catch (IOException e) {
          e.printStackTrace();
      }
      return pwd;
  }
  </code></pre>
  
  <p>And my AsyncTaskClass:</p>
  
  <pre><code>public class GetPwdAsyncTask extends AsyncTask&lt;SoccerUserModel, Integer, Double&gt; {
  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) {
  }
  
  }
  </code></pre>
  
  <p>And then my SQL part located inside the servlet:</p>
  
  <pre><code>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());
  
      }
  </code></pre>
  
  <p>I have tested with this URL: <a href="http://localhost:8080/SoccerWebService/SoccerWebService?action=GetPwd&amp;userName=user&amp;securityQuestion=Who" rel="nofollow">http://localhost:8080/SoccerWebService/SoccerWebService?action=GetPwd&amp;userName=user&amp;securityQuestion=Who</a> am I&amp;securityAnswer=user which returning me this result:</p>
  
  <pre><code>[{"pwd":"123"}] 
  </code></pre>
  
  <p>However, when I try to run from my phone, I am getting these error message:</p>
  
  <pre><code>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&amp;userName=user3&amp;securityQuestion=Whats your pet name&amp;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.&lt;init&gt;(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
  </code></pre>
  
  <p>I not sure which part went wrong. Thanks in advance.</p>