0

HttpPost为了安全起见,我改变了我所拥有的。

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

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

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

 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;
}
4

1 回答 1

0

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

    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 中,使用 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());
于 2013-11-01T23:25:26.850 回答