使用json元素在列表视图中放置一些元素

时间:2016-03-09 22:04:08

标签: java android json database android-asynctask

我是Android的新手。我看过一个教程,将我的数据库中的一些元素放到我的列表视图中,但是我遇到了一些问题。当我打开下面的页面时,应用程序崩溃:ListBooks.java。 下面我把我的代码。我错了什么?

customAdapter.java:

public class customAdapter{

public List<HashMap<String, Object>> parse(JSONObject JObject) {

    JSONArray infoBooks = null;

    try {
        infoBooks = JObject.getJSONArray("infoBooks");
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return getBooks(infoBooks);
}

private List<HashMap<String, Object>> getBooks(JSONArray infoBooks){

    int booksCount = infoBooks.length();

    List<HashMap<String, Object>> bookList = new ArrayList<HashMap<String, Object>>();
    HashMap<String, Object> book;

    for(int i = 0; i < booksCount; i++) {

        try {
            book = getBook((JSONObject) infoBooks.get(i));
            bookList.add(book);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
        return bookList;

    }


private HashMap<String, Object> getBook(JSONObject jBook){

    HashMap<String, Object> book = new HashMap<String, Object>();
    String user_name = "";
    String title = "";
    String author = "";
    String urlImage = "";

    try {
        user_name = jBook.getString("user_name");
        title = jBook.getString("title");
        author = jBook.getString("author");
        urlImage = jBook.getString("urlImage");

        String details = "Title: " + title + "\n" +
                         "Author/s: " + author;

        book.put("user_name", user_name);
        book.put("details", details);
        book.put("launcherImage", R.mipmap.ic_launcher);
        book.put("urlImage", urlImage);



    } catch (JSONException e) {
        e.printStackTrace();
    }

    return  book;
}

}

SearchBooks.java:

 ListView mListView;

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

    String strUrl = "http://192.168.1.118:8888/webappdb/listViewBooks.php";
    DownloadTask downloadTask = new DownloadTask();
    downloadTask.execute(strUrl);
    mListView = (ListView) findViewById(R.id.listView);

}



private String downloadUrl (String strUrl) throws IOException{

    String data = "";
    InputStream iStream = null;

    try {

        URL url = new URL(strUrl);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.connect();
        iStream = urlConnection.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
        StringBuffer sb = new StringBuffer();

        String line = "";
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        data = sb.toString();
        br.close();
    }catch (Exception e){
        Log.d("Exception while downloading url", e.toString());
    }finally {
        iStream.close();
    }

    return data;
}


private class DownloadTask extends AsyncTask<String, Integer, String>{

    String data = null;

    @Override
    protected String doInBackground(String... url) {

    try {
        data = downloadUrl(url[0]);
    } catch (IOException e) {
        Log.d("Background Task", e.toString());
    }

        return data;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        ListViewLoaderTask listViewLoaderTask = new ListViewLoaderTask();
        listViewLoaderTask.execute(result);
    }
}



private class ListViewLoaderTask extends AsyncTask<String, Void, SimpleAdapter>{

    JSONObject jObject;

    @Override
    protected SimpleAdapter doInBackground(String... strJson) {

        try {
            jObject = new JSONObject(strJson[0]);
            customAdapter customAdapter = new customAdapter();
            customAdapter.parse(jObject);

        } catch (JSONException e) {
            Log.d("JSON Exception1", e.toString());
        }

        customAdapter customAdapter = new customAdapter();

        List<HashMap<String, Object>> books = null;

        try {
            books = customAdapter.parse(jObject);
        } catch (Exception e){
            Log.d("Exception", e.toString());
        }

        String infoFrom[] = {"user_name", "details"};
        int infoTo[] = {R.id.user_name_search, R.id.bookDescriptionSearch};
        SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), books, R.layout.row_list_books, infoFrom, infoTo);

        return adapter;
    }


    @Override
    protected void onPostExecute(SimpleAdapter adapter) {
        mListView.setAdapter(adapter);

        for (int i = 0; i < adapter.getCount(); i++){
            HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(i);
            String imgUrl = (String) hm.get("urlImage");
            ImageLoaderTask imageLoaderTask = new ImageLoaderTask();

            HashMap<String, Object> hmDownload = new HashMap<String, Object>();
            hm.put("urlImage", imgUrl);
            hm.put("position", i);

            imageLoaderTask.execute();

        }
    }
}



private class ImageLoaderTask extends AsyncTask<HashMap<String, Object>, Void, HashMap<String, Object>>{


