2

I am trying to POST a data from android to restful webservices using loopj.Web services is working fine with POSTMAN testing. When i try to post its failing and i get following message in logcat.

cz.msebera.android.httpclient.client.HttpResponseException: Unsupported Media Type

From the error i think i am not adding Content-Type - application/json to the header. I saw few examples on how to add. But its really confusing.can someone help me.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etemployeeId = (EditText)findViewById(R.id.employeeId);
        etfirstName = (EditText)findViewById(R.id.firstName);

        buttonRegister = (Button) findViewById(R.id.btnRegister);
        buttonRegister.setOnClickListener(new View.OnClickListener() { 

            @Override
            public void onClick(View view) {   
                registerUser(view);
            }
        }); 
       getEmployees();
    } 
    public void registerUser(View view) {
        RequestParams params = new RequestParams();

        String employeeId = etemployeeId.getText().toString();
        String firstName = etfirstName.getText().toString();
        params.put("employeeId", employeeId);
        params.put("firstName", firstName);

    EmployeeRestClient.post("RestExample/employee", params, new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
 Log.d("Callback", "onSuccess response");
                Toast.makeText(MainActivity.this,
                        "You are successfully registered!", Toast.LENGTH_LONG).show();
                    }
       @Override
    public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                    super.onFailure(statusCode, headers, responseString, throwable);
                    Log.d("Callback", "onFailure responseString");
                    Toast.makeText(MainActivity.this,
                            "failed", Toast.LENGTH_LONG).show();
                }                   
            });
        }
    }
user3785322
  • 153
  • 1
  • 6
  • 15

2 Answers2

4

You have two options. One: just call .addHeader(headerKey, headerValue) on EmployeeRestClient, to add header. Second: in a similar case I had to put user as JSONObject wrapped in a StringEntity, RequestParams didn't work for me. In this case, the fourth parameter is also the content type. You don't need both, they will override each other. In any case, put the cursor over the post method and press Ctrl + P to find parameters info and alternatives:

asyncHttpClient = new AsyncHttpClient();

    StringEntity jsonEntity = null;

    asyncHttpClient.addHeader("Accept", "application/json");

    asyncHttpClient.addHeader("Content-Type", "application/json");

    JSONObject jsonParams = new JSONObject();
    try {
        jsonParams.put("name", "john");
        jsonParams.put("email", "john@gmail.com");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    try {
        jsonEntity = new StringEntity(jsonParams.toString());
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    asyncHttpClient.post(context, "http://myUrl", jsonEntity, "application/json", new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            super.onSuccess(statusCode, headers, response);

        }

        @Override
        public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
            super.onFailure(statusCode, headers, throwable, errorResponse);
        }
    });
Pedro Gonzalez
  • 1,429
  • 2
  • 19
  • 30
1

Add header in loopj

AsyncHttpClient client = new AsyncHttpClient();
client.addHeader("Accept-language", "fa");
client.addHeader("Authorization", "Token");
Masoud Siahkali
  • 5,100
  • 1
  • 29
  • 18