1

I need to make app that take photo, place photo in image view and when I click button to upload to web API service.

My problem is when I try upload photo, I have next error in code.

public void makeHTTPCall() {

    prgDialog.setMessage("Invoking php");
    StringEntity se = null;

    try{
        se = new StringEntity(params.toString());
        se.setContentType("application/json");
    }
    catch (UnsupportedEncodingException e){
        e.printStackTrace();
        return;
    }

    se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));

    AsyncHttpClient image = new AsyncHttpClient();

    image.post(uploadURL, se, new AsyncHttpResponseHandler()

enter image description here

This is my error. And my imports

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;

As u can i see, I have imported .RequestParams and .StringEntity

And when I run app I have this:

enter image description here

Any idea what to do ?

1 Answer 1

1

You are using AsyncHttpClient, and the lib is support file upload with easy way.

image.post(uploadURL, se, new AsyncHttpResponseHandler()

'se' is not StringEntity as like log result 'StringEntity cannot be converted to RequestParams.

image.post(targetUrl, params, Responsehandler(){});

You should do like this.

This is a sample code for file upload with AsyncHttpClient:

RequestParam params = new RequestParams();
params.put("key1", "value1");
params.put("key2", "value2");

File imgFile = new File(filePath);
try {
      params.put("file",imgFile);
} catch(FileNotFoundException e) {}

AsyncHttpClient image = new AsyncHttpClient();
image.post(uploadURL, params, new AsyncHttpResponseHandler(){});
Sign up to request clarification or add additional context in comments.

1 Comment

When i try this sample, I get the status code 415: Unsupported Media Type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.