۱۳۹۵/۰۹/۲۵, ۰۹:۴۸ ب.ظ
(۱۳۹۵/۰۳/۱۳, ۱۲:۵۴ ب.ظ)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<String, String, JSONObject> {
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 = 0; i < chat.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 context, int resource, int textViewResourceId,
String[] strings) {
super(context, resource, textViewResourceId, strings);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.listitem, parent, false);
String msg = json_string_1_all[position];
TextView tv_1 = (TextView) row.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);