因此NetBeans IDE可以从数据库生成RESTful Web服务(该表已包装到实体类中)。我跟着这tutorial和RESTful Web服务已成功生成。本地调用由NetBeans IDE从Android生成的RESTful Web服务?
现在,我想从我的Android应用程序调用,但到目前为止没有运气。我用 “Android的异步HTTP客户端 回调,基于HTTP客户端库为Android”
因此,这里是我的代码片段:
customActionBarView.findViewById(R.id.actionbar_done).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// "Done"
String id = generateId();
EditText number = (EditText) findViewById(R.id.caller_phone_number);
EditText information = (EditText) findViewById(R.id.caller_information);
checkAvailability(id, number.getText().toString(), information.getText().toString());
finish();
}
});
这:
public void processWebServices(RequestParams params) {
AsyncHttpClient client = new AsyncHttpClient();
client.post("http://localhost:8080/AndroidRESTful/com.erikchenmelbourne.entities.caller/create", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] response) {
try {
JSONObject obj = new JSONObject(response.toString());
if (obj.getBoolean("status")) {
setDefaultValues();
Toast.makeText(getApplicationContext(), "Information has been sent!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), obj.getString("error_msg"), Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Error Occured [Server's JSON response is invalid]!", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
@Override
public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
if (i == 404) {
Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
} else if (i == 500) {
Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]", Toast.LENGTH_LONG).show();
}
}
});
}
,这里是NetBeans的IDE生成POST方法:
@Path("/create")
@POST
@Consumes({"application/xml", "application/json"})
public void create(@QueryParam("id") int id, @QueryParam("number") String number, @QueryParam("information") String information) {
Caller entity = new Caller (id, number, information);
super.create(entity);
}
我添加了@Path(“ /创建“)注释并修改了一下方法。
请说清楚一点,我对此很新,所以我没有线索。我知道这是由于一些非常愚蠢的错误,但请帮助。程序在
"Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]"
。很明显,我无法很好地连接这两个程序。
你好,谢谢你这么多要拿出来。我正在手机上运行Android应用程序。 Web服务位于我的笔记本电脑上(NetBeans中的一个项目,右键单击它并点击“运行”)。他们都连接到路由器,因此他们在同一个网络上。在谷歌搜索我的IP后,我用IP替换“localhost”。但是,问题依然存在。任何专家可以检查我的路径吗?我真的认为这是因为路径或我需要将参数打包到JSON对象中。谢谢。 – Isley
谷歌搜索会给你你的公共IP - 你需要你的本地网络IP。请查看您的网络设置或运行ifconfig(OSX/Linux)或ipconfig(Windows) – tonys