json解析问题与安卓文本视图中的古吉拉特语字体

时间:2014-02-18 10:53:18

标签: java android json android-fonts

enter image description here我开发了一个应用程序,它有文本视图,用于显示PHP MySQL服务器数据库中json URL和数据存储中的一些Gujarati文本,因此显示Gujarati字体的问题

我的json http代码在这里......

public class CustomHttpClient {


    public static final int HTTP_TIMEOUT = 30 * 1000; 

    private static HttpClient mHttpClient;




 private static HttpClient getHttpClient() {

  if (mHttpClient == null) {
   mHttpClient = new DefaultHttpClient();

   final HttpParams params = mHttpClient.getParams();
   HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
   HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
   ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
  }

  return mHttpClient;
 }

public static String executeHttpPost(String url,ArrayList<NameValuePair> postParameters) throws Exception {

        BufferedReader in = null;

      try {

       HttpClient client = getHttpClient();

       HttpPost request = new HttpPost(url);

       UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
       postParameters);

       request.setEntity(formEntity);

       HttpResponse response = client.execute(request);

       in = new BufferedReader(new InputStreamReader(response.getEntity()
       .getContent()));


    //   in = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8000);
       StringBuffer sb = new StringBuffer("");

       String line = "";

       String NL = System.getProperty("line.separator");

       while ((line = in.readLine()) != null) {

       sb.append(line + NL);

       }

       in.close();
       String result = sb.toString();
       return result;

      } finally {

   if (in != null) {

    try {

     in.close();

    } catch (IOException e) {

     Log.e("log_tag", "Error converting result "+e.toString()); 

     e.printStackTrace();

    }

   }

  }

 }

和主要活动代码在这里..

desc_about=(TextView)v.findViewById(R.id.textdesc);

Typeface tf=Typeface.createFromAsset(getActivity().getAssets(),"Shruti.ttf");
       desc_about.setTypeface(tf);

ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();

postParameters.add(new BasicNameValuePair("temple_id","2"));



String response = null;

          try {             
                        response = CustomHttpClient.executeHttpPost(
                        url_temple,postParameters);

                        String result = response.toString();  

         try {
                 JSONArray jArray = new JSONArray(result);
                 for(int i=0;i<jArray.length();i++)
                 {
                     JSONObject json_data = jArray.getJSONObject(i);
                     about_temple=json_data.getString("about_text");
          }

         }
         catch(JSONException e){
                 Log.e("log_tag", "Error parsing data "+e.toString());
         }

 }

         try{


           desc_about.setText(about_temple);


         }
         catch(Exception e){
          Log.e("log_tag","Error in Display!" + e.toString());;
          Toast.makeText(getActivity(), "error" + 2, 100).show();
         }   
          }
          catch (Exception e) {
     Log.e("log_tag","Error in http connection!!" + e.toString());
     Toast.makeText(getActivity(), "error" + 3, 100).show();
    }

2 个答案:

答案 0 :(得分:0)

尝试在php端制作JSON时使用utf-encoding,同样在android端解码utf。我在iOS应用程序中使用这种方式解决了它,谢谢

答案 1 :(得分:0)

试试这个解决方案

 StringRequest stringRequest = new StringRequest(Request.Method.GET,"http://floming.com/shayri/guj_romanse.json", new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            String str = "";
            try {
                str = new String(response.getBytes("ISO-8859-1"), "UTF-8");
            } catch (UnsupportedEncodingException e) {

                e.printStackTrace();
            }

            String decodedStr = Html.fromHtml(str).toString();