Json String to Image

时间:2015-01-30 20:56:14

标签: android json parsing listview

所以我坚持这个...我需要在listview中显示图像,从json文件中获取数据。

我已经设置了连接,解析了json文件并显示了我需要的内容。但不知何故,我无法找到有关如何将字符串(具有URL)转换为列表视图中的图像的大量信息。

具有url的字符串称为" ImageLink"

以下是我的MainActivity。

public class MainActivity extends ListActivity {

private ProgressDialog pDialog;

// URL to get game info JSON
private static String url = "https://dl.dropboxusercontent.com/u/38379784/Upcoming%20Games/DataForUPG.js";

// JSON Node names
private static final String TAG_Games = "games";
private static final String TAG_Title = "Title";
private static final String TAG_Description = "Description";
private static final String TAG_Release = "Release";
private static final String TAG_ImageLink = "ImageLink";



// Gameinfo JSONArray
JSONArray games = null;

// Hashmap for ListView
ArrayList<HashMap<String, String>> GamesList;

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

    GamesList = new ArrayList<HashMap<String, String>>();

    ListView lv = getListView();

    // Listview on item click listener
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            // getting values from selected ListItem
            String Title = ((TextView) view.findViewById(R.id.Title))
                    .getText().toString();
            String Description = ((TextView) view.findViewById(R.id.Description))
                    .getText().toString();
            String Release = ((TextView) view.findViewById(R.id.Release))
                    .getText().toString();
            String ImageLink = ((TextView) view.findViewById(R.id.ImageLink_label))
                    .getText().toString();

            // Starting single contact activity
            Intent in = new Intent(getApplicationContext(),
                    SingleListItem.class);
            in.putExtra(TAG_Title, Title);
            in.putExtra(TAG_Description, Description);
            in.putExtra(TAG_Release, Release);
            in.putExtra(TAG_ImageLink, ImageLink);
            startActivity(in);

        }
    });

// Calling async task to get json
    new GetGames().execute();
}

/**
 * Async task class to get json by making HTTP call
 * */
private class GetGames extends AsyncTask<Void, Void, Void> {




    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Loading Data...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                games = jsonObj.getJSONArray(TAG_Games);

                // looping through All games
                for (int i = 0; i < games.length(); i++) {
                    JSONObject c = games.getJSONObject(i);

                    String Title = c.getString(TAG_Title);
                    String Description = c.getString(TAG_Description);
                    String Release = c.getString(TAG_Release);
                    String ImageLink = c.getString(TAG_ImageLink);




                    // tmp hashmap for single game
                    HashMap<String, String> games = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    games.put(TAG_Title, Title);
                    games.put(TAG_Description, Description);
                    games.put(TAG_Release, Release);
                    games.put(TAG_ImageLink, ImageLink);

                    // adding contact to gameinfo list
                    GamesList.add(games);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();






        /**
         * Updating parsed JSON data into ListView
         * */

         ListAdapter adapter = new SimpleAdapter(
                MainActivity.this, GamesList,
                R.layout.list_item, new String[] { TAG_Title, TAG_Release,
                TAG_Description, TAG_ImageLink }, new int[] { R.id.Title,
                R.id.Release, R.id.Description, R.id.ImageLink_label });

        setListAdapter(adapter);
    }

}

}

我将不胜感激任何帮助

1 个答案:

答案 0 :(得分:0)

好吧,您可以创建另一个异步任务来处理下载图像:

 private class DownloadImg extends AsyncTask<String, Void, Bitmap>{


    @Override
    protected Bitmap doInBackground(String... params) {
        // TODO Auto-generated method stub
        String TAG_ImageLink = params[0];
        Bitmap bm = null;
        try {
            InputStream in = new java.net.URL(TAG_ImageLink).openStream();
            bm = BitmapFactory.decodeStream(in);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return bm;
    }
    @Override
    protected void onPostExecute(Bitmap result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

    }

}

或者您可以使用第三方图片加载库,例如picassovolley's ImageRequest