0

I am new to RESTful Web Service. Just wrote a very simple Android client (tested on phone) to request GET an online web services API from this link. Structuring online documentation for a REST API But it keeps returning cannot connect to the remote server. Really frustrating. Can any expert identify the problem here? Thank you very much.

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final TextView message = (TextView) findViewById(R.id.message);

        AsyncHttpClient client = new AsyncHttpClient();
        client.get("http://www.thomas-bayer.com/sqlrest/CUSTOMER/3/", new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                message.setText(bytes.toString() + "/" + "Successfully connected to remote server.");
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                message.setText("Cannot connect to the remote server.");

            }
        });
    }

I was trying to connect to the web service generated by NetBeans Jersey from database(entities). My phone was connected to my laptop (where NetBeans web service was running, already check the status by opening glassfish portal http://localhost:4848, and I set the IP to 192.168.x.x something, no luck). I thought should find an easier, tested path such as this one. Still cannot connect to the remote server. Geez! I am using a indie made android http client from this guy http://loopj.com/android-async-http/

Community
  • 1
  • 1
Isley
  • 197
  • 15

1 Answers1

1

I believe you have not see your Logcat properly. I tried your code and I found:

05-17 04:23:26.499: E/AsyncHttpRequest(5263): java.lang.SecurityException: Permission denied (missing INTERNET permission?)

05-17 04:23:26.499: E/AsyncHttpRequest(5263): Caused by: libcore.io.ErrnoException: getaddrinfo failed: EACCES (Permission denied)

Add <uses-permission android:name="android.permission.INTERNET" /> to your Manifest file and you should be all good :) Let me know if it helps!

Mithun
  • 2,075
  • 3
  • 19
  • 26
  • Hi, thank you a million times. That solved my problem. I knew it was due to some silly mistake. I'm still a newbie to debugging. When I do coding on Android Studio, the logcat scrolls like a flood, so I filtered it with "FATAL EXCEPTION". Can I ask some tip how to find out the few lines of crucial information out of hundreds? Thanks. – Isley May 17 '15 at 02:04
  • Pleasure :) ... For logging, follow these 2 links, http://stackoverflow.com/questions/19931987/how-to-filter-logcat-in-android-studio and http://stackoverflow.com/questions/27618977/android-studio-maximum-number-of-lines-logcat. My personal favorite is to dump logs into a file in your system, `PATH\TO\YOUR\ADB\ adb.exe logcat > log.txt`. You can read more about how to use switches with `adb logcat` to make proper filter. Hope this helps! – Mithun May 17 '15 at 08:40