【问题标题】:Locally call RESTful Web Service generated by NetBeans IDE from Android?从 Android 本地调用 NetBeans IDE 生成的 RESTful Web Service?
【发布时间】:2015-05-14 11:53:24
【问题描述】:

因此 NetBeans IDE 可以从数据库生成 RESTful Web 服务(表已打包到实体类中)。我关注了这个tutorial,已经成功生成了RESTful Web Service。

现在,我想通过我的 Android 应用程序拨打电话,但到目前为止还没有成功。我使用“Android 异步 Http 客户端 一个基于回调的 Android Http 客户端库"

这是我的代码 sn-p:

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("/create") 注释并稍微修改了方法。

请说明一下,我对此很陌生,所以我不知道。我知道这是由于一些非常愚蠢的错误,但请帮忙。程序停在

“发生意外错误![最常见的错误:设备可能不是 连接到 Internet 或远程服务器未启动和运行]"

。很明显,我无法很好地连接这两个程序。

【问题讨论】:

    标签: java android json rest netbeans


    【解决方案1】:

    您的 HTTP URL 是“http://localhost:8080/...”,即它希望到达运行在 Android 设备上的服务器。如果您的 IDE/服务正在您的工作站上运行,那么:

    • 如果您在 Genymotion 模拟器上运行应用程序,请使用 10.0.3.2 而不是 localhost
    • 如果您在 SDK 模拟器上运行应用程序,请使用 10.0.2.2 而不是 localhost
    • 如果您在真实设备上运行应用程序,请替换 IP 您的工作站的地址,并确保设备和工作站位于同一网络上。

    参考文献:

    【讨论】:

    • 嗨,非常感谢您提出这个问题。我正在手机上运行 Android 应用程序。 Web 服务在我的笔记本电脑上(NetBeans 中的一个项目,右键单击它并点击“运行”)。它们都连接到路由器中,因此它们位于同一网络上。在谷歌搜索我的 IP 后,我将“localhost”替换为 IP。但是,问题仍然存在。任何专家都可以检查我的路径吗?我真的认为是因为路径或者我需要将参数打包到 JSON 对象中。谢谢。
    • 谷歌搜索将为您提供您的公共 IP - 您需要您的本地网络 IP。查看您的网络设置或运行 ifconfig (OSX/Linux) 或 ipconfig (Windows)
    猜你喜欢
    • 2016-02-14
    • 2012-06-07
    • 2016-05-11
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    • 2011-08-28
    • 2012-09-10
    • 2012-05-29
    相关资源
    最近更新 更多