انجمن سایت کلیدستان


رتبه موضوع:
  • 1 رای - 5 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
مشکل در دریافت متن فارسی json

مشکل در دریافت متن فارسی json

#1
با سلام به مدیریت و کاربران گرامی من یک مشکلی داشتم در دریافت اطلاعات متن فارسی به صورت ؟؟؟؟ میاد

این php ام

link

این هم کد دریافت json

کد پی‌اچ‌پی:
package com.test.readchat;

import java.io.UnsupportedEncodingException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.test.readchat.R.id;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class 
MainActivity extends ListActivity {
 
   private static String url;
 
   private static final String json_name "chat";
 
   private static final String json_node_name_1 "msg";
 
   JSONArray chat null;
 
   public String app_id_string;
 
   public String[] json_string_1_all;
 
   public int json_length;
 
   public ListView lv;
    @
Override
    
protected void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);
        
setContentView(R.layout.activity_main);
        
url "Test.com/Test.php";
        new 
JSONParse().execute();
    }
    public class 
JSONParse extends AsyncTask<StringStringJSONObject> {
        public 
ProgressDialog pDialog;
 
      @Override
       
public void onPreExecute() {
 
          super.onPreExecute();
 
          pDialog = new ProgressDialog(MainActivity.this);
 
          pDialog.setMessage("Getting Data ...");
 
          pDialog.setIndeterminate(false);
 
          pDialog.setCancelable(true);
 
          pDialog.show();
 
      }
 
      @Override
       
public JSONObject doInBackground(String... args) {
 
          JSONParser jParser = new JSONParser(); 
 
          JSONObject json jParser.getJSONFromUrl(url);
 
          return json;
 
      }
 
       @Override
        public void onPostExecute
(JSONObject json) {
 
           pDialog.dismiss();
 
           try    
                   chat 
json.getJSONArray(json_name);
 
                  json_length chat.length();
 
                  json_string_1_all = new String [chat.length()];
 
                  for(int i 0chat.length(); i++){
 
                      JSONObject c chat.getJSONObject(i);
 
                      json_string_1_all[i] = new String(c.getString(json_node_name_1).getBytes("ISO-8859-1"),"UTF-8");
 
                  }
 
                  setListAdapter(new MyAdapter(MainActivity.this
 
                             R.layout.listitem
 
                             R.id.textView1,
 
                             json_string_1_all));    
 
                  lv getListView();      
           
} catch (JSONException e) {
 
              e.printStackTrace();
 
          } catch (UnsupportedEncodingException e1) {
 
              e1.printStackTrace();
         
  }
 
       }
 
  }
    
    
    private class 
