4

MultipartEntity에 3 개의 이미지를 전달 중입니다. 이 방법이 효과적이지만 String 값을 MultipartEntity에 전달하는 방법을 모르겠습니다. 다음은 내 코드입니다 :MultipartEntity에서 문자열 변수를 전달하는 방법은 무엇입니까?

public void executeMultipartPost(Bitmap bm, int i) throws Exception { 
    try { 
     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
     bm.compress(CompressFormat.JPEG, 75, bos); 
     byte[] data = bos.toByteArray(); 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpPost postRequest = new HttpPost("http://www.3dsoles.com/droidapp/index.php"); 

     ByteArrayBody bab = new ByteArrayBody(data, "image" + i + ".jpg"); 
     // File file= new File("/mnt/sdcard/forest.png"); 
     // FileBody bin = new FileBody(file); 
     MultipartEntity reqEntity = new MultipartEntity(
     HttpMultipartMode.BROWSER_COMPATIBLE); 
     reqEntity.addPart("file", bab); 
     // reqEntity.addPart("userID", Constants.mUserImei); 
     reqEntity.addPart("photoCaption", new StringBody("sfsdfsdf")); 
     postRequest.setEntity(reqEntity); 
     HttpResponse response = httpClient.execute(postRequest); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
     response.getEntity().getContent(), "UTF-8")); 
     String sResponse; 
     StringBuilder s = new StringBuilder(); 
     while ((sResponse = reader.readLine()) != null) { 
      s = s.append(sResponse); 
     } 
     System.out.println("Response: " + s); 
    } catch (Exception e) { 
     BugSenseHandler.log("Uploading File", e); 
    } 
} 
+0

당신이 나를 도울 수 @Dipak –

답변

13

당신은 추가해야 additional jar files이 링크를 참조하십시오 ..

HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("http://www.tumblr.com/api/write"); 

try 
{ 
    MultipartEntity entity = new MultipartEntity(); 
    entity.addPart("type",new StringBody("yourstring")); 
    httppost.setEntity(entity); 
    HttpResponse response = httpclient.execute(httppost); 
} 
catch (ClientProtocolException e) 
{ } 
catch (IOException e) 
{ } 
관련 문제