我正在编写一个自定义但简单的客户端,以使用Java测试Elgg Web服务。我想使用一个简单的参数向服务器发送发布请求。

这是我在elgg中公开的函数:

function hello_world($name) {
    return "Hello ".$name;
}


elgg_ws_expose_function(
    "test.echo",
    "hello_world",
    array("name" => array("type" => "string", "required" => true)),
    'A testing method which echos back a string',
    'POST',
    false,
    false
);


这是我的Java代码,用于发送发布请求:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package readjson;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;


public class Main {

    public static void main(String[] args) {


        try {
            CloseableHttpClient client = HttpClients.createDefault();
            JSONObject object = new JSONObject();
            String name = "Mousa";
            object.put("name", name);
            HttpPost httpPost = new      HttpPost("http://localhost/elgg/services/api/rest/json?method=test.echo");
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");
        StringEntity entity = new StringEntity(object.toJSONString());
        System.out.println(object);
        httpPost.setEntity(entity);
        CloseableHttpResponse response = client.execute(httpPost);
        System.out.println(response.getStatusLine().getStatusCode());
        HttpEntity httpEntity = response.getEntity();
        StringBuilder stringBuilder = new StringBuilder();
            InputStream inputStream = httpEntity.getContent();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }
            inputStream.close();
            System.out.println(stringBuilder.toString());
        client.close();
        }
        catch(Exception ex) {
            System.out.println(ex.getLocalizedMessage());
        }
    }
}


但是我收到此输出,并且后期请求有问题。我不知道该代码出了什么问题:

{"name":"Mousa"}

200


{"status":-1,"message":"Missing parameter name in method test.echo"}


它说“名称”参数丢失!!!

请帮忙

最佳答案

据我所知,Elgg的Web服务无法在请求正文中处理JSON,而是使用序列化查询字符串,例如:

name=Mousa&interest[]=interest1&interest[]=interest2

Elgg可能会使用parse_str(),因此这可能会有所帮助:http://php.net/manual/en/function.parse-str.php

09-09 20:42