Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

테랑's Object

Android 에서 Http Mutilpart 를 이용한 File Upload 본문

IT Developer/Android

Android 에서 Http Mutilpart 를 이용한 File Upload

테랑이 2016. 7. 5. 17:38

안녕하세요. 테랑입니다. 오늘은 Android 에서 Http 를 사용한 File Upload 에 대해 알아 보도록 하겠습니다.



1. 일단 Android 에서 HttpClient 를 사용하기 위해 build.gradle 파일에 해당 Library 를 추가해 준다.


dependencies {

    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile 'com.android.support:appcompat-v7:23.0.1'

    compile group: 'org.apache.httpcomponents', name: 'httpclient-android', version: '4.3.5'

    compile (group: 'org.apache.httpcomponents', name: 'httpmime', version: '4.3.6') {

        exclude module: 'org.apache.httpcomponents:httpclient'

    }

}


파란색의 글자들이 추가된 부분이며, 다른 블로그들을 찾아보면 Apache HttpComponent 들을 Download 받아서 Library 추가하는 경우도 있는데, 간편하게 Gradle 설정으로 사용해 보았다.



2. File Upload 소스코드




3. 예외 상황들

Gradle 설정 및 소스 코드를 작성하다 보니 Exception 나는 부분들이 있었는데, 국내 블로그에서는 이것들에 대해 다룬 것들이 많이 없는 것 같아서 언급.


1) SDK 23 을 사용하다 보면 org.apache.http 를 찾을 수 없다는 Error 가 뜨는 경우가 있다. Java 파일에서 cannot find symbol class HttpClient 등과 같은 Error 가 난다. 이것은 마시멜로 (안드로이드 6.0) 이상에서는 org.apache.http 모듈이 포함되지 않아서 발생하는 것인데. 이 때 build.gradle 에서 useLibrary 'org.apache.http.legacy' 를 추가해 주면 해결된다.


2) 소스를 보면 httpPost.setEntity(builder.build()) 라는 부분이 있는데, 이 소스에서 Caused by: java.lang.NoSuchFieldError: No static field INSTANCE of type Lorg/apache/http/message/BasicHeaderValueFormatter; in class Lorg/apache/http/message/BasicHeaderValueFormatter; or its superclasses (declaration of 'org.apache.http.message.BasicHeaderValueFormatter' appears in /system/framework/ext.jar) 이런 Error 가 나는 경우가 있다. 이는 HttpClient Library 의 특정 Version 에서 발생하는 문제로 처음에 4.5.2 Version 을 사용했더니 이런 문제가 발생하여 위 Gradle 적혀 있는 4.3.5 / 4.3.6 Version 으로 변경했더니 문제 없이 사용할 수 있었다. (그냥 Gradle 에 위에 설정을 복사 붙여넣기 해서 사용하면 됩니다)


이상으로 Android 에서 Multipart 를 이용한 File Upload 에 대해 알아봤습니다.


IT is the foundation of all industries. Fighting!! All the developers in the world.

'IT Developer > Android' 카테고리의 다른 글

Android AsyncTask  (0) 2016.07.12
Android Event Listener 만들기  (0) 2016.07.07
Android EditText 공백 Check  (0) 2016.07.07
Android studio 와 SVN 연동  (0) 2016.07.04
Parcelable in Android  (0) 2016.06.26
Comments