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 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持