我想構建一個基於android的twitter提要閱讀器應用程序。我在解析json響應時遇到了問題。這裏是我的代碼:解析json時出錯
'公共類HttpClientActivity延伸活動{
class Tweet{
public String username;
public String message;
}
ArrayList<Tweet> tweets = new ArrayList<Tweet>();
static ArrayList<String> resultRow;
@Override
public void onCreate(Bundle savedInstanceState) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
public class Getdata {
public String getInternetData() throws Exception{
String data = null;
try {
URI website = new URI("http://search.twitter.com/search.json?q=blue%20angels&rpp=5&include_entities=true&result_type=mixed&include_rts=5");
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
data = EntityUtils.toString(entity);
}catch(Exception e){
Log.e("log_tag", "Error in http connection: " +e.toString());
}
return data;
}
}
Getdata getdata = new Getdata();
String returned = null;
try {
returned = getdata.getInternetData();
} catch (Exception e) {
e.printStackTrace();
}
try{
JSONArray jarray = new JSONArray(returned);
for(int i=0; i<jarray.length(); i++){
JSONObject jo = jarray.getJSONObject(i);
Tweet tt = new Tweet();
tt.username = jo.getString("from_user");
tt.message = jo.getString("text");
tweets.add(tt);
}
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data: "+e.toString());
}
ListView lv = (ListView) findViewById(R.id.listView1);
class FancyAdapter extends ArrayAdapter<Tweet> {
public FancyAdapter() {
super(HttpClientActivity.this, android.R.layout.simple_list_item_1, tweets);
}
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder;
if(convertView == null){
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(R.layout.listitem, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
holder.populatefrom(tweets.get(position));
return(convertView);
}
class ViewHolder {
public TextView username = null;
public TextView message = null;
ViewHolder(View listitem){
username = (TextView)listitem.findViewById(R.id.username);
message = (TextView)listitem.findViewById(R.id.message);
}
void populatefrom(Tweet t){
username.setText(t.username);
message.setText(t.message);
}
}
}
FancyAdapter ar = new FancyAdapter();
lv.setAdapter(ar);
}
}
現在,我得到的消息:
org.json.JSONException:Java類型值.lang.String不能被 轉換爲JSONArray
我不知道熱修復它:
try{
JSONArray jarray = new JSONArray(returned);
for(int i=0; i<jarray.length(); i++){
JSONObject jo = jarray.getJSONObject(i);
Tweet tt = new Tweet();
tt.username = jo.getString("from_user");
tt.message = jo.getString("text");
tweets.add(tt);
}
}
請告訴我們什麼'結果如下。 –
@AmokraneChentir:我相信網址是公開的:http://search.twitter.com/search.json?q=blue%20angels&rpp=5&include_entities=true&result_type=mixed&include_rts=5 – Amadan