0

I've made A GCM Application.

I made web sever by Node.js and I checked my POST is working well by using Postman.

So, I am making an app that when button clicked, it posts so GCM Message comes to my device.

and this is my post code.

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(SERVER_URL+"/push");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        //nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        //nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.v("11111", "11111");
    }

I don't have any params with my POST so nameValueParams has nothing.

But in my log cat, it shows log.v("11111","11111");

there is an IOException, what is the problem? My manifest includes INTERNET PERMISSION ALSO.

Every example code has that structure but it goes to IOException.

halfer
  • 19,824
  • 17
  • 99
  • 186
LKM
  • 2,410
  • 9
  • 28
  • 53
  • replace the 2nd argument to your Log.v with `Log.getStackTraceString(e)` and update your question with stacktrace – ashoke Oct 01 '14 at 19:00
  • Thank you! By doing this, I got "org.apache.http.conn.HttpHostConnectException: Connection to http://127.0.0.1:52273 refused" error... – LKM Oct 01 '14 at 19:20
  • what is `SERVER_URL`, are you testing on emulator ? you dont want `127.0.0.1` which is localhost (android device itself). – ashoke Oct 01 '14 at 19:45
  • SERVER_URL is LOCALhost. I know what you think. But I found my IP at ipip.kr. but after changing my address, post doesn't work... what do you think? Colleagues said I need Official Server not local server... – LKM Oct 01 '14 at 20:16

1 Answers1

1

If you are using your development system as the server, you cannot use localhost as it will be pointing to your android device itself, not the server.

If you are testing this code on emulator, you can replace localhost with 10.0.2.2. As the emulator is running on your development system, it can talk to the server via emulator internal network

If you would like to test this on a phone device, and not on emulator, you either need to have a server accessible from internet, or have your phone and your server be on the same wifi or local subnet. You will also need to configure your server to allow network connections coming in from different networks, see this SO question.

Community
  • 1
  • 1
ashoke
  • 6,441
  • 2
  • 26
  • 25