android.system.ErrnoException:连接失败:ECONNREFUSED(拒绝连接)

时间:2016-02-28 09:43:02

标签: php android post networking wamp

我正在尝试使用php将数据从我的应用程序发送到本地服务器(wamp),但最终导致连接失败:ECONNREFUSED(连接被拒绝)。不确定是否正确编写了android的POST部分或者是否正确指定了URL。

这是网址

private String url_create_product = "https://192.168.1.103/android_connect1/create_product.php";

android代码

private void createPost(String url) {
    String name = inputName.getText().toString();
    String price = inputPrice.getText().toString();
    String description = inputDesc.getText().toString();
    DataOutputStream output;

    try {
        URL url1 = new URL(url);
        HttpsURLConnection connection = (HttpsURLConnection) url1.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setChunkedStreamingMode(0);


        HashMap<String,String> map = new HashMap<String,String>();
        map.put("name", name);
        map.put("price", price);
        map.put("description", description);

        OutputStream os = connection.getOutputStream();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
        writer.write(String.valueOf(map));

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

PHP

<?php
$response = array();
if (isset($_POST['name']) && isset($_POST['price']) && isset($_POST['description'])) {

    $name = $_POST['name'];
   $price = $_POST['price'];
    $description = $_POST['description'];

    include 'db_connect.php';

    $result = mysqli_query($conn,"INSERT INTO products(name, price, description) VALUES('$name', '$price', '$description')");


    if ($result) {
        $response["success"] = 1;
        $response["message"] = "Product successfully created.";

        echo json_encode($response);
    } else {
        $response["success"] = 0;
        $response["message"] = "Oops! An error occurred.";
        echo json_encode($response);
    }
} else {
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";
    echo json_encode($response);
}
?>

logcat的

java.net.ConnectException: failed to connect to /192.168.1.103 (port 443): connect failed: ECONNREFUSED (Connection refused)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:276)
 at akshay.com.androidserverproject.NewProductActivity.createPost(NewProductActivity.java:135)
at akshay.com.androidserverproject.NewProductActivity.access$100(NewProductActivity.java:33)
at akshay.com.androidserverproject.NewProductActivity$CreateNewProduct.doInBackground(NewProductActivity.java:87)
at akshay.com.androidserverproject.NewProductActivity$CreateNewProduct.doInBackground(NewProductActivity.java:69)
Caused by: android.system.ErrnoException: connect failed: ECONNREFUSED (Connection refused)

0 个答案:

没有答案