【问题标题】:Android download image from server using jsonAndroid使用json从服务器下载图像
【发布时间】:2012-02-07 12:25:22
【问题描述】:

我的应用程序包含几个按钮,我想要的是通过单击任何按钮,它将引导我到一个 URL,该 URL 又将我带到一个 json 对象页面,然后为我提供一个要显示的图像源我的安卓设备。

例如:按钮 1 -> http://a.b.c.d/loadview.htm?buttonid=B1 -> Json 对象(img src- url 到图像文件) -> 显示在我的 android 设备上。

提前致谢:)

【问题讨论】:

    标签: android json


    【解决方案1】:

    这是工作代码:从服务器下载图像并在 android 中显示

    public void onCreate(Bundle savedInstanceState) 
    
    { 
    
    Bitmap bitmap = DownloadImage("http://www.aaa.com/images//29_13.jpeg");
    
    ImageView img = (ImageView) findViewById(R.id.imagefromserver);
    
    img.setImageBitmap(bitmap);
    
    }
    
    
    private InputStream OpenHttpConnection(String urlString) 
        throws IOException
    
     {
    
     InputStream in = null;
    
            int response = -1;
    
            URL url = new URL(urlString); 
            URLConnection conn = url.openConnection();
    
            if (!(conn instanceof HttpURLConnection))                     
                throw new IOException("Not an HTTP connection");
    
            try{
                HttpURLConnection httpConn = (HttpURLConnection) conn;
                httpConn.setAllowUserInteraction(false);
                httpConn.setInstanceFollowRedirects(true);
                httpConn.setRequestMethod("GET");
                httpConn.connect(); 
    
                response = httpConn.getResponseCode();                 
                if (response == HttpURLConnection.HTTP_OK) {
                    in = httpConn.getInputStream();                                 
                }                     
            }
            catch (Exception ex)
            {
                throw new IOException("Error connecting");            
            }
            return in;     
        }
        private Bitmap DownloadImage(String URL)
        {        
            Bitmap bitmap = null;
            InputStream in = null;        
            try {
                in = OpenHttpConnection(URL);
                bitmap = BitmapFactory.decodeStream(in);
                in.close();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            return bitmap;                
        }
    

    【讨论】:

      【解决方案2】:
      You need to open a HttpConnection with the url to image source and download it.
      Here is the code: 
      
      Bitmap getTheBitmap(String yourImgSrcUrl)
      {
      URL url = null;
      Bitmap bitmap = null;
      DataInputStream fileInputStream = null;
      try
      {
      String urlPath = yourImgSrcUrl;
      urlPath = urlPath.replace(" ", "%20"); // to replace any blank spaces
      url = new URL(urlPath);
      connection = (HttpURLConnection) url.openConnection();
      connection.setDoInput(true);
      connection.setUseCaches(false);
      connection.setRequestMethod("GET");
      
      fileInputStream = new DataInputStream(connection.getInputStream());
      byte[] bitmapBytes= new byte[fileInputStream.available()];
      fileInputStream.read(bitmapBytes);
      fileInputStream.close();
      bitmap = BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.length);
      return bitmap;
      }
      catch (IOException e) 
              {
                  e.printStackTrace();
      return null;
              }
      }
      
      
      Kindly check that Bitmap you are getting is not null before setting in the ImageView else it will throw exception.
      

      【讨论】:

      • 嗨,我将位图设为空。可能是什么问题。
      【解决方案3】:

      您的示例网址不起作用,但无论如何我都会尝试解释:

          WebRequest request = HttpWebRequest.Create("test.aspx");
          WebResponse response = request.GetResponse();
      
          using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
          {
              string json = streamReader.ReadToEnd();
      
              JavaScriptSerializer ser = new JavaScriptSerializer();
              var deserializedObject = ser.Deserialize(json, typeof(ControllerBuilder));
      
              if(deserializedObject != null)
                  //cast object to the correct type and use it.
          }
      

      我不建议使用内置 JavaScriptSerializer 类,因为我遇到了性能问题。更好的工具是:

      http://james.newtonking.com/pages/json-net.aspx

      希望对你有所帮助!

      【讨论】:

        猜你喜欢
        • 2017-08-06
        • 2015-09-10
        • 2012-09-22
        • 2016-01-21
        • 1970-01-01
        • 2014-04-07
        • 1970-01-01
        • 2016-05-31
        相关资源
        最近更新 更多