gpt4 book ai didi

java - 通过 JSON 提要代码下载推文无效

转载 作者:行者123 更新时间:2023-11-29 22:16:53 25 4
gpt4 key购买 nike

我正在构建我的第一个 Android 应用程序 - 可以在列表中显示我的推文。

我设法让该应用程序更早地运行,但推文有时需要很长时间才能下载,并且该应用程序会变得无响应或崩溃。

我决定向应用程序添加一个线程,这样它就不会变得无响应,但现在它根本不起作用了:/

有人知道怎么回事吗?学习java才第三天,我似乎无法弄清楚这里的问题是什么。

代码如下:

package com.app.first;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;

public class Twitter extends ListActivity {

public ProgressDialog pd = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

// Show the ProgressDialog on this thread
pd = ProgressDialog.show(this, "Working..", "Downloading Data...",
true, false);

new LoadTwitterFeed().execute();

}

public class LoadTwitterFeed extends AsyncTask<String, Integer, String> {

String tweets[] = new String[9];

public String readTwitterFeed() {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(
"https://api.twitter.com/1/statuses/user_timeline.json?screen_name=jjmpsp&include_rts=false&count=10");
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
Log.e(ParseJSON.class.toString(), "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return builder.toString();
}

@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub

String readTwitterFeed = readTwitterFeed();

try {
JSONArray jsonArray = new JSONArray(readTwitterFeed);

for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
tweets[i] = jsonObject.getString("text").toString();
}
} catch (Exception e) {
e.printStackTrace();
}

setListAdapter(new ArrayAdapter<String>(Twitter.this,
android.R.layout.simple_list_item_1, tweets));

return null;
}

@Override
protected void onPostExecute(String result) {
// what to do when the task ends.
pd.hide();

}

}

}

最佳答案

在onPostExecute中设置这段代码

setListAdapter(new ArrayAdapter<String>(Twitter.this,
android.R.layout.simple_list_item_1, tweets));

关于java - 通过 JSON 提要代码下载推文无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8247258/

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