    @Override
    protected HashMap<String, Object> doInBackground(HashMap<String, Object>... hm) {

        InputStream iStream = null;
        String imgUrl = (String) hm[0].get("urlImage");
        int position = (Integer) hm[0].get("position");

        URL url;
        try {
            url = new URL(imgUrl);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.connect();
            iStream = urlConnection.getInputStream();
            File cacheDirectory = getBaseContext().getCacheDir();
            File tmpFile = new File (cacheDirectory.getPath() + "/wpta_" + position + ".png");
            FileOutputStream fOutputStream = new FileOutputStream(tmpFile);
            Bitmap b = BitmapFactory.decodeStream(iStream);
            b.compress(Bitmap.CompressFormat.PNG, 100, fOutputStream);
            fOutputStream.flush();
            fOutputStream.close();

            HashMap<String, Object> hmBitmap = new HashMap<String, Object>();
            hmBitmap.put("launcherImage", tmpFile.getPath());
            hmBitmap.put("position", position);

            return hmBitmap;

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }


    @Override
    protected void onPostExecute(HashMap<String, Object> result) {
        String path = (String) result.get("launcherImage");
        int position = (Integer) result.get("position");
        SimpleAdapter simpleAdapter = (SimpleAdapter) mListView.getAdapter();
        HashMap<String, Object> hm = (HashMap<String, Object>) simpleAdapter.getItem(position);
        hm.put("launcherImage", path);
        simpleAdapter.notifyDataSetChanged();

    }
}

logcat的

03-09 23:12:36.876 9938-9938/? E/Zygote: v2
03-09 23:12:36.886 9938-9938/? E/SELinux: [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err: org.json.JSONException: No value for title
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at org.json.JSONObject.getString(JSONObject.java:550)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at gamingproject.sellmybooks.customAdapter.getBook(customAdapter.java:58)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at gamingproject.sellmybooks.customAdapter.getBooks(customAdapter.java:37)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at gamingproject.sellmybooks.customAdapter.parse(customAdapter.java:24)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at gamingproject.sellmybooks.SearchBooks$ListViewLoaderTask.doInBackground(SearchBooks.java:124)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at gamingproject.sellmybooks.SearchBooks$ListViewLoaderTask.doInBackground(SearchBooks.java:114)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at java.lang.Thread.run(Thread.java:818)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err: org.json.JSONException: No value for title
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at org.json.JSONObject.getString(JSONObject.java:550)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at gamingproject.sellmybooks.customAdapter.getBook(customAdapter.java:58)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at gamingproject.sellmybooks.customAdapter.getBooks(customAdapter.java:37)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at gamingproject.sellmybooks.customAdapter.parse(customAdapter.java:24)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at gamingproject.sellmybooks.SearchBooks$ListViewLoaderTask.doInBackground(SearchBooks.java:124)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at gamingproject.sellmybooks.SearchBooks$ListViewLoaderTask.doInBackground(SearchBooks.java:114)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at java.lang.Thread.run(Thread.java:818)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err: org.json.JSONException: No value for title
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at org.json.JSONObject.getString(JSONObject.java:550)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at gamingproject.sellmybooks.customAdapter.getBook(customAdapter.java:58)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at gamingproject.sellmybooks.customAdapter.getBooks(customAdapter.java:37)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at gamingproject.sellmybooks.customAdapter.parse(customAdapter.java:24)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at gamingproject.sellmybooks.SearchBooks$ListViewLoaderTask.doInBackground(SearchBooks.java:124)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at gamingproject.sellmybooks.SearchBooks$ListViewLoaderTask.doInBackground(SearchBooks.java:114)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at java.lang.Thread.run(Thread.java:818)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err: org.json.JSONException: No value for title
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at org.json.JSONObject.getString(JSONObject.java:550)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at gamingproject.sellmybooks.customAdapter.getBook(customAdapter.java:58)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at gamingproject.sellmybooks.customAdapter.getBooks(customAdapter.java:37)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at gamingproject.sellmybooks.customAdapter.parse(customAdapter.java:24)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at gamingproject.sellmybooks.SearchBooks$ListViewLoaderTask.doInBackground(SearchBooks.java:135)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at gamingproject.sellmybooks.SearchBooks$ListViewLoaderTask.doInBackground(SearchBooks.java:114)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at java.lang.Thread.run(Thread.java:818)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err: org.json.JSONException: No value for title
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at org.json.JSONObject.getString(JSONObject.java:550)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at gamingproject.sellmybooks.customAdapter.getBook(customAdapter.java:58)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at gamingproject.sellmybooks.customAdapter.getBooks(customAdapter.java:37)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at gamingproject.sellmybooks.customAdapter.parse(customAdapter.java:24)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at gamingproject.sellmybooks.SearchBooks$ListViewLoaderTask.doInBackground(SearchBooks.java:135)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at gamingproject.sellmybooks.SearchBooks$ListViewLoaderTask.doInBackground(SearchBooks.java:114)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at java.lang.Thread.run(Thread.java:818)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err: org.json.JSONException: No value for title
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at org.json.JSONObject.getString(JSONObject.java:550)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at gamingproject.sellmybooks.customAdapter.getBook(customAdapter.java:58)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at gamingproject.sellmybooks.customAdapter.getBooks(customAdapter.java:37)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at gamingproject.sellmybooks.customAdapter.parse(customAdapter.java:24)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at gamingproject.sellmybooks.SearchBooks$ListViewLoaderTask.doInBackground(SearchBooks.java:135)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at gamingproject.sellmybooks.SearchBooks$ListViewLoaderTask.doInBackground(SearchBooks.java:114)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-09 23:12:39.476 9938-10019/gamingproject.sellmybooks W/System.err:     at java.lang.Thread.run(Thread.java:818)

提前致谢。

0 个答案:

没有答案