1

My error:

java.lang.NoSuchFieldError: org.apache.http.message.BasicHeaderValueFormatter.INSTANCE

My code:

public static String POST(String path, JSONObject params, String fileName)
            throws ClientProtocolException, IOException {
        HttpPost request = new HttpPost(url + path);

        MultipartEntity multipartEntity = new MultipartEntity();

        FileBody bin = new FileBody(new File(fileName), "image/jpeg");
        multipartEntity.addPart("photo", bin);
        for (Object obj : params.entrySet()) {
            Entry e = (Entry)obj;
            multipartEntity.addPart((String)e.getKey(), new StringBody((String)e.getValue()));
        }
        request.setEntity(multipartEntity);
        HttpResponse response = httpClient.execute(request);

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String result = "";
        String line = "";
        while ((line = bufferedReader.readLine()) != null)
            result += line;
        bufferedReader.close();
        return result;
    }

My gradle:

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.andreabaccega:android-form-edittext:1.2.1@aar'
    compile files('libs/gson-2.2.1.jar')
    compile files('libs/json-simple-1.1.1.jar')
    compile files('libs/httpmime-4.5.1.jar')
    compile files('libs/httpcore-4.4.3.jar')
    compile files('libs/httpclient-4.5.1.jar')
    compile files('libs/commons-lang3-3.1.jar')
    compile files('libs/commons-logging-1.1.2.jar')
}

I want to upload a file (an image specifically) to a Server using HTTP POST. How to solve this problem?

  • http://stackoverflow.com/a/32702347/2533290 that may help. OR other solution: update your gradle to 1.4.0-beta6 (newer versions can be found here: https://jcenter.bintray.com/com/android/tools/build/gradle/) and say useLibrary 'org.apache.http.legacy' in the android scope hope it helps. – Martin Pfeffer Nov 04 '15 at 03:25

1 Answers1

0

I think you will need to use android port version of http client from apache
Try to change your gradle dependencies to this :

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.andreabaccega:android-form-edittext:1.2.1@aar'
    compile files('libs/gson-2.2.1.jar')
    compile files('libs/json-simple-1.1.1.jar')
    compile('org.apache.httpcomponents:httpmime:4.5.1') {
        exclude module: 'httpclient'
    }
    compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
    compile files('libs/httpcore-4.4.3.jar')
    compile files('libs/commons-lang3-3.1.jar')
    compile files('libs/commons-logging-1.1.2.jar')
}
Niko Adrianus Yuwono
  • 11,012
  • 8
  • 42
  • 64