0

so the NetBeans IDE can generate RESTful Web Service from database (the table has been packed into an entity class). I followed this tutorial and the RESTful Web Service has been generated successfully.

Now, I would like to call from my Android app, but no luck so far. I used "Android Asynchronous Http Client A Callback-Based Http Client Library for Android"

So here is my code snippet:

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();
                    }
                });

and this:

 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();
                }
            }
        });
    }

and here is NetBeans IDE generated POST method:

 @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);
    }

I added the @Path("/create") annotation and modified the method a bit.

Please shed some light, I am fairly new to this so I don't have a clue. I know it is due to some very silly mistakes but please help. The program stops at

"Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running]"

. So obvious I couldn't connect the two programs well.

Isley
  • 197
  • 15

1 Answers1

0

Your HTTP URL is "http://localhost:8080/..." i.e. it expects to reach a server running on the Android device. If your IDE/service is running on your workstation then:

  • If you're running the app on the Genymotion emulator use 10.0.3.2 instead of localhost
  • If you're running the app on the SDK emulator use 10.0.2.2 instead of localhost
  • If you're running the app on a real device then substitute the IP address of your workstation and make sure the device and workstation are on the same network.

References:

Community
  • 1
  • 1
tonys
  • 3,855
  • 33
  • 39
  • Hi, thank you so much for bring it up. I'm running the Android app on my mobile phone. And the web service is on my laptop (a project in NetBeans, right clicked it and hit "run"). They are both connected into a router so they are on the same network. After google search my IP, I replace "localhost" with the IP. However, the problem persists. Can any expert check my path? I really think it is because of the path or I need to pack the parameters into a JSON object. Thanks. – Isley May 14 '15 at 12:43
  • A google search will give you your public IP - you need your local network IP. Either look in your network settings or run ifconfig (OSX/Linux) or ipconfig (Windows) – tonys May 14 '15 at 12:54