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 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"} ´

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题