我想构建一个基于Android的twitter feed阅读器应用程序。我在解析json响应时遇到问题。这是我的代码:
`public class HttpClientActivity扩展了Activity {
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);
}
}
答案 0 :(得分:2)
要获得JSONArray,首先需要JSONObject:
示例包含数组的JSON对象:
String returned = "{ "myArray" : [item1, item2] }"
JSONObject jsonObj = new JSONObject(returned);
JSONArray jarray = jsonObj.getJSONArray("myArray");
JSONObject jsonObj = new JSONObject(returned);
JSONArray jarray = jsonObj.getJSONArray("results");
JSONObject firstResult = jarray.getJSONObject(0); // loop if you want more
String username = firstResult.getString("from_user");
String message = firstResults.getString("text");
如果你通过这样的格式化程序:http://jsonformatter.curiousconcept.com/它的100000x更容易阅读