2011-01-16 3 views
3

documented here 메서드를 통해 새로운 API를 통해 인기있는 서비스 인 Dailybooth에 사진을 업로드하려고합니다. 여기유효하지 않은 Content-Length가 서버로 전송됩니까?

<html><head><title>411 Length Required</title>... 

내가이 데이터를 보내는 데 사용하고 코드입니다 :

// 2: Build request 
HttpClient httpclient = new DefaultHttpClient(); 
SharedPreferences settings = DailyboothShared.getPrefs(DailyboothTakePhoto.this); 
String oauth_token = settings.getString("oauth_token", ""); 
HttpPost httppost = new HttpPost(
     "https://api.dailybooth.com/v1/pictures.json?oauth_token=" + oauth_token); 
Log.d("upload", "Facebook: " + facebook); 
Log.d("upload", "Twitter: " + twitter); 
try { 
    InputStream f = getContentResolver().openInputStream(snap_url); 
    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
    entity.addPart("picture", new InputStreamBody(f, snap_url.getLastPathSegment())); 
    entity.addPart("blurb", new StringBody(blurb)); 
    entity.addPart("publish_to[facebook]", new StringBody(facebook)); 
    entity.addPart("publish_to[twiter]", new StringBody(twitter)); 
    httppost.setEntity(entity); 
    HttpResponse response = httpclient.execute(httppost); 
    Log.d("upload", response.toString()); 
    int statusCode = response.getStatusLine().getStatusCode(); 
    if (statusCode == 200) { 
     // do something? 
    } else { 
     Log.d("upload", "Something went wrong :/"); 
    } 
    Log.d("upload", EntityUtils.toString(response.getEntity())); 
} catch (Exception ex) { 
    ex.printStackTrace(); 
} 

나는 '무슨 아이디어가있어

문제는 서버와 응답이다 잘못하고있다.

답변

8

MultipartEntity의 내용을 설명하는 StringBodyInputStreamBody 클래스를 사용하고 있습니다. 출처를 보면 StringBody.getContentLength()은 문자열의 길이를 반환하지만 InputStreamBody은 항상 -1을 반환하며, 크기를 모르는 상태에서 일부 데이터를 서버에 업로드해야하는 경우에 해당하며 데이터가 올라 오는 동안 업로드가 시작됩니다. 그 시내.

new InputStreamBody(f, snap_url.getLastPathSegment()) { 

    public long getContentLength() { 
     return /*your length*/; 
    } 
} 

:

그런 다음 콘텐츠 길이를 설정 할 수 있도록하려는 경우 그런 경우가 있다면 당신은 무엇을 할 수 있는지, 사전에 스트림의 크기를 알 필요는 InputStreamBody 그런 식으로 설정하는 것입니다 당신은 당신이 작업 말했듯이 나 ...

byte[] 배열 스트림을 덤프하고 당신이 그것을 통해 보내기 전에 메모리에 데이터를 캐시 할 필요가 당신이 스트리밍 기능을 잃어 그렇게 물론, InputStreamBodyByteArrayInputStream 전달 이미지에,이 이미지는 File 언제나입니까? 그렇다면 올바른 content-length을 반환하는 FileBody도 있습니다.

+0

감사합니다. 이러한 제안을 시도해 보겠습니다. 안타깝게도 필자는 안드로이드 시스템이 나를 그렇게 할 수 없어도 쉽게 "File"객체를 얻으려고 노력했다. –

+0

어떻게 파일을 읽을 수 없습니까? java.io.File은 SDK의 일부이므로 안드로이드에서 그렇게 할 수 있다고 확신합니다. 어쩌면 일부 사용권 문제 일 수 있습니다 – CodegistCRest

+0

URL을 "content : // blah/blah/blah"로 반환 한 카메라 및 다른 응용 프로그램에서 파일을 가져 오려고합니다. 어떤 이유로 File이 열리지 않습니다. 그러나 필자는 파일 설명자를 사용하여 길이를 "계산"할 수있게 변경했습니다. 감사 : D –

관련 문제