Android凌空发送'错误请求'每次都用不同的方法

时间:2018-01-30 15:12:17

标签: android http post request android-volley

我是Android Studio新手,刚开始使用Http Requests。我已经使用Oracle数据库创建了一个REST API。我一直在尝试使用Android Volley将变量从我的移动应用程序发送到数据库。我使用了不同的方法,如字符串请求和JSON对象请求,每次收到400的状态代码。

以下是我的代码:

private void sendRequest() {
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://***.**.***.***:8080/engAppApi/webservices/engineerTable/";

final JSONObject jsonObject = new JSONObject();
try {
    jsonObject.put("machineType", machineType);
    jsonObject.put("workOrderNumber", workOrderNumber);
    jsonObject.put("employeeName", employee);
    jsonObject.put("activity", activity);
    jsonObject.put("durationHours", durationHours);
    jsonObject.put("durationMins", durationMins);
    jsonObject.put("downtimeHours", downTimeHours);
    jsonObject.put("downtimeMins", downTimeMins);
    jsonObject.put("details", details);
    jsonObject.put("createdTimestamp", currentDateandTime);

} catch (JSONException e) {
    // handle exception
}
JsonObjectRequest putRequest = new JsonObjectRequest(Request.Method.PUT, url, jsonObject,
        new Response.Listener<JSONObject>()
        {
            @Override
            public void onResponse(JSONObject response) {
                // response
                Log.d("Response", response.toString());
                Toast.makeText(MainActivity.this, response.toString(), Toast.LENGTH_SHORT).show();
            }
        },
        new Response.ErrorListener()
        {
            @Override
            public void onErrorResponse(VolleyError error) {
                // error
                    // As of f605da3 the following should work
                    NetworkResponse response = error.networkResponse;
                    if (error instanceof ServerError && response != null) {
                        try {
                            String res = new String(response.data,
                                    HttpHeaderParser.parseCharset(response.headers, "utf-8"));
                            Toast.makeText(MainActivity.this, "ERROR OCCURED: " + error.toString(), Toast.LENGTH_SHORT).show();
                            // Now you can use any deserializer to make sense of data
                        } catch (UnsupportedEncodingException e1) {
                            // Couldn't properly decode data to string
                            e1.printStackTrace();
                        }
                    }
                }
        }
) {

    @Override
    public Map<String, String> getHeaders()
    {
        Map<String, String> headers = new HashMap<String, String>();
        //  headers.put("Content-Type" , "application/json");
        headers.put("Content-Type", "application/json;charset=utf-8");
        headers.put("securityToken", "Good Token");
        return headers;
    }

    @Override
    public byte[] getBody() {
        try {
            Log.i("json", jsonObject.toString());
            return jsonObject.toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String getBodyContentType() {
        return "application/json";
    }
};

queue.add(putRequest);
queue.start();


}

任何人都可以看到我是否遗漏了任何内容,因为我已阅读了有关请求期望的所有文档。

非常感谢所有帮助。

0 个答案:

没有答案