gpt4 book ai didi

java - 我如何将 TwitterFeed (JSON) 解析为自定义 ListView ?

转载 作者:行者123 更新时间:2023-11-30 11:42:17 26 4
gpt4 key购买 nike

我从如何将 JSON 数据解析为自定义 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);
}
}
}

我已经用我的推特源替换了教程中的 JSON(工作正常):https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=3FMNu&count=20&exclude_replies=True

并将属性从 namebirthday 更改为 textcreated_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

为什么会发生这种情况,我该如何解决?

最佳答案

尝试:

HttpGet httpGet = new HttpGet(https://api.twitter.com/1/statuses/user_timeline.json);

带参数:

List<NameValuePair> params = new LinkedList<NameValuePair>();
params.add(new BasicNameValuePair("include_entities", true));
...

也试试 How to add parameters to a HTTP GET request in Android?

关于java - 我如何将 TwitterFeed (JSON) 解析为自定义 ListView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11808279/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com