通过Android将数据插入MySQL

时间:2019-01-26 15:43:18

标签: java android

我在名为“ insert.php”的文件中制作php代码,此页面具有通过POST方法将数据插入我的MySQL数据库的功能。我手动检查,它的工作原理。 在我的android studio中没有插入数据。我调试和搜索,但我不知道问题出在哪里! 这是php文件

<?php
    require_once '../includes/DbOperation.php';

$db = new DbOperation();

    $result = $db->createcontact(
        $_POST['firstName'],
        $_POST['lastName'],
        $_POST['dispalyName'],
        $_POST['mobileNumber'],
        $_POST['homeNumber'],
        $_POST['hasExtraInfo'],
        $_POST['hasExtraNumber'],
        $_POST['userId']
    );

    //if the record is created adding success to response
    if($result){
        //record is created means there is no error
        $response['error'] = false; 
        //in message we have a success message
        $response['message'] = 'Contact added successfully';

    }else{
        //if record is not added that means there is an error 
        $response['error'] = true; 
        //and we have the error message
        $response['message'] = 'Some error occurred please try again';
    }
    echo $response['message'];
?>

这是Java代码:

//.....
private final String ROOT_URL = "http://10.0.2.2/android/HeroApi/v1/";
public static final String URL_CREATE_CONTACT = "insert.php";
 if (checkNetwork()) {
            new AddContactAsyncTask().execute();
}
//.....
 private class AddContactAsyncTask extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... strings) {

            String reg_url = ROOT_URL + URL_CREATE_CONTACT;
            String encode="UTF-8";
            try {

                String data = URLEncoder.encode(KEY_FIRST_NAME, encode) + "="
                        + URLEncoder.encode(contact.getGivenName(), encode) + "&"
                        + URLEncoder.encode(KEY_LAST_NAME, encode)
                        + "=" + URLEncoder.encode(contact.getFamilyName(), encode) + "&"
                        + URLEncoder.encode(KEY_DISPLAY_NAME, encode)
                        + "=" + URLEncoder.encode(contact.getDisplayName(), encode) + "&"
                        + URLEncoder.encode(KEY_MOBILE_NUMBER, encode)
                        + "=" + URLEncoder.encode(contact.getFirstPhone(), encode) + "&"
                        + URLEncoder.encode(KEY_HAS_EXTRA_INFO, encode)
                        + "=" + URLEncoder.encode("0", encode) + "&"
                        + URLEncoder.encode(KEY_HAS_EXTRA_NUMBER, encode)
                        + "=" + URLEncoder.encode("0", encode) + "&"
                        + URLEncoder.encode(KEY_USER_ID, encode)
                        + "=" + URLEncoder.encode("0", encode) + "&"
                        + URLEncoder.encode(KEY_HOME_NUMBER, encode)
                        + "=" + URLEncoder.encode("", encode) ;
                URL url = new URL(reg_url);
                HttpURLConnection httpURLConnection =
                        (HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                OutputStreamWriter wr = new OutputStreamWriter(httpURLConnection.getOutputStream());
                wr.write( data );
                BufferedReader reader = new BufferedReader(new
                        InputStreamReader(httpURLConnection.getInputStream()));
                StringBuilder sb = new StringBuilder();
                String line = "";
                // Read Server Response
                while((line = reader.readLine()) != null) {
                    sb.append(line);
                    break;
                }
                return sb.toString();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ProtocolException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

}

没有错误或警告,但代码不起作用。

0 个答案:

没有答案