Java 在异步任务之外填充列表

Java 在异步任务之外填充列表,java,android,Java,Android,我试图通过异步任务用json数据(来自web)填充listview。我在脚本前面创建了一个名为myCars的列表,但在异步线程中填充它时遇到了问题 package com.*****.complexlistview; import android.app.Activity; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import

我试图通过异步任务用json数据(来自web)填充listview。我在脚本前面创建了一个名为myCars的列表,但在异步线程中填充它时遇到了问题

package com.*****.complexlistview;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends Activity {
    private List<Car> myCars = new ArrayList<Car>();
    protected String[] mBlogPostTitles;
    public static final int NUMBER_OF_POSTS = 20; //caps indicate constants
    public static final String TAG = MainActivity.class.getSimpleName();//prints name of class without package name

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(isNetworkAvailable()) {
            GetBlogPostsTask getBlogPostsTask = new GetBlogPostsTask(); // new thread
            getBlogPostsTask.execute();// don't call do in background directly
            populateListView();
        }else{
            Toast.makeText(this, "Network is unavailable", Toast.LENGTH_LONG).show();
        }
    }
    public boolean isNetworkAvailable() {
        ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();

        boolean isAvailable = false;

        if(networkInfo != null && networkInfo.isConnected()){
            isAvailable = true;
        }

        return isAvailable;
    }
    private class GetBlogPostsTask extends AsyncTask<Object, Void, String> {

        @Override
        protected String doInBackground(Object[] params) {

            int responseCode = -1;//need to have this variable outside scope of try/catch block

            try {
                URL blogFeedUrl = new URL("http://blog.teamtreehouse.com/api/get_recent_summary/?count=" + NUMBER_OF_POSTS);
                HttpURLConnection connection = (HttpURLConnection) blogFeedUrl.openConnection();
                connection.connect();

                responseCode = connection.getResponseCode();
                if(responseCode == HttpURLConnection.HTTP_OK){ //could have used just 200 value
                    InputStream inputStream = connection.getInputStream();
                    Reader reader = new InputStreamReader(inputStream);
                    int contentLength = connection.getContentLength();
                    char[] charArray = new char[contentLength];
                    reader.read(charArray);
                    String responseData = new String(charArray);

                    JSONObject jsonResponse = new JSONObject(responseData);
                    String status = jsonResponse.getString("status");
                    Log.v(TAG, status);

                    JSONArray jsonPosts = jsonResponse.getJSONArray("posts");
                    for(int i=0; i < jsonPosts.length(); i++ ){
                        JSONObject jsonPost = jsonPosts.getJSONObject(i);
                        String title = jsonPost.getString("title");
                        Log.v(TAG, "Post " + i + ": " + title);
                        myCars.add(new Car(title, 1994, R.drawable.kanye8080s, "Lovable"));

                        /*myCars.add(new Car("Ford", 1940, R.drawable.stadiumarcadium, "Needing work"));
                        myCars.add(new Car("Toyota", 1994, R.drawable.kanye8080s, "Lovable"));
                        myCars.add(new Car("Honda", 1999, R.drawable.meteora, "Great condition"));
                        myCars.add(new Car("Porsche", 2005, R.drawable.olp, "Awesome"));
                        myCars.add(new Car("Jeep", 2010, R.drawable.yeezus, "Out of this world"));
                        myCars.add(new Car("Honda", 1999, R.drawable.meteora, "Great condition"));
                        myCars.add(new Car("Porsche", 2005, R.drawable.olp, "Awesome"));
                        myCars.add(new Car("Jeep", 2010, R.drawable.yeezus, "Out of this world"));*/
                    }
                }else{
                    Log.i(TAG, "Unsuccessful HTTP Response Code: " + responseCode);
                }
            }
            catch (MalformedURLException e){
                Log.e(TAG, "Exception caught");
            }
            catch (IOException e){
                Log.e(TAG, "Exception caught");
            }
            catch (Exception e){//must be in this order, this is the last, general catch
                Log.e(TAG, "Exception caught");
            }

            return "Code: " + responseCode;
        }
    }
    private void populateListView() {
        ArrayAdapter<Car> adapter = new MyListAdapter();
        ListView list = (ListView) findViewById(R.id.carsListView);
        list.setAdapter(adapter);
    }

    private class MyListAdapter extends ArrayAdapter<Car>{
        public MyListAdapter() {
            super(MainActivity.this, R.layout.item_view, myCars);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // make sure we have a view to work with
            View itemView = convertView;
            if (itemView == null) {
                itemView = getLayoutInflater().inflate(R.layout.item_view, parent,false);
            }

            //find the car to work with
            Car currentCar = myCars.get(position);

           //fill the view
            ImageView imageView = (ImageView) itemView.findViewById(R.id.item_icon);
            imageView.setImageResource(currentCar.getIconID());

            //Make:
            TextView makeText = (TextView) itemView.findViewById(R.id.item_txtMake);
            makeText.setText(currentCar.getMake());

            //Year
            TextView yearText = (TextView) itemView.findViewById(R.id.item_txtYear);
            yearText.setText("" + currentCar.getYear());

            //Condition
            TextView conditionText = (TextView) itemView.findViewById(R.id.item_txtCondition);
            conditionText.setText(currentCar.getCondition());


            return itemView;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
} 
package com.*****.complexlistview;
导入android.app.Activity;
导入android.content.Context;
导入android.net.ConnectivityManager;
导入android.net.NetworkInfo;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.ImageView;
导入android.widget.ListView;
导入android.widget.TextView;
导入android.widget.Toast;
导入org.json.JSONArray;
导入org.json.JSONObject;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.io.Reader;
导入java.net.HttpURLConnection;
导入java.net.MalformedURLException;
导入java.net.URL;
导入java.util.ArrayList;
导入java.util.List;
公共类MainActivity扩展了活动{
private List myCars=new ArrayList();
受保护字符串[]mBlogPostTitles;
public static final int NUMBER_OF_POSTS=20;//大写表示常量
public static final String TAG=MainActivity.class.getSimpleName();//打印不带包名的类名
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(isNetworkAvailable()){
GetBlogPostsTask GetBlogPostsTask=new GetBlogPostsTask();//新线程
getBlogPostsTask.execute();//不要直接在后台调用do
populateListView();
}否则{
Toast.makeText(此“网络不可用”,Toast.LENGTH_LONG.show();
}
}
公共布尔值isNetworkAvailable(){
ConnectivityManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_服务);
NetworkInfo NetworkInfo=manager.getActiveNetworkInfo();
布尔值isAvailable=false;
if(networkInfo!=null&&networkInfo.isConnected()){
isAvailable=真;
}
可获得的回报;
}
私有类GetBlogPostsTask扩展异步任务{
@凌驾
受保护的字符串doInBackground(对象[]参数){
int responseCode=-1;//需要将此变量置于try/catch块的作用域之外
试一试{
URL blogFeedUrl=新URL(“http://blog.teamtreehouse.com/api/get_recent_summary/?count=“+职位数目);
HttpURLConnection连接=(HttpURLConnection)blogFeedUrl.openConnection();
connection.connect();
responseCode=connection.getResponseCode();
如果(responseCode==HttpURLConnection.HTTP_OK){//可能只使用了200个值
InputStream InputStream=connection.getInputStream();
Reader Reader=新的InputStreamReader(inputStream);
int contentLength=connection.getContentLength();
char[]charArray=新字符[contentLength];
读取器.read(charArray);
字符串响应数据=新字符串(charArray);
JSONObject jsonResponse=新的JSONObject(responseData);
String status=jsonResponse.getString(“status”);
Log.v(标签、状态);
JSONArray jsonPosts=jsonResponse.getJSONArray(“posts”);
for(int i=0;i@Override
        protected void onPostExecute(Void result) {
             // call populateListView method here
            populateListView();
            super.onPostExecute(result);
        }