MyAdapter extends ArrayAdapter<String>{

        public 
MyAdapter(Context contextint resourceint textViewResourceId,
                
String[] strings) {
            
super(contextresourcetextViewResourceIdstrings);
            
// TODO Auto-generated constructor stub
        
}
        
        @
Override
        
public View getView(int positionView convertViewViewGroup parent) {
             
   LayoutInflater inflater = (LayoutInflatergetSystemService(Context.LAYOUT_INFLATER_SERVICE);
         
       View row inflater.inflate(R.layout.listitemparentfalse);
                
String msg json_string_1_all[position];
                
TextView tv_1 = (TextViewrow.findViewById(R.id.textView1);
                
tv_1.setText(msg);
                
            return 
row;
        }
        
        
    }



لطفا راهنمایی کنید.با تشکر
  Huh Huh
پاسخ

مشکل در دریافت متن فارسی json

#2
(۱۳۹۵/۰۳/۱۳, ۱۲:۵۴ ب.ظ)kingtaha نوشته: با سلام به مدیریت و کاربران گرامی من یک مشکلی داشتم در دریافت اطلاعات متن فارسی به صورت ؟؟؟؟ میاد

این php ام

link

این هم کد دریافت json

کد پی‌اچ‌پی:
package com.test.readchat;

import java.io.UnsupportedEncodingException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.test.readchat.R.id;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class 
MainActivity extends ListActivity {
 
   private static String url;
 
   private static final String json_name "chat";
 
   private static final String json_node_name_1 "msg";
 
   JSONArray chat null;
 
   public String app_id_string;
 
   public String[] json_string_1_all;
 
   public int json_length;
 
   public ListView lv;
    @
Override
    
protected void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);
        
setContentView(R.layout.activity_main);
        
url "Test.com/Test.php";
        new 
JSONParse().execute();
    }
    public class 
JSONParse extends AsyncTask<StringStringJSONObject> {
        public 
ProgressDialog pDialog;
 
      @Override
       
public void onPreExecute() {
 
          super.onPreExecute();
 
          pDialog = new ProgressDialog(MainActivity.this);
 
          pDialog.setMessage("Getting Data ...");
 
          pDialog.setIndeterminate(false);
 
          pDialog.setCancelable(true);
 
          pDialog.show();
 
      }
 
      @Override
       
public JSONObject doInBackground(String... args) {
 
          JSONParser jParser = new JSONParser(); 
 
          JSONObject json jParser.getJSONFromUrl(url);
 
          return json;
 
      }
 
       @Override
        public void onPostExecute
(JSONObject json) {
 
           pDialog.dismiss();
 
           try    
                   chat 
json.getJSONArray(json_name);
 
                  json_length chat.length();
 
                  json_string_1_all = new String [chat.length()];
 
                  for(int i 0chat.length(); i++){
 
                      JSONObject c chat.getJSONObject(i);
 
                      json_string_1_all[i] = new String(c.getString(json_node_name_1).getBytes("ISO-8859-1"),"UTF-8");
 
                  }
 
                  setListAdapter(new MyAdapter(MainActivity.this
 
                             R.layout.listitem
 
                             R.id.textView1,
 
                             json_string_1_all));    
 
                  lv getListView();      
           
} catch (JSONException e) {
 
              e.printStackTrace();
 
          } catch (UnsupportedEncodingException e1) {
 
              e1.printStackTrace();
         
  }
 
       }
 
  }
    
    
    private class 
MyAdapter extends ArrayAdapter<String>{

        public 
MyAdapter(Context contextint resourceint textViewResourceId,
                
String[] strings) {
            
super(contextresourcetextViewResourceIdstrings);
            
// TODO Auto-generated constructor stub
        
}
        
        @
Override
        
public View getView(int positionView convertViewViewGroup parent) {
             
   LayoutInflater inflater = (LayoutInflatergetSystemService(Context.LAYOUT_INFLATER_SERVICE);
         
       View row inflater.inflate(R.layout.listitemparentfalse);
                
String msg json_string_1_all[position];
                
TextView tv_1 = (TextViewrow.findViewById(R.id.textView1);
                
tv_1.setText(msg);
                
            return 
row;
        }
        
        
    }



لطفا راهنمایی کنید.با تشکر

برای دریافت صحیح متون فارسی باید خط کد زیر رو در متد doInBackground :
json_string_1_all[i] = new String(c.getString(json_node_name_1).getBytes("ISO-8859-1"),"UTF-8");

به این صورت تغییر بدید :
json_string_1_all[i] = c.getString(json_node_name_1);
پاسخ

مشکل در دریافت متن فارسی json

#3
برای دریافت صحیح متون فارسی باید خط کد زیر رو در متد onPost:
json_string_1_all[i] = new String(c.getString(json_node_name_1).getBytes("ISO-8859-1"),"UTF-8");

به این صورت تغییر بدید :
json_string_1_all[i] = c.getString(json_node_name_1);
پاسخ
 سپاس شده توسط شماره مجازی امارات ، تلگرام ضد فیلتر 2023


پرش به انجمن:


کاربران در حال بازدید این موضوع: 1 مهمان