3

Here is my code:

HttpClient client = new DefaultHttpClient();
            client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "android");
            HttpGet request = new HttpGet();
            request.setHeader("Content-Type", "text/plain; charset=utf-8");
            Log.d("URL", convertURL(URL));
            request.setURI(new URI(URL));
            HttpResponse response = client.execute(request);
            bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer stringBuffer = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");

I don't know which error in my URL:

http://localhost/CyborgService/chatservice.php?action=recive_game&nick_sender=mkdarkness&pass=MV030595&date_last=2012-11-18 09:46:37&id_game=1

I have already used a function to convert URL, but has not worked. But, if I trying open this URL in my Browser, it opens successfully.

Here is my error:

11-18 21:46:37.766: E/GetHttp(823): java.net.URISyntaxException: Illegal character in query at index 127: http://192.168.0.182/CyborgService/chatservice.php?action=recive_game&nick_sender=mkdarkness&pass=MV030595&date_last=2012-11-18 09:46:37&id_game=1
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Marcos Bergamo
  • 1,073
  • 2
  • 11
  • 19
  • I haven't counted out the 127 characters but I would guess the space in `2012-11-18 09:46:37` is the problem... – Sam Nov 18 '12 at 21:59

3 Answers3

10

There is a space in your URL, in position 127. The date is generated as "date_last=2012-11-18 09:46:37", which causes an error when opening the URL.

Spaces are not formally accepted in URLs, although your browser will happily convert it to "%20" or to "+", both valid representations of a space in a URL. You should escape all characters: you can replace space with "+" or just pass the String through URLEncoder and be done with it.

To use URLEncoder see e.g. this question: encode with URLEncoder only parameter values, not the full URL. Or use one of the constructors for URI which have a few parameters, not a single one. You are not showing the code that constructs the URL so I cannot comment on it explicitly. But if you have a map of parameters parameterMap it would be something like:

String url = baseUrl + "?";
for (String key : parameterMap.keys())
{
  String value = parameterMap.get(key);
  String encoded = URLEncoder.encode(value, "UTF-8");
  url += key + "&" + encoded;
}

Some other day we can talk about why Java requires to set the encoding and then requires that the encoding be "UTF-8", instead of just using "UTF-8" as the default encoding, but for now this code should do the trick.

Community
  • 1
  • 1
alexfernandez
  • 1,938
  • 3
  • 19
  • 30
  • 1
    Yea, you need to use URLEncoder. It is far more than spaces that are problems. This will generate the encoded URL to use in all cases. +1 – IAmGroot Nov 18 '12 at 22:08
  • alexinblue, I trying using URLEncoder, but unsuccessfully my url is thus: http%3A%2F%2F192.168.0.182%2FCyborgService%2Fchatservice.php%3Faction%3Drecive_game%26nick_sender%3Dmkdarkness%26pass%3DMV030595%26date_last%3D2012-11-19+12%3A40%3A05%26id_game%3D1 And other error: java.lang.IllegalStateException: Target host must not be null, or set in parameters. – Marcos Bergamo Nov 19 '12 at 00:42
  • Thanks for accepting, I will add a paragraph about how to use URLEncoder. – alexfernandez Nov 19 '12 at 08:48
2

There is a whitespace character:

...2012-11-18 09:46:37... (at index 127, just like the error message says).

Try replacing it with %20

jlordo
  • 37,490
  • 6
  • 58
  • 83
0

Do this way it will definetly help you

    HttpClient myClient = new DefaultHttpClient();

            HttpPost myConnection = new HttpPost("http://192.168.1.2/AndroidApp/SendMessage");

            try {

    //Your parameter should be as..

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("messageText", msgText));
                nameValuePairs.add(new BasicNameValuePair("senderUserInfoId", loginUserInfoId));

//set parameters to ur URL

                myConnection.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//execute the connection
                HttpResponse response = myClient.execute(myConnection);
    }
    catch (ClientProtocolException e) {

                //e.printStackTrace();
            } catch (IOException e) {
                //e.printStackTrace();
            }