简体   繁体   English

推动Android

[英]Pushing at Android

I've made A GCM Application. 我已经提出了GCM申请。

I made web sever by Node.js and I checked my POST is working well by using Postman. 我用Node.js制作了网络服务器,并使用Postman检查了我的POST是否运行良好。

So, I am making an app that when button clicked, it posts so GCM Message comes to my device. 因此,我正在制作一个应用程序,当单击按钮时,它会发布,以便GCM消息进入我的设备。

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. 我的POST没有任何参数,因此nameValueParams没有任何内容。

But in my log cat, it shows log.v("11111","11111"); 但是在我的日志猫中,它显示log.v(“ 11111”,“ 11111”);

there is an IOException, what is the problem? 有一个IOException,是什么问题? My manifest includes INTERNET PERMISSION ALSO. 我的清单还包括INTERNET PERMISSION。

Every example code has that structure but it goes to IOException. 每个示例代码都具有该结构,但它属于IOException。

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. 如果您将开发系统用作服务器,则不能使用localhost因为它将指向您的android设备本身,而不是服务器。

If you are testing this code on emulator, you can replace localhost with 10.0.2.2 . 如果要在模拟器上测试此代码,则可以将localhost替换为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. 如果要在电话设备上而不是在模拟器上进行测试,则需要使服务器可以从Internet访问,或者电话和服务器位于同一wifi或本地子网中。 You will also need to configure your server to allow network connections coming in from different networks, see this SO question . 您还需要配置服务器以允许来自不同网络的网络连接,请参阅此SO问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM