图片未使用来自网址

时间:2017-06-20 11:27:30

标签: android json

这是我的代码。 图像空间保持空白。没有加载。 这里我的错误是什么?
我需要什么样的代码。 给出更多的定义。

public class MainActivity extends AppCompatActivity {

        private String TAG = MainActivity.class.getSimpleName();

        private ProgressDialog progressDialog;
        private ListView listView;

        // JSON data url
        private static String Jsonurl = "http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors";
        ArrayList<HashMap<String, String>> contactJsonList;

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

            contactJsonList = new ArrayList<>();
            listView = (ListView) findViewById(R.id.listview);
            new GetContacts().execute();
        }

        private class GetContacts extends AsyncTask<Void, Void, Void> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                // Showing progress dialog
                progressDialog = new ProgressDialog(MainActivity.this);
                progressDialog.setMessage("Please wait...");
                progressDialog.setCancelable(false);
                progressDialog.show();
            }

            @Override
            protected Void doInBackground(Void... arg0) {
                HTTPHandler httpHandler = new HTTPHandler();

                // request to json data url and getting response
                String jsonString = httpHandler.makeServiceCall(Jsonurl);
                Log.e(TAG, "Response from url: " + jsonString);
                if (jsonString != null) {
                    try {
                        JSONObject jsonObject = new JSONObject(jsonString);
                        // Getting JSON Array node
                        JSONArray contacts = jsonObject.getJSONArray("actors");

                        for (int i = 0; i < contacts.length(); i++) {

                            JSONObject c = contacts.getJSONObject(i);
                            String name = c.getString("name");
                            String country = c.getString("country");
                            String spouse = c.getString("spouse");
                            String dob = c.getString("dob");
                            String description = c.getString("description");
                            String children = c.getString("children");
                            String image = c.getString("image");

                            // tmp hash map for single contact
                            HashMap<String, String> contact = new HashMap<>();

                            // adding each child node to HashMap key => value
                            contact.put("name", name);
                            contact.put("country", country);
                            contact.put("spouse", spouse);
                            contact.put("dob", dob);
                            contact.put("description", description);
                            contact.put("children", children);
                            contact.put("image", image);

                            // adding contact to contact list
                            contactJsonList.add(contact);
                        }
                    } catch (final JSONException e) {
                        Log.e(TAG, "Json parsing error: " + e.getMessage());
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(), "Json parsing error: " + e.getMessage(),Toast.LENGTH_LONG).show();
                            }
                        });
                    }
                } else {
                    Log.e(TAG, "Could not get json from server.");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),"Could not get json from server.",Toast.LENGTH_LONG).show();
                        }
                    });
                }
                return null;
            }

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

                /**     * Updating parsed JSON data into ListView    * */
                ListAdapter adapter = new SimpleAdapter(MainActivity.this, contactJsonList, R.layout.row,
                        new String[]{"name","country", "spouse", "dob", "description", "children", "image"},
                        new int[]{R.id.name, R.id.country, R.id.spouse, R.id.dob, R.id.description, R.id.children, R.id.imageview});

                listView.setAdapter(adapter);
            }
        }
    }

感谢您的帮助

3 个答案:

答案 0 :(得分:1)

如果要从URL加载图像使用自定义适配器并使用picasso或Glide库加载图像。 要么 如果您想使用simpleAdapter,请查看此链接Image from URL in ListView using SimpleAdapter

答案 1 :(得分:0)

你可以使用Glide库从url加载图片看下面的代码它可以用简单的方式帮助你

编译此库

compile 'com.github.bumptech.glide:glide:4.0.0-RC0'

比像这样加载图片

Glide.with(HomeClass.this)
            .load(userProfileUrl)
            .centerCrop()
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .skipMemoryCache(true)
            .dontAnimate()
            .into(imageview);

答案 2 :(得分:0)

您想从网址加载图片列表吗?然后 看看下面的链接,有详细的例子使用json和volly库处理图像列表。 Example

我希望这会对你有所帮助。