2014-10-16 2 views
0

다른 값과 함께 이미지 (base64로 인코딩 됨)를 PHP 웹 서버에 업로드하려고합니다. PHP 웹 서버는이 정보를 취하여 추가 처리를 수행합니다.HttPost 및 HttpClient를 사용하여 base64로 인코딩 된 이미지 업로드

folowing 함수는 요청을 실행하는 AsyncTask의 일부입니다.

다른 값을 직접 연결하여 postData를 작성합니다.

JsonUrlLook 클래스는 데이터를 전송할 올바른 URL을 찾는 데 도움이됩니다. 나는 그것을 확인했다. 맞습니다.

생성 된 HttpPost를 실행하려고하면 내 응답이 null입니다. 왜 이거지?

private String getPostData(String arrayString, String base64Image, String boundary) { 
    String remoteMethod = "uploadPic"; 
    String postData = ""; 

    // Add remote method name 
    postData += "--" + boundary + "\r\n"; 
    postData += "Content-Disposition: form-data; name=\"" + boundary + "[method]\"\r\n\r\n"; 
    postData += remoteMethod; 

    postData += "--" + boundary + "\r\n"; 
    postData += "Content-Disposition: form-data; name=\"" + boundary + "[params]\"\r\n\r\n"; 
    postData += arrayString; 

    postData += "--" + boundary + "\r\n"; 
    postData += "Content-Disposition: form-data; name=\"" + boundary + "\"\r\n\r\n"; 
    postData += "filename=\"image.jpg\"\r\n\r\n"; 
    postData += base64Image; 

    postData += "--" + boundary + "--"; 

    return postData; 
} 

private String uploadPicture(String arrayString, String base64Image) { 

    String boundary = "tx_fpoemobile_pi2"; 

    // Get post data 
    String postData = getPostData(arrayString, base64Image, boundary); 

    // Get webservice uri 
    JsonUrlLookup lookup = new JsonUrlLookup(context); 
    URL webserviceUrl = lookup.buildUrl(context.getResources().getString(R.string.page_uid_webservice)); 
    URI webserviceUri = null; 
    try { 
     webserviceUri = new URI(webserviceUrl.toString()); 
    } catch(Exception e) { 
     // TODO 
    } 

    // Build client and post 
    HttpClient httpClient = new DefaultHttpClient(); 
    HttpPost httpPost = new HttpPost(webserviceUri); 

    // Set header of post 
    httpPost.setHeader("Content-Type", "multipart/form-data; boundary=" + boundary); 
    httpPost.setHeader("Content-Length", String.valueOf(postData.length())); 

    // Set body 
    try { 
     httpPost.setEntity(new StringEntity(postData)); 
    } catch (Exception e) { 
     // TODO 
    } 

    HttpResponse response = null; 
    try { 
     response = httpClient.execute(httpPost); 
    } catch (ClientProtocolException e) { 
     // TODO 
    } catch (IOException e) { 
     // TODO 
    } 

    return "uploadPictureSuccess"; 
} 

답변

0

당신은 당신은 아무것도를 보낼 수있는 이미지 대신에 응답

httpPost.setEntity(entity); 
HttpResponse response = httpClient.execute(httpPost); 

BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")); 



String sResponse; 
while ((sResponse = reader.readLine()) != null) 
{ 
    s = s.append(sResponse); 
} 

if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) 
{ 
    return s.toString(); 
}else 
{ 
    return "{\"status\":\"false\",\"message\":\"Some error occurred\"}"; 
} 
} catch (Exception e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

을 받고이

public void connectForMultipart() throws Exception { 
    con = (HttpURLConnection) (new URL(url)).openConnection(); 
    con.setRequestMethod("POST"); 
    con.setDoInput(true); 
    con.setDoOutput(true); 
    con.setRequestProperty("Connection", "Keep-Alive"); 
    con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); 
    con.connect(); 
    os = con.getOutputStream(); 
} 

public void addFormPart(String paramName, String value) throws Exception { 
    writeParamData(paramName, value); 
} 

public void addFilePart(String paramName, String fileName, byte[] data) throws Exception { 
    os.write((delimiter + boundary + "\r\n").getBytes()); 
    os.write(("Content-Disposition: form-data; name=\"" + paramName + "\"; filename=\"" + fileName + "\"\r\n" ).getBytes()); 
    os.write(("Content-Type: application/octet-stream\r\n" ).getBytes()); 
    os.write(("Content-Transfer-Encoding: binary\r\n" ).getBytes()); 
    os.write("\r\n".getBytes()); 

    os.write(data); 

    os.write("\r\n".getBytes()); 
} 
public void finishMultipart() throws Exception { 
    os.write((delimiter + boundary + delimiter + "\r\n").getBytes()); 
} 

처럼 작동합니다. 이 경우

당신은

+0

httpmime-4.2.1-1.jar가 나는 바이너리로 base64로 인코딩 된 문자열로 이미지를 전송하지 할 필요가 있다고합니다. 하지만 여하튼, 나는 당신의 해결책을 시도 할 것입니다. – Klaus

+0

"writeParamData"함수를 설명해 주시겠습니까? – Klaus

+0

"응답 받기"게시물에서 httpClient와 httpPost를 설정 엔티티와 함께 ​​사용합니다. 이것은 첫 번째 부분과 많이 다릅니다. 그들은 서로 연결되어 있습니까, 아니면 완전히 다른 것입니까? 나는 그것을 얻지 않는다. – Klaus