dongpankao6133 2016-04-10 12:22
浏览 103
已采纳

从android在线数据库中插入数据

i am trying to insert data from android to online database, my app does not give any error so that i can fix it, it worked fine when i connected my app to localhost and it was inserting data successfully but when i try to insert data to online database it doesn't work. can someone tell me what am doing wrong? before you have a look at my code i want you to look at my hosting information, in case you guys think am doing something wrong with this.

Main Domain securekid.base.pk
FTP hostname:   ftp.base.pk
FTP username:   basep_****
MySQL hostname: sql104.base.pk
MySQL username: basep_****
Hosting Volume  vol14_2

my db connection file:

<?php
$host = "sql104.base.pk";
$user = "basep_****";
$password = "******";
$db = "basep_*****_android";
$con = mysqli_connect($host, $user, $password, $db);
if($con)
{
    echo "successful";
}
else
{
    die("error" . mysqli_connect_error());
}

?>

register.php

<?php
require "db_connect.php";
$username =$_POST["username"];
$password =$_POST["password"];
$phone_no =$_POST["phone_no"];
$mysql_qry = "insert into user(username,password,phone_no) values('$username', '$password', '$phone_no')";

if($con->query($mysql_qry) === TRUE)
{
    echo  "Registration Successful";
}
else{
    echo "failed";
}
?>

this is background.java file

class backgroundTask extends AsyncTask<String,Void,String> {
    AlertDialog alertDialog;
    Context ctx;
   // Globals session_id = Globals.getInstance();
    // Globals session_id_child = Globals.getInstance();
    backgroundTask(Context ctx) {
        this.ctx = ctx;
    }

    String reg_url;

    @Override
    protected void onPreExecute() {
        reg_url = "http://securekid.base.pk/app/register.php";
    }

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

        String method = args[0];

        if (method.equals("register")) {
            String username = args[1];
            String password = args[2];
            String phone_no = args[3];
            try {
                URL url = new URL(reg_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream OS = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
                String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8") + "&" +
                        URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8") + "&" +
                        URLEncoder.encode("phone_no", "UTF-8") + "=" + URLEncoder.encode(phone_no, "UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                OS.close();
                InputStream IS = httpURLConnection.getInputStream();
                IS.close();
                //httpURLConnection.connect();
                httpURLConnection.disconnect();
                return "Registration Successful";
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
  • 写回答

1条回答 默认 最新

  • dtwr2012 2016-04-10 12:34
    关注

    It appears that in code you haven't posted you are checking if the browser supports JavaScript. An Android HttpURLConnection doesn't process JavaScript so your request will fail. The result you will get is this:

    <html>
        <body>
            <script type="text/javascript" src="/aes.js" ></script>
            <script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f
                <d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("b9a6c9d5cb703e2906de146c3bad1187");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; document.cookie="referrer="+escape(document.referrer); location.href="http://securekid.base.pk/app/register.php?ckattempt=1";
                </script>
                <noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript>
            </body>
        </html>
    

    I would suggest looking at the part of your site that generates that code and disabling it for your API files since an API client would never be expected to process JavaScript.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题