dqwh2717 2017-03-20 08:49
浏览 45
已采纳

Android Async PHP

I want to send from my android application data to a php file and insert those data in my database. I made a Async task that I perfom in a button click but it does not work, no error nothing, and nothing is parsed to my database.

  public class Async extends AsyncTask {
    Integer name=1150;
    @Override
    protected Object doInBackground(Object [] params) {
        URL url = null;
        try {
            url = new URL("http://mywebsite.com/kundenexecute.php");
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            InputStream a = new BufferedInputStream(urlConnection.getInputStream());
            name = Integer.valueOf(convertStreamToString(a));
            urlConnection.disconnect();
        } catch (Exception e) {
            Log.d("abcd",e.toString());
        }

        return null;
    }

    @Override
    protected void onPostExecute(Object o) {
    }

    @Override
    protected void onPreExecute() {

    }

    private String convertStreamToString(InputStream is) {

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line).append('
');
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}

Button to send data

 public void senddata(View view) {
    new Async();
    Intent intent = new Intent(PDF.this, LoadingScreenActivity.class);
    startActivity(intent);
}

PHP File

<?php       
 include "db.inc.php";
 include "redirect.php";
 session_start();

 $vorname = $_GET['regid'];

 $conn = mysqli_connect($db_server, $db_user, $db_password, $db_name)  or die("you did not connect bozo");

 $statement = $conn->prepare("INSERT INTO kunden(kunden_plz) VALUES (?)");

 $statement->bind_param("s", $vorname);
 $statement->execute();   

 ?>
  • 写回答

1条回答 默认 最新

  • duanliang9288 2017-03-20 09:13
    关注

    Try to use generic asynctask. That's a pretty simple way to POST to PHP or create GET request. On the github page of the asynctask you can see some discription. Generic AsyncTask

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

报告相同问题?

悬赏问题

  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题