我正在尝试通过Android连接到使用Spark创建的REST服务器。 我可以使用POSTMAN(Chrome插件)发送POST请求并获得所需结果, 但是当我从我的Android设备发送POST请求时,出现以下错误:
E/error: com.android.volley.NoConnectionError: java.net.ConnectException: failed to connect to /127.0.0.1 (port 4567) after 2500ms: isConnected failed: ECONNREFUSED (Connection refused)
这是服务器部分:
Spark.post("/up", new Route()
{
public Object handle(Request req, Response res)
{
System.out.println(req.queryParams());
return "its something";
}
});
这是关于 Android 的部分:
public void sendHTTPRequest()
{
RequestQueue MyRequestQueue = Volley.newRequestQueue(this);
String url = "http://127.0.0.1:4567/up";
StringRequest MyStringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//This code is executed if the server responds, whether or not the response contains data.
//The String 'response' contains the server's response.
}
}, new Response.ErrorListener() { //Create an error listener to handle errors appropriately.
@Override
public void onErrorResponse(VolleyError error) {
Log.e("error",error.toString());
}
}) {
protected Map<String, String> getParams() {
Map<String, String> MyData = new HashMap<String, String>();
MyData.put("Field", "Value"); //Add the data you'd like to send to the server.
return MyData;
}
};
MyRequestQueue.add(MyStringRequest);
}
我还在清单文件中添加了重要的权限。
我搜索了几个小时,但是仍然无法解决我的问题:( 希望你能帮助我。 最好的祝福, Felix