Android致命错误:异步任务

时间:2015-03-17 19:53:42

标签: java php android android-asynctask

嘿,我的应用程序出了问题我无法解决,我尝试了所有关于异步任务的知识,但我并不擅长。

错误:

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)

代码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代码:

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);

有人可以帮我解决这个问题吗?我遇到了这个错误,有趣的是,我有几乎相同的代码注册用户,并没有给出这个错误。< / p>

感谢您的时间。

1 个答案:

答案 0 :(得分:1)

您的网址中包含非法字符(主要是空格和冒号),您需要确保使用正确的网址编码。一个简单的方法是使用Uri.Builder()来构建你的URL。

编辑:

在您的例外情况下,它会显示以下网址引发错误:&#34; http://sqlphptry.co.nf/createUser.php?teamName=vcxvxcvcxvxcvxc&game=COD:AW&amp; consola = Xbox One&#34;。请注意,即使在此处显示,第一个非法字符也会破坏超链接。

要清理此Url,您需要正确附加查询参数,例如使用上面的网址:

    Uri.Builder builder = Uri.parse("http://sqlphptry.co.nf/createUser.php").buildUpon();
        builder.appendQueryParameter("teamName", "vcxvxcvcxvxcvxc");
        builder.appendQueryParameter("game", "COD: AW");
        builder.appendQueryParameter("consola", "Xbox One");

    String url = builder.build().toString();

结果:http://sqlphptry.co.nf/createUser.php?teamName=vcxvxcvcxvxcvxc&game=COD%3A%20AW&consola=Xbox%20One