java - Android Fatal Error: Async Task
Hey I am having a problem with my application that I can't resolve, I tryed everything I knew about Async Task but I am not good at it.
Error:
03-17 19:43:38.794 1662-1681/com.cwpsiproject E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #5
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.IllegalArgumentException: Illegal character in query at index 72: http://sqlphptry.co.nf/createUser.php?teamName=vcxvxcvcxvxcvxc&game=COD: AW&consola=Xbox One
at java.net.URI.create(URI.java:727)
at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
at com.cwpsiproject.ApiConnector.createTeam(ApiConnector.java:196)
at com.cwpsiproject.CreateTeamActivity$createTeamTask.doInBackground(CreateTeamActivity.java:131)
at com.cwpsiproject.CreateTeamActivity$createTeamTask.doInBackground(CreateTeamActivity.java:124)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
Code CreateTeamTask:
private class createTeamTask extends AsyncTask<ApiConnector,Long,JSONArray> {
@Override
protected JSONArray doInBackground(ApiConnector... params) {
// it is executed on Background thread
String teamName = f.getTeamNameC(); // returns string with teamName
String game = f.getGameC(); // returns string with game
String consola = f.getConsoleC(); // returns string with console
return params[0].createTeam(teamName, game, consola);
}
@Override
protected void onPostExecute(JSONArray jsonArray) {
Toast.makeText(CreateTeamActivity.this, "Team Successfully Created!", Toast.LENGTH_SHORT).show();
}
}
ApiConnector code:
public JSONArray createTeam(String teamName, String game, String consola) {
String url = "http://sqlphptry.co.nf/createUser.php?teamName="+teamName+"&game="+game+"&consola="+consola;
// Get HttpResponse Object from url.
// Get HttpEntity from Http Response Object
HttpEntity httpEntity = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient(); // Default HttpClient
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
httpEntity = httpResponse.getEntity();
} catch (ClientProtocolException e) {
// Signals error in http protocol
e.printStackTrace();
//Log Errors Here
} catch (IOException e) {
e.printStackTrace();
}
// Convert HttpEntity into JSON Array
JSONArray jsonArray = null;
if (httpEntity != null) {
try {
String entityResponse = EntityUtils.toString(httpEntity);
Log.e("Entity Response : ", entityResponse);
jsonArray = new JSONArray(entityResponse);
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return jsonArray;
}
PHPCode:
include 'connection.php';
if (isset($_GET['teamName']) && isset($_GET['game']) && isset($_GET['consola'])) {
$teamName = $_GET['teamName'];
$game = $_GET['game'];
$consola = $_GET['consola'];
$teamName = mysql_real_escape_string($teamName);
$game = mysql_real_escape_string($game);
$consola = mysql_real_escape_string($consola);
mysql_query("INSERT INTO `team_data` (`teamName`, `game`, `console`) VALUES('$teamName','$game','$consola')");
print('[{"created":"'.$teamName.'"}]');
}
mysql_close($dbhandle);
Can someone please help me with this? I am stuck at this error, the funny thing is that I have almost the same code for registering the user and it doesn't give this error.
thanks for the time.
Answer
Solution:
You have illegal characters in your Url (mainly the spaces, and colon), you need to make sure you're using correct url encoding. An easy way to accomplish this, would be to use Uri.Builder() to build your URL.
Edit:
In your exception, it shows the following URL throws the error: "http://sqlphptry.co.nf/createUser.php?teamName=vcxvxcvcxvxcvxc&game=COD: AW&consola=Xbox One". Note that even as displayed here, the first illegal character breaks the hyperlink.
To sanitize this Url, you need to append the query parameters correctly, for example using the above url:
The result: http://sqlphptry.co.nf/createUser.php?teamName=vcxvxcvcxvxcvxc&game=COD%3A%20AW&consola=Xbox%20One