通过HttpURLConnection将Utf8(波斯语)发送到Server

时间:2015-06-20 14:02:05

标签: android utf-8 httpurlconnection

我想将我的信息发送到服务器 但我不能发送波斯语(波斯语)字符

请帮助我..

给我一​​个示例代码

1 个答案:

答案 0 :(得分:1)

尝试使用UTF-8编码,这是如何使用POST请求进行编码的示例:

    try {
        URL url = new URL("your url");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setDoOutput(true);

        OutputStreamWriter writer = new OutputStreamWriter(
                conn.getOutputStream(), "UTF-8");
        String request = "test data";
        writer.write(request);
        writer.flush();
        System.out.println("Code:" + conn.getResponseCode());
        System.out.println("mess:" + conn.getResponseMessage());

        String response = "";
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                conn.getInputStream(), "UTF-8"));
        String line;
        while ((line = reader.readLine()) != null) {
            response += line;
        }

        System.out.println(new String(response.getBytes(), "UTF8"));
        writer.close();
        reader.close();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }