1

因此,我最近在我的应用程序中集成了 Volley API,以提供云存储解决方案 (REST)。因为,在尝试通过 HTTP POST 发送 RDF(文本/乌龟)数据时遇到一些麻烦,我从未使用过 API。REST 服务器运行良好,因为我发送 GET 和 POST 请求(通过 Postman Chrome 应用程序)并且每次收到 200 和 201 响应。尽管我设法通过 API 发送了一个简单的 GET 请求,但当我发送 HTTP POST 时出现 400 错误。

代码如下:

//the RDF turtle payload to send over HTTP POST
final String bodyPost = "@prefix : <http://xxx.xxx.xxx.gr/xxx/schema/xxx#> ." + "\n" +
                    "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> ." + "\n" +
                    "[a :Patient ;" + "\n" +
                    ":lastName "+'"'+"Aylakiotis"+'"'+"^^xsd:string ; " + "\n" +
                    ":firstName "+'"'+"Yiannis"+'"'+"^^xsd:string ;" + "\n" +
                    ":dateOfBirth "+'"'+"1970-04-14"+'"'+"^^xsd:date ;" + "\n" +
                    ":amka "+'"'+"12345678903"+'"'+"^^xsd:string ;" + "\n" +
                    ":gender :Male ;" + "\n" +
                    "] .";
RequestQueue queue = Volley.newRequestQueue(this);
final String URL ="http://xxx.xxx.xxx:8080/xxx/";
EditText folderTitle = (EditText) findViewById(R.id.folderTitle);
StringRequest strReq = new StringRequest(Request.Method.POST, URL,
            new Response.Listener() {

                @Override
                public void onResponse(Object response) {
                    folderTitle.setText("Response is: " + response.toString().substring(0,500));

                }

           }, new Response.ErrorListener() {
               @Override
               public void onErrorResponse(VolleyError error) {
                   folderTitle.setText("Error: " + error.getMessage());
               }
           }) {



        @Override
        public byte[] getBody() throws AuthFailureError {
            System.out.println("string post data: " + bodyPost);
            if (params != null && params.size() > 0) {
                System.out.println("string post data: " + bodyPost);
                return encodeParameters(params, getParamsEncoding());
            }
            return bodyPost.getBytes();
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<String, String>();
            String creds = String.format("%s:%s", "xxx", "xxx");
            String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT);
            headers.put("Authorization", auth);
            headers.put("Content-Type", "text/turtle");
            return headers;
        }

    };

    // Add the request to the RequestQueue.
    queue.add(strReq);

String bodyPost是我想以海龟 RDF 格式发送的数据负载。我把它放在我的getBody()方法中,但是我仍然收到 400 错误请求。我已经通过 Postman Chrome 应用程序通过 POST http 发送了这个字符串,它可以工作(201 Created)。我看到大多数实现都有getParams(),但这需要键/值对,而我正在使用三元组,我想将它们作为整个原始数据字符串发送

4

1 回答 1

0

将您的内容类型设置为 text/turtle。像这样覆盖您的请求标头:

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    final HashMap<String, String> params = new HashMap<String, String>();
    params.put("Content-Type", "text/turtle");
    return params;
}
于 2015-02-24T11:55:10.973 回答