Answer the question
In order to leave comments, you need to log in
How to apply encoding to JSON in Android?
Hey!
I just can’t solve the problem with applying the encoding to the data received from JSON in the Android application.
So how to correctly apply the encoding to the Cyrillic alphabet?
Here is my code:
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import es.dmoral.toasty.Toasty;
public class FactsFragment extends Fragment {
private String TAG = MainActivity.class.getSimpleName();
private ProgressDialog pDialog;
private ListView lv;
Activity activity;
// URL to get contacts JSON
private static String url = "http://sunshineapps.com.ua/JSON/main.json";
ArrayList<HashMap<String, String» contactList;
List<Product> products;
@Override
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.facts_fragment, container, false);
contactList = new ArrayList<);
lv = (ListView) v.findViewById(R.id.list);
new GetContacts().execute();
return v;
}
public static FactsFragment newInstance(String text) {
FactsFragment f = new FactsFragment();
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray contacts = jsonObj.getJSONArray("contacts");
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString("id");
String name = c.getString("name");
String email = c.getString("email");
// Phone node is JSON Object
JSONObject phone = c.getJSONObject("phone");
String mobile = phone.getString("mobile");
// tmp hash map for single contact
HashMap<String, String> contact = new HashMap<);
// adding each child node to HashMap key => value
contact.put("id", id);
contact.put("name", name);
contact.put("email", email);
contact.put("mobile", mobile);
// adding contact to contact list
contactList.add(contact);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
//Toast.makeText(getActivity().getApplicationContext(),
//"Json parsing error: " + e.getMessage(),
//Toast.LENGTH_LONG)
//.show();
Toasty.error(getActivity().getApplicationContext(), getString(R.string.wrong), Toast.LENGTH_SHORT, true).show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
//Toast.makeText(getActivity().getApplicationContext(),
//"Couldn't get json from server. Check LogCat for possible errors!",
//Toast.LENGTH_LONG)
//.show();
Toasty.info(getActivity().getApplicationContext(), getString(R.string.no_network), Toast.LENGTH_SHORT, true).show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
getActivity(), contactList,
R.layout.list_item, new String[]{"name", "email",
"mobile"}, new int[]{R.id.name,
R.id.email, R.id.mobile});
lv.setAdapter(adapter);
}
}
}
Answer the question
In order to leave comments, you need to log in
from the JSON from the server hardcoded into your code , you can see that "name": "Пример PSP°РїРёСЃР°РЅРёСЏ"
I would ask the backend developer to convert the text to a normal encoding, for example UTF-8
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question