【问题标题】:How to upload a image to server using HttpPost in android?如何在android中使用HttpPost将图像上传到服务器?
【发布时间】:2011-11-16 13:06:29
【问题描述】:

这是我使用 HttpPost 以 Json 格式将一些数据发送到 php 服务器的代码。我怎样才能将图像与 DATA 一起上传?

HttpPost httppost = new HttpPost(urlToSendCheckListReport);  
JSONObject json = new JSONObject();
        // prepare JSON data:
        json.put("tag_id", tagID);
        json.put("building_id", buildingID);
        json.put("timestamp", new SimpleDateFormat("dd-MM-yyyy, HH:mm:ss").format(Calendar.getInstance().getTime())); 

        JSONArray postjson=new JSONArray();
        postjson.put(json);  

        // Post the data:
        httppost.setHeader("json", json.toString());
        httppost.getParams().setParameter("jsonpost",postjson);  

   HttpResponse response = new DefaultHttpClient().execute(httppost);

    if(response != null)
    { 
     .....Read the response
    }

提前致谢

【问题讨论】:

标签: android image upload http-post


【解决方案1】:

试试这个:

HttpPost httppost = new HttpPost(urlToSendCheckListReport);  
JSONObject json = new JSONObject();
    // prepare JSON data:
    json.put("tag_id", tagID);
    json.put("building_id", buildingID);
    json.put("timestamp", new SimpleDateFormat("dd-MM-yyyy, HH:mm:ss").format(Calendar.getInstance().getTime())); 

    JSONArray postjson=new JSONArray();
    postjson.put(json);  

    // IMAGE
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    bitmap = BitmapFactory.decodeFile(filePath, o2);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
    byte[] data = bos.toByteArray();
    String mediaString = Base64.encodeBytes(data);
    // /IMAGE

    // Post the data:
    httppost.setHeader("json", json.toString());
    httppost.getParams()
    .setParameter("jsonpost",postjson)
    .setParameter("image", new StringBody(mediaString));  //ADD IMAGE PARAMETER

   HttpResponse response = new DefaultHttpClient().execute(httppost);

    if(response != null)
    { 
     .....Read the response
    }

【讨论】:

    【解决方案2】:
    HttpPost httppost = new HttpPost(urlToSendCheckListReport);  
          JSONObject json = new JSONObject();
        // prepare JSON data:
        json.put("tag_id", tagID);
        json.put("building_id", buildingID);
        json.put("timestamp", new SimpleDateFormat("dd-MM-yyyy, HH:mm:ss").format(Calendar.getInstance().getTime())); 
    
        JSONArray postjson=new JSONArray();
        postjson.put(json);  
    
        StringEntity entity = null;
         try {
                        entity = new StringEntity(entityString);
                        entity.setContentType("application/json");
                } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                }
        httppost.setEntity(entity);
        // Post the data:
        httppost.setHeader("json", json.toString());
        httppost.getParams().setParameter("jsonpost",postjson);  
    
    HttpResponse response = new DefaultHttpClient().execute(httppost);
    
    if(response != null)
    { 
     .....Read the response
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-25
      • 2021-03-18
      • 2014-04-19
      • 1970-01-01
      • 1970-01-01
      • 2012-02-06
      相关资源
      最近更新 更多