0

I'm trying to send notification for both Android & iOS using non-Latin charset.

I notice when I send message from Android to iOS using non-Latin charset, message displayed on iPhone as "????", since the Java server side for iOS and Android are the same, I assume the problem is how I send the request from Android handset, notice message from iOS to iOS works fine.

below is the code that I'm using to open network connection and sending the request, please, let me know if it's OK.

byte[] bytes = body.getBytes(/*"UTF-16"*//*"ISO-8859-1"*/"UTF-8");

        HttpURLConnection conn = null;

        StringBuilder stringBuilder = new StringBuilder();

        try {

            conn = (HttpURLConnection) url.openConnection();//conn.setRequestProperty("Content-Type", "text/plain; charset=utf-8");
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setFixedLengthStreamingMode(bytes.length);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded;charset=UTF-8");
            // post the request
            OutputStream out = conn.getOutputStream();
            out.write(bytes);
            out.close();
            // handle the response
            int status = conn.getResponseCode();
            if (status != 200) {
                Log.d("send message", "Coud Send Message, No Internet Connection Avilable.");
                throw new IOException("Post failed with error code " + status);
            }

            InputStream in = new BufferedInputStream(conn.getInputStream());
            // readStream(in);
            int b;
            while ((b = in.read()) != -1) {
                stringBuilder.append((char) b);
            }
Cœur
  • 37,241
  • 25
  • 195
  • 267
user836026
  • 10,608
  • 15
  • 73
  • 129
  • 1
    it seems like you're using some stupid tutorial ... first reading InputStream byte by byte is not efficient, second, in that way you're putting chars as byte to StringBuilder without applying proper encoding ... you can try to read response to byte array and then apply encoding in opposite way as you're getting body bytes array ... or wrap InputStream to some kind of reader like `InputStreamReader` and then read response line by line ... – Selvin Sep 03 '12 at 13:04
  • 1
    For me the request looks ok, can you check the log of the server? – User Sep 03 '12 at 13:10
  • 1
    @Selvin the problem is the request, not the response. He gets the error displaying it on iOS, as far I understand. – User Sep 03 '12 at 13:12

1 Answers1

2

check below code it works for me, i have also same issue, to get Data from server,

String is = null;
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(URL);
            // httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs2));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs2,
                    HTTP.UTF_8));
            HttpResponse responce = httpclient.execute(httppost);
            HttpEntity entity = responce.getEntity();
            is = EntityUtils.toString(entity, "UTF-8");
        } catch (Exception e) {
            // TODO: handle exception
            Log.d("call http :", e.getMessage().toString());
            is = null;
        }
        return is;

hope it may help you.

Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
  • Thank You!! ... It works great!! ... I can't be grateful enough, thanks again!! – user836026 Sep 03 '12 at 14:03
  • Hm, I actually also use this code, and it works, yes. But strange that the poster's code doesn't work, since it gets the byte array with "UTF-8" encoding, and sends the request also with UTF-8 Content-Type... – User Sep 03 '12 at 15:17
  • Yes, it's strange, but it's OK for me since it works with the code you posted!! – user836026 Sep 03 '12 at 17:00
  • @lxx use this way : httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs2, "UTF-8")); check this----> http://stackoverflow.com/a/5286074/1168654 – Dhaval Parmar Sep 04 '12 at 09:59