1
私はJavaScriptを使ってelggウェブサービスをテストするためのカスタムでシンプルなクライアントをコーディングしています。私は簡単なパラメータでサーバーに投稿要求を送信したいですが。ここでelggウェブサービスクライアント
はElggの中に私の公開されている関数です:
/*
* 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());
}
}
}
しかし、私はこの出力を受けていますし、問題がある:
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
);
、ここでは、POSTリクエストを送信するために私のJavaコードであります投稿要求あり。私はそのコードで何が間違っているのか分かりません:
{"name":"Mousa"}
200
{"status":-1,"message":"Missing parameter name in method test.echo"}
「名前」パラメータが欠落しています。
私が覚えている限りでは