【问题标题】:Not able to upload image to server using Httprequest library无法使用 Httprequest 库将图像上传到服务器
【发布时间】:2014-06-03 10:11:38
【问题描述】:

我创建了一个应用程序,用户在该应用程序中从图库中选择图像并将该图像上传到服务器。我尝试使用 Httprequest 库进行此操作。但我无法上传它。

代码

HttpRequest request = HttpRequest.post ("https://beta135.hamarisuraksha.com/Web/MobileApp/PostImsafePrflPic.aspx").send ("MODE=PP" + "&ID=" + "8" + "&ext=.jpg" + f);
            request.part ("mode", "MODE=PP");
            request.part ("id", "&ID=34588A34-E969-4723-84FE-E5409B66A5B7");
            request.part ("ext", "&ext=.jpg");
            request.part ("file", f);
            String response = request.body ();
            Log.e ("Image Upload", "" + response);


            return response; 

另外,如果有任何其他好的库可以用来完成这项工作,请告诉我。

【问题讨论】:

    标签: android httprequest multipartform-data


    【解决方案1】:

    试试这个代码和这个库添加 httpmime-4.1-beta1.jar :

    String url = "Your Url";
    
    
                HttpClient client = new DefaultHttpClient();
                client.getParams().setParameter(
                        CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
                HttpPost postMethod = new HttpPost(url);
    
    
                MultipartEntity entity = new MultipartEntity();
    
                  entity.addPart("fname", new StringBody("xyz"));
    
                try {
    
                        File file1 = new File("Your image Path");
                        FileBody bin1 = new FileBody(file1, "images/jpeg");
    
                        entity.addPart("file", bin1);
    
                    postMethod.setEntity(entity);
                    HttpResponse response;
                    response = client.execute(postMethod);
    
                    String result = EntityUtils.toString(response.getEntity());
    
                }
    
                catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-12
      • 1970-01-01
      • 1970-01-01
      • 2021-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多