如何将标头添加到loopj POST

时间:2016-08-26 09:16:19

标签: android loopj

我正在尝试使用loopj.Web服务将来自android的数据发布到restful webservices。使用POSTMAN测试工作正常。当我尝试发布失败时,我会在logcat中收到以下消息。

  

cz.msebera.android.httpclient.client.HttpResponseException:不支持的媒体类型

从错误中我认为我没有在标题中添加Content-Type - application / json。我看到几个关于如何添加的例子。但它确实令人困惑。有人帮助我。

    @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();
                }                   
            });
        }
    }

2 个答案:

答案 0 :(得分:2)

您有两种选择。一:只需在EmployeeRestClient上调用.addHeader(headerKey,headerValue),即可添加标题。第二:在类似的情况下,我不得不将用户作为JSONObject包装在StringEntity中,RequestParams对我不起作用。在这种情况下,第四个参数也是内容类型。你不需要两者,他们会互相覆盖。在任何情况下,将光标放在post方法上,然后按Ctrl + P查找参数信息和备选方案:

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);
        }
    });

答案 1 :(得分:0)

在loopj中添加标题

AsyncHttpClient client = new AsyncHttpClient();
client.addHeader("Accept-language", "fa");
client.addHeader("Authorization", "Token");