0

在这里,我必须从 json 数据中获取图像,并且必须在图像视图中设置该图像。我从 json 获得了所有其他必要的数据并将它们设置为文本视图,但无法将图像设置为图像视图。下面是我的代码,

MainActivity.java

public class MainActivity extends ListActivity {
    private Context context;

    // this url returns json encoded data
    private static String url = "http://192.168.0.104/productdetails/product.php";

    private static final String NAME = "name";
    private static final String STORE_NAME = "store_name";
    private static final String PIC = "pic";
    //private static final String TREAD = "Tread";

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

    ListView lv ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new ProgressTask(MainActivity.this).execute();
    }

    private class ProgressTask extends AsyncTask<String, Void, Boolean> {
        private ProgressDialog dialog;

        private ListActivity activity;

        // private List<Message> messages;
        public ProgressTask(ListActivity activity) {
            this.activity = activity;
            context = activity;
            dialog = new ProgressDialog(context);
        }

        private Context context;

        protected void onPreExecute() {
            this.dialog.setMessage("Please wait...");
            this.dialog.show();
        }

        @Override
        protected void onPostExecute(final Boolean success) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
            ListAdapter adapter = new SimpleAdapter(context, jsonlist,
                    R.layout.list_item, new String[] { NAME, STORE_NAME, PIC,
                             }, new int[] {
                            R.id.vehicleType, R.id.vehicleColor, R.id.image });

            setListAdapter(adapter);
            lv = getListView();
        }

        protected Boolean doInBackground(final String... args) {

            JSONParser jParser = new JSONParser();

            // get JSON data from URL
            JSONArray json = jParser.getJSONFromUrl(url);

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

                try 
                {
                    JSONObject c = json.getJSONObject(i);
                    String name = c.getString(NAME);

                    String storename = c.getString(STORE_NAME);
                    String pic = c.getString(PIC);


                    HashMap<String, String> map = new HashMap<String, String>();

                    // Add child node to HashMap key & value
                    map.put(NAME, name);
                    map.put(STORE_NAME, storename);
                    map.put(PIC, pic);

                    jsonlist.add(map);
                }
                catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    }
}

JSONParser.java

public class JSONParser {

    static InputStream iStream = null;
    static JSONArray jarray = null;
    static String json = "";

    public JSONParser() {
    }

    public JSONArray getJSONFromUrl(String url)
    {

        StringBuilder builder = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);

        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("==>", "Failed to download file");
            }
        }

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

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

        // Parse String to JSON object
        try
        {
            jarray = new JSONArray(builder.toString());
        } 

        catch (JSONException e) 
        {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON Object
        return jarray;

    }
}

如何解码图像并在图像视图中显示?

4

0 回答 0