这是我的代码:
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
ListAdapter adapter = new SimpleAdapter(MainActivity.this, contactList,
R.layout.list_item, new String[]{ "rendered", "renderedcont"},
new int[]{R.id.title, R.id.content});
//new int[]{R.id.title});
lv.setAdapter(adapter);
}
如何显示支持HTML的文本? 这是完整的代码:
package com.codespeedy.jsonreader;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
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 java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
private String TAG = MainActivity.class.getSimpleName();
private ListView lv;
ArrayList<HashMap<String, String>> contactList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactList = new ArrayList<>();
lv = (ListView) findViewById(R.id.list);
new GetContacts().execute();
}
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(MainActivity.this, "JSON Is Downloading...", Toast.LENGTH_LONG).show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String url = "https://www.eyeswift.com/wp-json/wp/v2/posts/";
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 jsnarray = new JSONArray(jsonStr);
// looping through All Contacts
for (int i = 0; i < jsnarray.length(); i++) {
JSONObject c = jsnarray.getJSONObject(i);
String id = c.getString("id");
String date = c.getString("date");
String type = c.getString("type");
String link = c.getString("link");
String author = c.getString("author");
// String gender = c.getString("title");
// Phone node is JSON Object
JSONObject title = c.getJSONObject("title");
String rendered = title.getString("rendered");
JSONObject content = c.getJSONObject("content");
String renderedcont = content.getString("rendered");
// 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("date", date);
contact.put("author", author);
contact.put("link", link);
contact.put("rendered", rendered);
contact.put("renderedcont", renderedcont);
// adding contact to contact list
contactList.add(contact);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG).show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
ListAdapter adapter = new SimpleAdapter(MainActivity.this, contactList,
R.layout.list_item, new String[]{ "rendered", "renderedcont"},
new int[]{R.id.title, R.id.content});
//new int[]{R.id.title});
lv.setAdapter(adapter);
}
}
}