Reputation: 61
i need to show text in a UTF-8 Character Encoding in the android App, Here is my Code for JSONAsyncTask in:
class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(getActivity());
dialog.setMessage("Wait...");
dialog.setTitle("Loading");
dialog.show();
dialog.setCancelable(false);
}
@Override
protected Boolean doInBackground(String... urls) {
try {
HttpGet httppost = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsono = new JSONObject(data);
JSONArray jarray = jsono.getJSONArray("news");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
News news = new News();
news.setTitle(object.getString("title"));
news.setDescription(object.getString("description"));
news.setDate(object.getString("date"));
news.setImage(object.getString("image"));
newsList.add(news);
}
return true;
}
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
Of course, Before i'm Asking, research and see these result:
and many more... But that answers can't solve my problem.
Upvotes: 2
Views: 31696
Reputation: 61
Updated in 2021
JAVA
Finally i'm solved my problem.
Final code:
@Override
protected Boolean doInBackground(String... urls) {
try {
HttpGet httppost = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
int status = response.getStatusLine().getStatusCode();
change status to:
if (status == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
edit this code to:
String data = EntityUtils.toString(response.getEntity(), cz.msebera.android.httpclient.protocol.HTTP.UTF_8);
JSONObject jsono = new JSONObject(data);
JSONArray jarray = jsono.getJSONArray("news");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
News news = new News();
news.setTitle(object.getString("title"));
news.setDescription(object.getString("description"));
news.setDate(object.getString("date"));
news.setImage(object.getString("image"));
newsList.add(news);
}
return true;
}
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
Kotlin
fun doInBackground(vararg urls: String?): Boolean? {
try {
val httppost = HttpGet(urls[0])
val httpclient: HttpClient = DefaultHttpClient()
val response: HttpResponse = httpclient.execute(httppost)
val status: Int = response.getStatusLine().getStatusCode()
if (status == HttpStatus.SC_OK) {
val entity: HttpEntity = response.getEntity()
}
Upvotes: 1
Reputation: 1
String URL = your_URL;
URL obj = new URL(URL);
HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
connection.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new
InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
Upvotes: 0
Reputation: 538
You can simply encode, decode the Jsonobject using URLEncoder, URLDecoder. The example is given below
1.Encode the Jsonobject
try
{
String encoded = URLEncoder.encode(jsonobject, "UTF-8");
Log.e("UTF 8",encoded );
}
catch (UnsupportedEncodingException e)
{
Log.e("utf8", "conversion", e);
}
2.Decode the Jsonobject
try
{
String decoded = URLDecoder.decode(jsonobject, "UTF-8");
Log.e("UTF 8",decoded );
}
catch (UnsupportedEncodingException e)
{
Log.e("utf8", "conversion", e);
}
Upvotes: 0