Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过JSON源代码下载推文无效_Java_Android_Json_Twitter_Listactivity - Fatal编程技术网

Java 通过JSON源代码下载推文无效

Java 通过JSON源代码下载推文无效,java,android,json,twitter,listactivity,Java,Android,Json,Twitter,Listactivity,我正在构建我的第一个android应用程序——它将在列表中显示我的推文 我设法让应用程序运行得更早,但推文有时需要很长时间才能下载,应用程序会变得无响应或崩溃 我决定在应用程序中添加一个线程,这样它就不会变得无响应,但现在它根本不工作了:/ 有人知道怎么了吗?这只是我学习java的第三天,我似乎不知道这里有什么问题 代码如下: package com.app.first; import java.io.BufferedReader; import java.io.IOException; im

我正在构建我的第一个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();

    }

}

}
package com.app.first;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.StatusLine;
导入org.apache.http.client.ClientProtocolException;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpGet;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.json.JSONArray;
导入org.json.JSONObject;
导入android.app.ListActivity;
导入android.app.ProgressDialog;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.widget.ArrayAdapter;
公共类Twitter扩展了ListActivity{
public ProgressDialog pd=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
//显示此线程上的ProgressDialog
pd=ProgressDialog.show(这是“正在工作…”,“正在下载数据…”),
正确的,错误的);
新建LoadTwitterFeed().execute();
}
公共类LoadTwitterFeed扩展异步任务{
字符串tweets[]=新字符串[9];
公共字符串readTwitterFeed(){
StringBuilder=新的StringBuilder();
HttpClient=new DefaultHttpClient();
HttpGet HttpGet=新HttpGet(
"https://api.twitter.com/1/statuses/user_timeline.json?screen_name=jjmpsp&include_rts=false&count=10");
试一试{
HttpResponse response=client.execute(httpGet);
StatusLine StatusLine=response.getStatusLine();
int statusCode=statusLine.getStatusCode();
如果(状态代码==200){
HttpEntity=response.getEntity();
InputStream内容=entity.getContent();
BufferedReader reader=新的BufferedReader(
新的InputStreamReader(内容));
弦线;
而((line=reader.readLine())!=null){
builder.append(行);
}
}否则{
Log.e(ParseJSON.class.toString(),“未能下载文件”);
}
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回builder.toString();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
//TODO自动生成的方法存根
字符串readTwitterFeed=readTwitterFeed();
试一试{
JSONArray JSONArray=新的JSONArray(readTwitterFeed);
for(int i=0;i
在onPostExecute中设置此代码

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

它可以工作!非常感谢你,伙计!在过去的两个小时里,我一直在搞乱这个代码,真不敢相信我从来没有看到过这个错误!再次感谢:D