【问题标题】:unable to post data to database with httpurlconnection even though there is data in the java side即使 java 端有数据,也无法使用 httpurlconnection 将数据发布到数据库
【发布时间】:2016-04-20 10:43:01
【问题描述】:

这是我的 php 脚本。我一直在尝试使用 php 脚本将数据发布到 mysql 服务器。数据成功添加到注册函数中。我的 url 连接也没有问题。似乎是什么问题?

<?php
include ('classes/functions.php');
    if(isset($_POST['product_id'])){
        $product_id = $_POST['product_id'];

        $insert_order = "INSERT INTO orders (product_id) values  
 ('$product_id')";

        $run_order = mysqli_query($con, $insert_order);

        if($run_order){
            echo"<script>alert('Order Successfully!')</script>";
        }
    }
 ?>

这是我的 java 脚本。

 public final class confirm extends Context {

private static final String REGISTER_URL = 
"http://192.168.43.214/apexStore2/confirm.php";

public static void register(String id) {
    class RegisterUser extends AsyncTask<String, Void, String> {
         ProgressDialog loading;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
        }
  @Override
        protected String doInBackground(String... params) {

            HashMap<String, String> data = new HashMap<>();
            data.put("product_id",params[0]);

            String result = sendPost(REGISTER_URL,data);

            return  result;
        }
    }

    RegisterUser ru = new RegisterUser();
    ru.execute(id);
}
public static String sendPost(String requestURL,
                              HashMap<String, String> postDataParams) {

    URL url;
    String response = " ";
    try {
        url = new URL(requestURL);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded");
        conn.setRequestProperty("Content-Length", "" +
                    Integer.toString(getPostDataString(postDataParams).
    getBytes().length));
        conn.setRequestProperty("Content-Language", "en-US");
        conn.setReadTimeout(15000);
        conn.setConnectTimeout(15000);
        conn.setDoInput(true);
        conn.setDoOutput(true);


        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, "UTF-8"));
        writer.write(getPostDataString(postDataParams));
        System.out.println("wei" + getPostDataString(postDataParams));
        Log.d(getPostDataString(postDataParams), "onCreate()");
        writer.flush();
        writer.close();
        os.close();
        int responseCode=conn.getResponseCode();

        if (responseCode == HttpsURLConnection.HTTP_OK) {
            BufferedReader br=new BufferedReader(new  
    InputStreamReader(conn.getInputStream()));
            response = br.readLine();
        }
        else {
            response="Error Registering";
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return response;
}

private static String getPostDataString(HashMap<String, String> params) 
throws UnsupportedEncodingException {
    StringBuilder result = new StringBuilder();
    boolean first = true;
    for(Map.Entry<String, String> entry : params.entrySet()){
        if (first)
            first = false;
        else
            result.append("&");

        result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
    }

    return result.toString();
}

【问题讨论】:

    标签: java php android mysql


    【解决方案1】:

    您是在模拟器上还是在设备上运行它?无论哪种方式,我认为确认设备在正确的网络上以查看您的内部 IP 192.168.43.214 会很有帮助,您可能需要使用您的外部 IP,尤其是当您通过数据连接进行连接时。除此之外,android日志的输出是什么?

    【讨论】:

    • 我正在使用手机。此ip经过测试没有问题,因为有其他功能使用此ip并且可以正常工作。
    • android 日志只显示我传递的值。但它没有显示成功
    • 你能发布输出吗?服务器是否从客户端获得任何连接,或者什么都没有?我没有发现您的代码有任何不当之处,但是如果没有有关输出的更多信息或有关行为的更多信息,则很难进行调试。
    猜你喜欢
    • 1970-01-01
    • 2016-08-08
    • 2020-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-04
    • 2023-02-16
    相关资源
    最近更新 更多