android 从服务器端下载图片列表 服务器端封装json

来源:互联网 发布:linux开发视频教程 编辑:程序博客网 时间:2024/06/04 21:47
一般只获得几张图片 可以直接访问图片URL 但有两种情况是不行的 完成不了需求:       一:服务器端用的服务器不支持文件资源共享 如:jboss       二:当客户端请求多张 如:用户列表头像 这时以本人的开发经验得出:图片放在服务器端的一个磁盘文件夹中 客户端将图片名字传到服务器端 服务器端将图片用json装起来传到客户端 代码如下:(中间有数据库) 客户端主要代码: protected Integer doInBackground(Object... params) {    if (NetHelper.isNetAvailable(POIDetailActivity.this)) {     imagePath = (String)params[0];     iv = (ImageView)params[1];     HashMap<String, String> item = new HashMap<String, String>();     String url = String.format(Constants.COMMON_URL, 107);     item.put("image", imagePath);     item.put("type", "0");     try {      InputStream is = NetHelper.doPostImageRequest(url, item, null);      if (is != null) {       try {        byte[] data = Tools.inputStreamToBytes(is);        if(data != null){                    bm = BitmapFactory.decodeByteArray(data, 0, data.length);              }         } catch (Exception e) {        LogHelper.errorLog(TAG, e.toString());        return Constants.CODE_NETWORK_CONNECT_ERROR;       }      }     } catch (ClientProtocolException e) {      LogHelper.errorLog(TAG, e.toString());      return Constants.CODE_CLIENT_PROTOCOL_ERROR;     } catch (IOException e) {      LogHelper.errorLog(TAG, e.toString());      return Constants.CODE_NETWORK_CONNECT_ERROR;     } catch (JSONException e) {      LogHelper.errorLog(TAG, e.toString());      return Constants.CODE_JSON_ERROR;     }    } else {     return Constants.CODE_NO_AVAILABLE_NETWORK;    }    return Constants.CODE_SUCCESS;   }    服务器端主要代码: /*请求图片资源*/public static InputStream doPostImageRequest  (String url, Map<String, String> params, Map<String, String> headData)   throws ClientProtocolException, IOException, JSONException{   HttpPost httpPost = new HttpPost(url);   LogHelper.debugLog(TAG, ">>>>>>>>>>doPostRequest.url:"+url);   InputStream is = null;      // 设置HTTP POST 请求的头信息   if(headData != null)    for(Map.Entry<String, String> entry : headData.entrySet())     httpPost.setHeader(entry.getKey(), entry.getValue());      // 封装POST请求传送的数据   ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();   if(params != null)    for(Map.Entry<String, String> entry : params.entrySet())     pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));      // 对POST请求的数据进行UTF-8转码   UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(pairs, ENCODING);   httpPost.setEntity(formEntity);   HttpResponse response = getHttpClient().execute(httpPost);   if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){    HttpEntity resEntity = response.getEntity();    is = resEntity.getContent();   }else{    httpPost.abort();   }