Note that there are some explanatory texts on larger screens.

plurals
  1. POUnsupported media type error in android when trying to upload image
    text
    copied!<p>I am trying to create an application which uploads an image to the server. I am getting a response, <code>415 unsupported media type</code> from the server. My code is as follows, what changes should I make?</p> <pre><code>class RetreiveFeedTask extends AsyncTask&lt;String, Void, String&gt; { protected String doInBackground(String... url){ try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable(); Bitmap bitmap = drawable.getBitmap(); bitmap.compress(CompressFormat.JPEG, 50, bos); byte[] data = bos.toByteArray(); HttpClient httpClient = new DefaultHttpClient(); HttpPost postRequest = new HttpPost("http://10.155.103.167:9090/RestServer/rest/todos"); String fileName = String.format("File_%d.png", new Date().getTime()); ByteArrayBody bab = new ByteArrayBody(data, fileName); ContentBody mimePart = bab; // File file= new File("/mnt/sdcard/forest.png"); // FileBody bin = new FileBody(file); MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); reqEntity.addPart("file", bab); postRequest.setEntity(reqEntity); int timeoutConnection = 60000; HttpParams httpParameters = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); int timeoutSocket = 60000; HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); HttpConnectionParams.setTcpNoDelay(httpParameters, true); HttpResponse response = httpClient.execute(postRequest); BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")); String sResponse; StringBuilder s = new StringBuilder(); System.out.println("Response: " + response.getStatusLine()); while ((sResponse = reader.readLine()) != null) { s = s.append(sResponse); } //txt.setText("NEW TEXT"+s); } catch (Exception e) { // handle exception here e.printStackTrace(); System.out.println(e.toString()); } return null; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload