【问题标题】:JSON parsing issue with Gujarati font in Android text viewAndroid 文本视图中古吉拉特语字体的 JSON 解析问题
【发布时间】:2014-02-18 10:53:17
【问题描述】:

我开发了一个应用程序,它具有文本视图,用于显示来自 JSON URL 的一些古吉拉特语文本和存储在 PHP MySQL 服务器数据库中的数据。

所以,显示古吉拉特语字体的问题:

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

【问题讨论】:

  • 使用这一行:new BufferedReader(new InputStreamReader(is,"iso-8859-1"), 8);
  • 得到错误空指针异常

标签: java android json android-fonts


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    试试这个解决方案

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-23
      • 2013-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多