dream890110
dream890110
2014-11-25 13:43
浏览 35

发送Json到php文件连接拒绝

I trie to send some json data with android to a server. The server is raspberry pi connected in my local WI-FI. Everytime I try to send the data I get a connection refused in my debug output. But I can't figure out why.

This is my android method:

public void sendJson(View view){
    String url = "http://192.168.0.5/control_center/functions/incomming.php";
    TextView textView_result;
    textView_result = (TextView) findViewById(R.id.textView_result);

    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("Content-Type" , "application/json");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("regid", "1234345893458435934kl34n543kl543öl53n4k5j43");
        String json = jsonObject.toString();

        StringEntity se = new StringEntity(json);

        httpPost.setEntity(se);
        HttpResponse httpResponse = httpClient.execute(httpPost);

        textView_result.setText(httpResponse.toString());

    }catch(JSONException e){
        e.printStackTrace();
    }catch(UnsupportedEncodingException e){
        e.printStackTrace();
    }catch(Exception e){
        e.printStackTrace();
    }
}

And this is the php file on my server:

<?php
include('db_connect.php');
$db = new _DB_Connect();
$db->connect();

if(isset($_POST['regid'])){
    $regid = $_POST['regid'];
    $save_entry = "insert into gcm_users (gcm_regid) values ('$regid')";
    mysql_query($save_entry) or die (mysql_error());
} else {
    echo 'no data';
}

?>

Internet permission on android is given.

<uses-permission android:name="android.permission.Internet" />
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douhanshu5517
    douhanshu5517 2014-11-25 16:31
    已采纳

    Ok, I solve this in my case. I know that the internet tasks should be in async task, but I solve this for testing purpose with a workaround:

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    

    The connection is fine for now. I did this, because it's at the moment more important for me, that I get my data pass to the server.

    No I got this output, but unfortunately no data in my database:

    ´ 11-25 17:27:02.745 31868-31868/com.development2014.cs.ajsontest3 D/libc﹕ [NET] getaddrinfo+,hn 26(0x6373646576656c),sn(),family 0,flags 4 11-25 17:27:02.745 31868-31868/com.development2014.cs.ajsontest3 D/libc﹕ [NET] getaddrinfo-,err=8 11-25 17:27:02.745 31868-31868/com.development2014.cs.ajsontest3 D/libc﹕ [NET] getaddrinfo+,hn 26(0x6373646576656c),sn(),family 0,flags 1024 11-25 17:27:02.745 31868-31868/com.development2014.cs.ajsontest3 D/libc﹕ [NET] getaddrinfo-, 1 11-25 17:27:02.745 31868-31868/com.development2014.cs.ajsontest3 D/libc﹕ [NET] getaddrinfo_proxy+ 11-25 17:27:02.815 31868-31868/com.development2014.cs.ajsontest3 D/libc﹕ [NET] getaddrinfo_proxy-, success 11-25 17:27:02.815 31868-31868/com.development2014.cs.ajsontest3 D/libc﹕ [NET] getaddrinfo+,hn 13(0x3138382e313030),sn(),family 0,flags 4 11-25 17:27:02.815 31868-31868/com.development2014.cs.ajsontest3 D/libc﹕ [NET] getaddrinfo-, SUCCESS 11-25 17:27:02.865 31868-31868/com.development2014.cs.ajsontest3 I/System.out﹕ org.apache.http.message.BasicHttpResponse@41f848d8 11-25 17:27:02.865 31868-31868/com.development2014.cs.ajsontest3 I/System.out﹕ {"regid":"blablabla"} ´

    点赞 评论
  • dreamfly0514
    dreamfly0514 2014-11-25 13:49

    There could be many reasons, but the most common are:

    • Firewall blocking access between the machines (also check local firewalls)

    • Port not open on the destination machine

    After checking for firewalls and that the port is open, use telnet to connect to the ip/port to test connectivity. This removes any potential issues from your application.

    点赞 评论

相关推荐