簡體   English   中英

elgg web服務客戶端

[英]elgg web services client

我正在編寫一個自定義但簡單的客戶端來測試使用Java的elgg Web服務。 我想用一個簡單的參數向服務器發送一個post請求但是。

這是我在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() ,所以這可能會有所幫助: httpparse_str()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM