2013-08-30 59 views
0

我更改了我的安全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; 
} 

回答

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(); 

前往以下,這實際上是在計算器,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()); 
相關問題