0

I tried to upload image using AsyncHttpClient, my image uploaded sucsessfully but it can't open from my server, they said " This is not a valid bitmap file or its format is not supported." before I use AsyncHttpClient library, I can upload and see my image in my server, this is how I upload my image before :

public static String POSTUpload(String url, String file) {
    int status = 0;
    String result = "";

    PostMethod method = new PostMethod(url);
    try {
        HttpClient httpClient = new HttpClient();
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(300000);
        httpClient.getHttpConnectionManager().getParams().setSoTimeout(300000);

        method.setRequestEntity(new FileRequestEntity(new File(file), "multipart/form-data"));

        status = httpClient.executeMethod(method);
        result = String.valueOf(status);
        System.out.println("HTTP status " + method.getStatusCode() + " creating con\n\n");
    } catch (ConnectTimeoutException e) {
        Log.e("Timeout Exception: ", e.toString());
    } catch (SocketTimeoutException ste) {
        Log.e("Timeout Exception: ", ste.toString());
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        method.releaseConnection();
    }
    return result;
}

and this is how I upload my Image using AsyncHttpClient :

 private void POSTUpload(String url_upload, String file) {
    RequestParams params = new RequestParams();
    try {
        params.put("uploaded_file", new File(file));
        params.setForceMultipartEntityContentType(true);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    AsyncHttpClient client = new AsyncHttpClient();
    client.setMaxConnections(100);
    client.post(url_upload, params, new AsyncHttpResponseHandler() {

        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            System.out.println("statusCode " + statusCode);
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
        }
    });
}

I don't get why my Image can't show when I using this library. I hope someone can help me to solve my problem. thank you very much.

Menma
  • 799
  • 1
  • 9
  • 35

1 Answers1

0

Try to use OkHttp for posting form data... https://github.com/square/okhttp/blob/master/samples/guide/src/main/java/okhttp3/recipes/PostMultipart.java

Shanmugam
  • 301
  • 1
  • 10