JSON在Android的文本视图古吉拉特语字体分析问题 [英] json parsing issue with Gujarati font in android text view

查看:253
本文介绍了JSON在Android的文本视图古吉拉特语字体分析问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了具有文本视图显示从JSON一些古吉拉特文字应用URL和数据存储在PHP MySQL服务器数据库中,以便与显示古吉拉特语字体问题

JSON HTTP我的code是这里...

 公共类CustomHttpClient {
    公共静态最终诠释HTTP_TIMEOUT = 30 * 1000;    私有静态HttpClient的mHttpClient;
 私有静态HttpClient的getHttpClient(){  如果(mHttpClient == NULL){
   mHttpClient =新DefaultHttpClient();   最终的HttpParams PARAMS = mHttpClient.getParams();
   HttpConnectionParams.setConnectionTimeout(参数,可以HTTP_TIMEOUT);
   HttpConnectionParams.setSoTimeout(参数,可以HTTP_TIMEOUT);
   ConnManagerParams.setTimeout(参数,可以HTTP_TIMEOUT);
  }  返回mHttpClient;
 }公共静态字符串executeHttpPost(字符串URL,ArrayList的<&的NameValuePair GT; postParameters)抛出异常{        在的BufferedReader = NULL;      尝试{       HttpClient的客户= getHttpClient();       HttpPost要求=新HttpPost(URL);       UrlEn codedFormEntity formEntity =新UrlEn codedFormEntity(
       postParameters);       request.setEntity(formEntity);       HTT presponse响应= client.execute(请求);       在=新的BufferedReader(新的InputStreamReader(response.getEntity()
       .getContent()));
    //在=新的BufferedReader(新的InputStreamReader(是,UTF-8),8000);
       StringBuffer的SB =新的StringBuffer();       串线=;       串NL = System.getProperty(line.separator);       而((行= in.readLine())!= NULL){       sb.append(行+ NL);       }       附寄();
       字符串结果= sb.toString();
       返回结果;      } {最后   如果(在!= NULL){    尝试{     附寄();    }赶上(IOException异常五){     Log.e(log_tag,错误转换结果+ e.toString());     e.printStackTrace();    }   }  } }

和主要活动code在这里..

  desc_about =(TextView中)v.findViewById(R.id.textdesc);字体TF = Typeface.createFromAsset(getActivity()getAssets(),Shruti.ttf);
       desc_about.setTypeface(TF);ArrayList的<&的NameValuePair GT; postParameters =新的ArrayList<&的NameValuePair GT;();postParameters.add(新BasicNameValuePair(temple_id,2));串响应=无效;          尝试{
                        响应= CustomHttpClient.executeHttpPost(
                        url_temple,postParameters);                        字符串结果= response.toString();         尝试{
                 JSONArray jArray =新JSONArray(结果);
                 的for(int i = 0; I< jArray.length();我++)
                 {
                     JSONObject的json_data = jArray.getJSONObject(I)
                     about_temple = json_data.getString(about_text);
          }         }
         赶上(JSONException E){
                 Log.e(log_tag,错误分析数据+ e.toString());
         } }         尝试{
           desc_about.setText(about_temple);
         }
         赶上(例外五){
          Log.e(log_tag,中显示错误!+ e.toString());;
          Toast.makeText(getActivity(),错误+ 2,100).show();
         }
          }
          赶上(例外五){
     Log.e(log_tag,在HTTP连接错误!+ e.toString());
     Toast.makeText(getActivity(),错误+ 3,100).show();
    }


解决方案

尝试使用UTF-endcoding在PHP的一侧,同样的方式去code UTF Android中端使JSON的时间。我解决了它在iOS应用中使用这种方式,谢谢

I have developed an application that have text view for display some Gujarati text from the json URL and data store in PHP MySQL server database so problem with display Gujarati font

my code of json http is here...

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();

    }

   }

  }

 }

and main activity code here..

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();
    }

解决方案

Try using utf-endcoding at the time of making JSON on php side and same way decode utf in android side. I solved it using this way in iOS app, Thanks

这篇关于JSON在Android的文本视图古吉拉特语字体分析问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