0

我想在我的布局上加载来自web services.

但是每次我都无法从网络服务中获取图像。

当网络服务是这样时,请提供代码:-

    {
        "data": {
            "Success": "1",
            "Message": "Successful",
            "userid": "145",
            "username": "rahul",
            "password": "*****",
            "firstname": "jaydeep",
            "lastname": "darji",
            "mobile": "232312221",
            "email": "jaydeepdajri65@yahoo.in",
            "countryid": "1",
            "countryname": "India",
            "userphoto": "http:\/\/www.sevenstarinfotech.com\/projects\/demo\/okaz\/uploads\/profile\/1681692778Mar2120131938_145.png"
        }
}
4

2 回答 2

0
http:\/\/www.sevenstarinfotech.com\/projects\/demo\/okaz\/uploads\/profile\/1681692778Mar2120131938_145.png

这是图像的 URL。您应该解析出这个 URL,然后下载图像并使用它。

于 2013-04-04T10:32:14.233 回答
0

解析json如下:

try {
            JSONObject json = new JSONObject();
            JSONObject data = json.getJSONObject("data");
            userphoto = data.getString("userphoto");
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

下载图片使用下面的方法。调用它:getBitmapFromURL(userphoto);它让你bitmap在 imageview 中使用它。喜欢 : imagev.setImageBitmap(bitmap);

public static Bitmap getBitmapFromURL(String src) {
        try {
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
于 2013-04-04T10:44:17.483 回答