makeHttpRequest

时间:2015-05-29 05:19:19

标签: java android

我收到java.lang.IllegalStateException: Target host must not be null, or set in parameters错误。在进行makeHttpRequest时是新行字符,还是我在这里的路径不正确? 代码:

        String url = "http://www.<myDomain>.co.za/insertTeamResultwithCode.php?Code="+code+
                "&Section="+section+
                "&Gender="+gender+
                "&WinningTeam="+newResult.getWinningTeam()+
                "&LosingTeam="+newResult.getLosingTeam()+
                "&FixtureD="+newResult.getDate()+
                "&FixtureT="+newResult.getTime()+
                "&Venue="+newResult.getVenue()+
                "&Court="+newResult.getCourt()+
                "&Texts="+newResult.getText();

        try 
        {
            String encodedUrl = URLEncoder.encode(url, "UTF-8");

            JSONParser jParser = new JSONParser();
            JSONObject json = jParser.makeHttpRequest(encodedUrl, "GET", isSuccessfull);

            try
            {
                boolean success = (json.getBoolean("isValid"));
                DisplayToast(success);
            }
            catch (JSONException d)
            {
                d.printStackTrace();
            }
        }
        catch (UnsupportedEncodingException e) 
        {
            e.printStackTrace();
        }

和logcat错误:

05-29 06:55:49.230: E/AndroidRuntime(25416): java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=http://www.<myDomain>.co.za/insertTeamResultwithCode.php?Code=masjiensquash2015&Section=A&Gender=Men&WinningTeam=KZN&LosingTeam=WP&FixtureD=2015-05-29&FixtureT=09:00:00&Venue=Potch+CC&Court=1&Texts=mufasa+(KZN)+beat+scar+(WP)+3-0
05-29 06:55:49.230: E/AndroidRuntime(25416): (11-8,+11-6,+11-4)
05-29 06:55:49.230: E/AndroidRuntime(25416): optimus+(KZN)+beat+megatron+(WP)+3-0
05-29 06:55:49.230: E/AndroidRuntime(25416): (11-6,+11-3,+11-8)
05-29 06:55:49.230: E/AndroidRuntime(25416): Chuck+(KZN)+beat+Larry+(WP)+3-0
05-29 06:55:49.230: E/AndroidRuntime(25416): (11-5,+11-4,+11-4)
05-29 06:55:49.230: E/AndroidRuntime(25416): Tiger+(KZN)+beat+Rory+(WP)+3-0
05-29 06:55:49.230: E/AndroidRuntime(25416): (11-6,+11-3,+11-6)
05-29 06:55:49.230: E/AndroidRuntime(25416): Zandre+(KZN)+beat+Anzelle+(WP)+3-0
05-29 06:55:49.230: E/AndroidRuntime(25416): (11-5,+11-6,+11-2)

1 个答案:

答案 0 :(得分:2)

您不应该对完整的URL进行编码,因为它也会对主机部分进行编码,如下所示:

http%3A%2F%2Fwww.%3CmyDomain%3E.co.za%2FinsertTeamResultwithCode.php

这将失败,因为它无法解析此网址。

您应该只编码传递给此URL的参数。