チュートリアルからコードを取得しましたJSONデータをカスタムリストビューに解析する方法
チュートリアルのコードにいくつかの変更を加えたものを次に示します。
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(正常に機能していた)をTwitterフィードに置き換えました: https://api.twitter.com/1/statuses/user_timeline.json?include_entities = true&include_rts = true&screen_name = 3FMNu&count = 20&exclude_replies = True
属性をからとに変更しました。name
デバッグbirthday
するtext
と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
なぜこれが発生し、どうすれば修正できますか?