用Java和JSON编码希伯来语

用Java和JSON编码希伯来语,第1张

概述所以我有一个Android应用程序,显示一些消息.我可以将消息从应用程序添加到Web服务的数据库中,但是我很难将希伯来编码为正确的格式.这是我的JSONParser.class:packagecom.example.neotavraham;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.I

所以我有一个Android应用程序,显示一些消息.我可以将消息从应用程序添加到Web服务的数据库中,但是我很难将希伯来语编码为正确的格式.

这是我的JsONParser.class:

package com.example.neotavraham;import java.io.BufferedReader;import java.io.IOException;import java.io.inputStream;import java.io.inputStreamReader;import java.io.UnsupportedEnCodingException;import java.util.List;import org.apache.http.httpentity;import org.apache.http.httpResponse;import org.apache.http.nameValuePair;import org.apache.http.clIEnt.ClIEntProtocolException;import org.apache.http.clIEnt.entity.UrlEncodedFormEntity;import org.apache.http.clIEnt.methods.httpGet;import org.apache.http.clIEnt.methods.httpPost;import org.apache.http.clIEnt.utils.URLEncodedUtils;import org.apache.http.impl.clIEnt.DefaulthttpClIEnt;import org.Json.JsONException;import org.Json.JsONObject;import androID.util.Log;public class JsONParser {static inputStream is = null;static JsONObject jObj = null;static String Json = "";// constructorpublic JsONParser() {}public JsONObject getJsONFromUrl(final String url) {    // Making http request    try {        // Construct the clIEnt and the http request.        DefaulthttpClIEnt httpClIEnt = new DefaulthttpClIEnt();        httpPost httpPost = new httpPost(url);        // Execute the POST request and store the response locally.        httpResponse httpResponse = httpClIEnt.execute(httpPost);        // Extract data from the response.        httpentity httpentity = httpResponse.getEntity();        // Open an inputStream with the data content.        is = httpentity.getContent();    } catch (UnsupportedEnCodingException e) {        e.printstacktrace();    } catch (ClIEntProtocolException e) {        e.printstacktrace();    } catch (IOException e) {        e.printstacktrace();    }    try {        // Create a BufferedReader to parse through the inputStream.        BufferedReader reader = new BufferedReader(new inputStreamReader(                is, "iso-8859-1"), 8);        // Declare a string builder to help with the parsing.        StringBuilder sb = new StringBuilder();        // Declare a string to store the JsON object data in string form.        String line = null;        // Build the string until null.        while ((line = reader.readline()) != null) {            sb.append(line + "\n");        }        // Close the input stream.        is.close();        // Convert the string builder data to an actual string.        Json = sb.toString();    } catch (Exception e) {        Log.e("Buffer Error", "Error converting result " + e.toString());    }    // Try to parse the string to a JsON object    try {        jObj = new JsONObject(Json);    } catch (JsONException e) {        Log.e("JsON Parser", "Error parsing data " + e.toString());    }    // Return the JsON Object.    return jObj;}// function get Json from url// by making http POST or GET mehtodpublic JsONObject makehttpRequest(String url, String method,        List<nameValuePair> params) {    // Making http request    try {        // check for request method        if(method == "POST"){            // request method is POST            // defaulthttpClIEnt            DefaulthttpClIEnt httpClIEnt = new DefaulthttpClIEnt();            httpPost httpPost = new httpPost(url);            httpPost.setEntity(new UrlEncodedFormEntity(params));            httpResponse httpResponse = httpClIEnt.execute(httpPost);            httpentity httpentity = httpResponse.getEntity();            is = httpentity.getContent();        }else if(method == "GET"){            // request method is GET            DefaulthttpClIEnt httpClIEnt = new DefaulthttpClIEnt();            String paramString = URLEncodedUtils.format(params, "utf-8");            url += "?" + paramString;            httpGet httpGet = new httpGet(url);            httpResponse httpResponse = httpClIEnt.execute(httpGet);            httpentity httpentity = httpResponse.getEntity();            is = httpentity.getContent();        }               } catch (UnsupportedEnCodingException e) {        e.printstacktrace();    } catch (ClIEntProtocolException e) {        e.printstacktrace();    } catch (IOException e) {        e.printstacktrace();    }    try {        BufferedReader reader = new BufferedReader(new inputStreamReader(                is, "iso-8859-1"), 8);        StringBuilder sb = new StringBuilder();        String line = null;        while ((line = reader.readline()) != null) {            sb.append(line + "\n");        }        is.close();        Json = sb.toString();    } catch (Exception e) {        Log.e("Buffer Error", "Error converting result " + e.toString());    }    // try parse the string to a JsON object    try {        jObj = new JsONObject(Json);    } catch (JsONException e) {        Log.e("JsON Parser", "Error parsing data " + e.toString());    }    // return JsON String    return jObj;}}

这是从用户那里获取信息并尝试对其进行编码的行:

        String post_Title = Title.getText().toString();        String post_message = message.getText().toString();        byte ptext[] = post_Title.getBytes(windows_1255);         String value = new String(ptext, UTF_8);        post_Title = value;        byte ptext2[] = post_message.getBytes(windows_1255);         value = new String(ptext2, UTF_8);        post_message = value;

时间:public static final Charset windows_1255 = Charset.forname(“ windows-1255”);
        公共静态最终字符集UTF_8 = Charset.forname(“ UTF-8”);

因此,例如,如果标题为“שלום”(希伯来语,您好),它将显示为“ ????” (仅问号).

一切都能在应用程序中完美运行,因此不能与编码部分无关!

我能做什么?

解决方法:

我找到了解决方案.

此功能对我有帮助:

// convert from internal Java String format -> UTF-8public static String convertToUTF8(String s) {    String out = null;    try {        out = new String(s.getBytes("UTF-8"), "ISO-8859-1");    } catch (java.io.UnsupportedEnCodingException e) {        return null;    }    return out;}
总结

以上是内存溢出为你收集整理的用Java和JSON编码希伯来语全部内容,希望文章能够帮你解决用Java和JSON编码希伯来语所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1089202.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存