可以在android中显示来自json响应的本机字符

时间:2014-10-13 08:32:29

标签: java android json

我想在android中使用textview显示hindi字符,这是来自指定url的json响应。 glassfishserver输出屏幕中的json响应如下所示

{“combo”:[{“qid”:“7H1”,“qstn”:“? - ???”,“ans”:“?”,“op1”:“?”,“ OP2 “:”?“}]}

每个?马克是印地文人物。

在android中它会显示类似àô-àô??

的内容

所有这些印地文文都以utf8的形式存储在mysql中。

{con = DriverManager.getConnection(“jdbc:mysql:// localhost:3306 / edupad ?? useUnicode = true& characterEncoding = UTF-8”,“root”,“root”);}

当使用代码

直接将印地文文本设置为textview时

textview.settext(“क - म??”); 它会正确显示印地语字符。

我该怎么做才能在Android中正确显示url中的hindi字符.. ???

jsp页面在下面给出

 <%@page import="com.edupad.quiz_list"%>
<%@page import="com.edupad.Lang_List"%>
<%@page import="com.google.gson.Gson"%>
<%@page import="com.edupad.dbclass"%>
<%@page import="com.edupad.Language_bean"%>
<%@page import="com.sun.org.apache.bcel.internal.generic.LAND"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
try {
    Class.forName("com.mysql.jdbc.Driver");
    Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/edupad?        useUnicode=yes&characterEncoding=utf-8", "root", "root");;
      Statement s = cn.createStatement();

    ResultSet re = s.executeQuery("select * from language_tab");

  Gson gn = new Gson();



    String lang = request.getParameter("lang");
    System.out.println("lang=" + lang);

    String type = request.getParameter("type");
    System.out.println("type=" + type);
    List<quiz_list> lst = new ArrayList<quiz_list>();
    //   lst = db.reqst_qstns(lang, type);

    try {
        String q = "select qid,qstn,answer,op1,op2 from quiz_tab,language_tab,type_tab where         quiz_tab.lid=language_tab.lid and quiz_tab.tid=type_tab.tid and type_tab.type_name='" + type + "' and language_tab.lang_name='" + lang + "'";

        System.out.println("q=" + q);
        re = s.executeQuery(q);

        while (re.next()) {


            lst.add(new quiz_list(re.getString(1), re.getString(2), re.getString(3), re.getString(4), re.getString(5)));

        }

    } catch (Exception ex) {
        System.out.println("@reqst_example" + ex);
    }



    System.out.println("lst size=" + lst.size());



    String msg = gn.toJson(lst);


    out.println("{\"combo\":" + msg + "}");

    System.out.println("{\"combo\":" + msg + "}");

} catch (Exception ex) {
    System.out.println("@reqst_example" + ex);
}
 %>

获取json响应的函数在下面给出

try {
        ArrayList<NameValuePair> npv = new ArrayList<NameValuePair>();

        npv.add(new BasicNameValuePair("lang", lang));
        npv.add(new BasicNameValuePair("type", type));




        String url = "http://" + ip.trim() + ":8080/edupad/faces/questions.jsp";


        jsonparser jp = new jsonparser();
        JSONObject jsson = jp.getJSONfromURL(url,    getApplicationContext().getApplicationContext(), npv);
        JSONArray jSONArray = jsson.getJSONArray("combo");

        qstns = new String[jSONArray.length()];
        ans = new String[jSONArray.length()];
        op1 = new String[jSONArray.length()];
        op2 = new String[jSONArray.length()];
        r = new int[jSONArray.length()];
        for (int d = 0; d < jSONArray.length(); d++) {
            JSONObject jsn = jSONArray.getJSONObject(d);

            qstns[d]=jsn.getString("qstn");
            ans[d] = jsn.getString("ans");
            op1[d] = jsn.getString("op1");
            op2[d] = jsn.getString("op2");

        }
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), " exc==" + e, Toast.LENGTH_LONG).show();
    }
}

0 个答案:

没有答案