本文介绍了使用网络连接系统,发送UTF8字符集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图发送通知为Android和放大器;使用非拉丁字符集的iOS。

我发现当我发送消息从Android的使用非拉丁字符集,如????在iPhone上显示的消息的iOS,因为Java服务器端的iOS和Android都是一样的,我认为这个问题是怎么我送从Android手机的要求,从iOS版到iOS通知信息工作正常。

下面是code,我使用打开网络连接,并发送请求,请让我知道这是否正常。

 字节[]字节= body.getBytes(/ *UTF-16* // *ISO-8859-1* /UTF-8);        HttpURLConnection的康恩= NULL;        StringBuilder的StringBuilder的=新的StringBuilder();        尝试{            康恩=(HttpURLConnection类)url.openConnection(); // conn.setRequestProperty(内容类型,text / plain的;字符集= UTF-8);
            conn.setDoOutput(真);
            conn.setUseCaches(假);
            conn.setFixedLengthStreamingMode(bytes.length);
            conn.setRequestMethod(POST);
            conn.setRequestProperty(内容类型,
                    应用程序/ x-WWW的形式urlen codeD;字符集= UTF-8);
            //张贴请求
            OutputStream的OUT = conn.getOutputStream();
            out.write(字节);
            out.close();
            //处理响应
            INT状态= conn.getResponse code();
            如果(状态!= 200){
                Log.d(发送消息,Coud发送信息,无Internet连接即时拍摄。);
                抛出新IOException异常(+状态后,错误code失败);
            }            在的InputStream =新的BufferedInputStream(conn.getInputStream());
            // readStream(在);
            INT B:
            而((二= in.read())!= - 1){
                stringBuilder.append((char)的B);
            }


解决方案

检查以下code它为我工作,我也有同样的问题,
从服务器获取数据,

 字符串为= NULL;
        尝试{
            HttpClient的HttpClient的=新DefaultHttpClient();
            HttpPost httppost =新HttpPost(URL);
            // httppost.setEntity(新UrlEn codedFormEntity(nameValuePairs2));
            httppost.setEntity(新UrlEn codedFormEntity(nameValuePairs2,
                    HTTP.UTF_8));
            HTT presponse的反响= httpclient.execute(httppost);
            HttpEntity实体= responce.getEntity();
            是= EntityUtils.toString(实体,UTF-8);
        }赶上(例外五){
            // TODO:处理异常
            Log.d(调用HTTP:e.getMessage()的toString());
            是=无效;
        }
        回报;

希望它可以帮助你。

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);
            }
解决方案

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.

这篇关于使用网络连接系统,发送UTF8字符集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 08:23