I am developing an android app with both English and Arabic language support. In this, I am parsing Json data from server which contain both English and Arabic. I have to display English or Arabic text which is parsed from Json, according to user language selection. I can display the English string successfully. But while trying to display the arabic string it showing some weird characters.
Please note that I don't have much knowledge in Character Encoding. So please don’t blame me if it is a wrong question.
getjsondata function
private class GetJsondata extends AsyncTask<Void, Void, ArrayList<ArrayList<String>>> {
@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(getActivity(), "", getActivity().getResources().getString(R.string.pleasewait), true);
}
@Override
protected ArrayList<ArrayList<String>> doInBackground(Void... arg0) {
// Creating service handler class instance
ConnectivityManager conMgr = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
if (activeNetwork != null && activeNetwork.isConnected()) {
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
jsonObj = new JSONObject(jsonStr);
ObjectOutput out = new ObjectOutputStream(new FileOutputStream
(new File(getActivity().getCacheDir(), "") + File.separator + "cacheFile.srl"));
out.writeObject(jsonObj.toString());
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
}
else{
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream
(new File(getActivity().getCacheDir() + File.separator + "cacheFile.srl")));
jsonObj = new JSONObject((String) in.readObject());
in.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (OptionalDataException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (StreamCorruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
if(jsonObj!=null) {
try {
ofrList = new ArrayList<ArrayList<String>>();
// Getting JSON Array node
contacts = jsonObj.getJSONArray("offers");
shoplistarray = new ArrayList<ArrayList<String>>();
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String url = c.getString("url");
if (userPrefs.getString("locale",null)==null || userPrefs.getString("locale",null).equals("en")) {
desc = c.getString("desc");
}
else{
String s= c.getString("desc_ar");
try {
byte[] b= s.getBytes("UTF-8");
desc = new String(b, "UTF-16");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
String expdate = c.getString("expdate");
String ofrdate = c.getString("date");
ArrayList<String> ofrData = new ArrayList<String>();
ofrData.add(url);
ofrData.add(desc);
ofrData.add(expdate);
ofrData.add(ofrdate);
ofrList.add(ofrData);
shoplist = new ArrayList<String>();
shopsarray = c.getJSONArray("shops");
for (int n = 0; n < shopsarray.length(); n++) {
JSONObject jobj = shopsarray.getJSONObject(n);
String shopid = jobj.getString("shopid");
shoplist.add(shopid);
}
shoplistarray.add(shoplist);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return ofrList;
}
servicehandler.java
package com.alsafeer.xpressions;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
public class ServiceHandler {
static String response = null;
public final static int GET = 1;
public final static int POST = 2;
public ServiceHandler() {
}
/*
* Making service call
* @url - url to make request
* @method - http request method
* */
public String makeServiceCall(String url, int method) {
return this.makeServiceCall(url, method, null);
}
/*
* Making service call
* @url - url to make request
* @method - http request method
* @params - http request params
* */
public String makeServiceCall(String url, int method,
List<NameValuePair> params) {
try {
// http client
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
// Checking http request method type
if (method == POST) {
HttpPost httpPost = new HttpPost(url);
// adding post params
if (params != null) {
httpPost.setEntity(new UrlEncodedFormEntity(params));
}
httpResponse = httpClient.execute(httpPost);
} else if (method == GET) {
// appending params to url
if (params != null) {
String paramString = URLEncodedUtils
.format(params, "UTF-8");
url += "?" + paramString;
}
HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);
}
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity, HTTP.UTF_8);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
}
Aucun commentaire:
Enregistrer un commentaire