3

I used the following code to post some data to my site. I tested the site with Postman using the same data and it works properly, but when running the following code on Android Studio, response.getStatusLine() returns HTTP/1.1 415 Unsupported Media Type.

       HttpClient httpClient = new DefaultHttpClient();
       HttpPost httpPost = new HttpPost("http://www.example.com/entity/node");
       HttpResponse response = null;
       String responseBody = "";


       try{
           JSONObject json = new JSONObject();
           JSONObject c0 = new JSONObject();
           JSONObject c00 = new JSONObject();
           JSONObject c1 = new JSONObject();
           JSONObject c2 = new JSONObject();


           json.put("_links",c0);
           json.put("title",c1);
           json.put("type",c2);

           c0.put("type",c00);
           c00.put("href","http://www.example.com/rest/type/node/article");
           c1.put("value", "test string");
           c2.put("target_id", "article");


           String base64EncodedCredentials = "Basic " + Base64.encodeToString(
                   ("user"+ ":" + "pass").getBytes(),
                   Base64.NO_WRAP);


           StringEntity se = new StringEntity(json.toString());



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

           httpPost.setHeader(HTTP.CONTENT_TYPE, "application/json");
           httpPost.setHeader("Authorization", base64EncodedCredentials);

           httpPost.setEntity(se);

           // Execute HTTP Post Request
           response = httpClient.execute(httpPost);

           String jsonResponse = EntityUtils.toString(response.getEntity());
           JSONObject jsonObj = new JSONObject(jsonResponse);

           if (response.getStatusLine().getStatusCode() == 200) {
               Log.v("response ok", "ok response :/");
           } else {
               Log.v("response not ok", "Something went wrong :/");
           }
       catch (Exception e){
           Log.v("Error posting:",e.getMessage());
       }

I tested each of these lines separately and together, but none of them work.

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

           httpPost.setHeader(HTTP.CONTENT_TYPE, "application/json");

Why does it return HTTP/1.1 415 Unsupported Media Type?

  • Do you perhaps need to set a X-CSRF-Token? – undersound Feb 5 '17 at 19:59
  • Maybe you forgot to add the type of request you are sending as a query parameter? Like so http://www.example.com/entity/node?_format=hal_json – Canutza Jul 19 '17 at 7:45
0

I have faced same problem with same situation as you described above. After spending 2-3 hours I have got data through:

$formdata = file_get_contents('php://input');

It's worked for me.

0

Mde, Instead of using HttpClient try using OkHttpClient like this Drupal 8 Android app at https://github.com/onedrupal/One-Drupal-Android.

request = new Request.Builder()
                        .header("Content-Type", "application/json")
                        .url(url)
                        .patch(api_body)
                        .build();
OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(20, TimeUnit.SECONDS)
                .writeTimeout(20, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS)
                .addInterceptor(new AddCookiesInterceptor())
                .build();

AddCookiesInterceptor at https://github.com/onedrupal/One-Drupal-Android/blob/master/app/src/main/java/com/technikh/onedrupal/network/AddCookiesInterceptor.java

public class AddCookiesInterceptor implements Interceptor {
    @Override
    public Response intercept(Interceptor.Chain chain) throws IOException {
        Request.Builder builder = chain.request().newBuilder();
.......
Preferences.getDefaultPreferences().getStringSet(Preferences.PREF_COOKIES, new HashSet<>());
        for (String cookie : preferences) {
            builder.addHeader("Cookie", cookie);
        }
......
        builder.addHeader("X-CSRF-Token", mAuthPreferences.getAuthToken());

        return chain.proceed(builder.build());
    }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.