【问题标题】:How would I implement using multipart form data?我将如何使用多部分表单数据来实现?
【发布时间】:2013-08-30 23:15:40
【问题描述】:

为了安全起见,我更改了 HttpPost

我能够让应用加载/拍照,但它无法与 API 对话,因为 API 使用了不同的检索方法。

我确实有一个多部分编码的示例,但不确定如何使用它来实现。 可以在here 找到我正在做什么/需要做什么的示例。

如何使用多部分表单数据实现?

 protected Object doInBackground(Object... arg0) {
    Bitmap bitmapOrg = ((Main) parentActivity).mPhoto;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // compress bitmap into the byte array output stream
    bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 75, baos);
    // form byte array out of byte array output stream
    byte[] ba = baos.toByteArray();
    String encodedImage = Base64.encodeBytes(ba);
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(
            "-----------------------------------");
    // Add your data


    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("image", encodedImage));
    try {
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        InputStream inputstream = entity.getContent();
        StringBuilder sb = new StringBuilder();
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(inputstream));
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        results = sb.toString();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

【问题讨论】:

    标签: java android api multipart


    【解决方案1】:

    首先,您必须先使用多部分方法进行转换。

        Bitmap bitmapOrg = ((Main) parentActivity).mPhoto;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        // compress bitmap into the byte array output stream
        bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 75, baos);
        // form byte array out of byte array output stream
        byte[] ba = baos.toByteArray();
        String encodedImage = Base64.encodeBytes(ba);
        HttpClient httpclient = new DefaultHttpClient();
    

    通过这个实际上位于 stackoverflow 中的Post multipart request with Android SDK,提供有关多部分如何工作的关键示例和示例。

    HttpPost httppost = new HttpPost("some url");
    
        MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);  
        multipartEntity.addPart("Title", new StringBody("Title"));
        multipartEntity.addPart("Nick", new StringBody("Nick"));
        multipartEntity.addPart("Email", new StringBody("Email"));
        multipartEntity.addPart("Description", new StringBody(Settings.SHARE.TEXT));
        multipartEntity.addPart("Image", new FileBody(image));
        httppost.setEntity(multipartEntity);
    
        mHttpClient.execute(httppost, new PhotoUploadResponseHandler());
    

    【讨论】:

      猜你喜欢
      • 2020-01-07
      • 1970-01-01
      • 2022-10-13
      • 2012-12-07
      • 2015-06-23
      • 2017-02-04
      • 2016-05-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多