Android Fatal Error: Async Task

Android Fatal Error: Async Task

嘿,我的应用程序有一个我无法解决的问题,我尝试了我所知道的关于 Async Task 的一切,但我并不擅长。

错误:

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.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.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.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask.run(FutureTask.java:234)
            at android.os.AsyncTask$SerialExecutor.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)

代码创建团队任务:

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

PHP代码:

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

有人可以帮我解决这个问题吗?我被这个错误困住了,有趣的是我有几乎相同的用户注册代码,但它没有给出这个错误。

谢谢抽出时间。

您的 Url 中有非法字符(主要是空格和冒号),您需要确保使用正确的 url 编码。实现此目的的一种简单方法是使用 Uri.Builder() 来构建您的 URL.

编辑:

在您的异常中,它显示以下 URL 引发错误:“http://sqlphptry.co.nf/createUser.php?teamName=vcxvxcvcxvxcvxc&game=COD: AW&consola=Xbox One”。请注意,即使在此处显示,第一个非法字符也会破坏超链接。

要清理此 Url,您需要正确附加查询参数,例如使用上面的 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