Java Android Twitter字符串到Json数组
我有一个twitter提要的URL,我已经从中创建了一个字符串,我想把它放在一个JSONArray中,这样我就可以调用各个条目。在本例中,“文本” 这是我的代码:Java Android Twitter字符串到Json数组,java,android,json,Java,Android,Json,我有一个twitter提要的URL,我已经从中创建了一个字符串,我想把它放在一个JSONArray中,这样我就可以调用各个条目。在本例中,“文本” 这是我的代码: try { // Create a new HTTP Client DefaultHttpClient defaultClient = new DefaultHttpClient(); // Setup the get request HttpGet httpGetRequest = new HttpGet(
try {
// Create a new HTTP Client
DefaultHttpClient defaultClient = new DefaultHttpClient();
// Setup the get request
HttpGet httpGetRequest = new HttpGet(
"https://api.twitter.com/1/statuses/user_timeline.json?screen_name=evostikleague&count=10");
// Execute the request in the client
HttpResponse httpResponse = defaultClient.execute(httpGetRequest);
// Grab the response
BufferedReader reader = new BufferedReader(new InputStreamReader(
httpResponse.getEntity().getContent(), "UTF-8"));
String json = reader.readLine();
// Instantiate a JSON object from the request response
JSONObject obj = new JSONObject(json);
List<String> items = new ArrayList<String>();
JSONArray jArray = obj.getJSONArray(json);
for (int i = 0; i < jArray.length(); i++) {
JSONObject oneObject = jArray.getJSONObject(i);
items.add(oneObject.getString("text"));
Log.i("items", "items");
}
setListAdapter(new ArrayAdapter<String>(this, R.layout.single_item,
items));
ListView list = getListView();
list.setTextFilterEnabled(true);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),
((TextView) arg1).getText(), 1000).show();
}
});
} catch (Exception e) {
// In your production code handle any errors and catch the individual exceptions
e.printStackTrace();
}
试试看{
//创建一个新的HTTP客户端
DefaultHttpClient defaultClient=新的DefaultHttpClient();
//设置get请求
HttpGet httpGetRequest=新HttpGet(
"https://api.twitter.com/1/statuses/user_timeline.json?screen_name=evostikleague&count=10");
//在客户机中执行请求
HttpResponse HttpResponse=defaultClient.execute(httpGetRequest);
//抓住回应
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
httpResponse.getEntity().getContent(),“UTF-8”);
字符串json=reader.readLine();
//从请求-响应实例化JSON对象
JSONObject obj=新的JSONObject(json);
列表项=新建ArrayList();
JSONArray jArray=obj.getJSONArray(json);
for(int i=0;i
您试图将响应解析为JSONObject
({}
),而它是JSONArray
([]
)。您可能应该删除该行:
// Instantiate a JSON object from the request response
JSONObject obj = new JSONObject(json);
相反,您需要:
JSONArray jArray = new JSONArray(json);
您试图将响应解析为
JSONObject
({}
),而它是JSONArray
([]
)。您可能应该删除该行:
// Instantiate a JSON object from the request response
JSONObject obj = new JSONObject(json);
相反,您需要:
JSONArray jArray = new JSONArray(json);
我们需要首先创建JSON对象。比如说,
JSONObject jsonObject = new JSONObject(resp);
jsonObject可能包含其他JSON对象或JSON数组。如何转换JSON取决于字符串
JSONArray arr = jsonObject.getJSONArray("arraykey");
关于JSON字符串到JSON数组的解释,有一个完整的例子,可以在找到,我们需要先创建JSON对象。比如说,
JSONObject jsonObject = new JSONObject(resp);
jsonObject可能包含其他JSON对象或JSON数组。如何转换JSON取决于字符串
JSONArray arr = jsonObject.getJSONArray("arraykey");
关于JSON字符串到JSON数组的解释,有一个完整的例子,可以在中找到,它没有在我的列表视图中显示任何内容,我在我的LogCat中得到一个JSONeexception。它没有在我的列表视图中显示任何内容,我在我的LogCat中得到一个JSONeexceptionjson.getString json=reader.readLine();Log.v(json,“jsonfeed”);列表项=新建ArrayList();对于(inti=0;i