我从教程 How to parse JSON data into a Custom Listview 中得到了一个代码
这是教程中的代码,有一些修改:
public class TwitterParseHandler extends Activity {
ArrayList<TwitterFeed> arrayOfWebData = new ArrayList<TwitterFeed>();
class TwitterFeed {
public String text;
public String created_at;
}
FancyAdapter aa=null;
static ArrayList<String> resultRow;
public void onCreate(Bundle savedInstanceState) {
try
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_twitter_parse_handler);
String result = "";
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=3FMNu&count=20&exclude_replies=True");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream webs = entity.getContent();
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(webs, "iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "/n");
}
webs.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error in converting result "+e.toString());
}
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
try
{
JSONArray jArray = new JSONArray(result);
for (int i=0;i<jArray.length();i++)
{
JSONObject json_data = jArray.getJSONObject(i);
TwitterFeed resultRow = new TwitterFeed();
resultRow.text = json_data.getString("text");
resultRow.created_at = json_data.getString("created_at");
arrayOfWebData.add(resultRow);
}
}catch(JSONException e){
Log.e("log_tag", "Error in parsing data "+e.toString());
}
ListView myListView = (ListView)findViewById(R.id.list);
aa=new FancyAdapter();
myListView.setAdapter(aa);
}
catch(Exception e){
Log.e("ERROR", "ERROR IN CODE: "+e.toString());
e.printStackTrace();
}
}
class FancyAdapter extends ArrayAdapter<TwitterFeed> {
FancyAdapter() {
super(TwitterParseHandler.this, android.R.layout.simple_list_item_1, arrayOfWebData);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView==null) {
LayoutInflater inflater=getLayoutInflater();
convertView=inflater.inflate(R.layout.twitterlist, null);
holder=new ViewHolder(convertView);
convertView.setTag(holder);
}
else
{
holder=(ViewHolder)convertView.getTag();
}
holder.populateFrom(arrayOfWebData.get(position));
return(convertView);
}
}
class ViewHolder {
public TextView name=null;
public TextView birthday=null;
ViewHolder(View row) {
name=(TextView)row.findViewById(R.id.TwitterText);
birthday=(TextView)row.findViewById(R.id.TwitterDatum);
}
void populateFrom(TwitterFeed r) {
name.setText(r.text);
birthday.setText(r.created_at);
}
}
}
我已经用我的 twitterfeed 替换了教程中的 JSON(工作正常):https ://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=3FMNu&count=20&exclude_replies=True
并将属性从name
and更改birthday
为text
and created_at
,现在当我调试它时,我收到以下错误:
08-04 12:36:40.539: E/log_tag(4599): Error in parsing data org.json.JSON
Exception: Value
{ "error": "This method requires a GET.",
"request":"\/1\/statuses\/user_timeline.json?include_entities=true&include_rts=true&screen_name=3FMNu&count=20&exclude_replies=True"
} of type org.json.JSONObject cannot be converted to JSONArray
为什么会发生这种情况,我该如何解决?