doudou8783 2015-06-22 20:20
浏览 61
已采纳

Java GET请求PHP URL未发送

I am trying to get my Java program to send a GET request to the following (pseudo) PHP file:

www.domain.com/example.php

Here is the PHP script:

<?php

include "dbcredentials.php";

$query = "INSERT INTO accesscode (id) VALUES ('" . $_GET['auth'] . "')";

$result = mysql_query($query) or die(mysql_error());  // Check if query was successful

print ($_GET['auth']);

?>

The above php is fairly self explanatory. When a GET request is received, it prints the content of the request to the page and also adds it in the column "ID" in table named "accesscode".

Therefore if typing "www.domain.com/example.php?auth=Brad+Pitt", the PHP file will print Brad Pitt and add that to the table "accesscode".

Okay, simple enough. Except I want my Java program to be able to send a GET request too. So I devised the following function which is called by new phpGET().execute():

public class phpGET extends AsyncTask<Void, Void, Void> {
    protected Void doInBackground(Void... params) {

    try {

        URL url = new URL("http://domain.com/example.php?auth=David+Cameron");
        URLConnection connection = url.openConnection();
        connection.connect();

    } catch (Exception e) {

        Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();

    }

    return null;
}

}

Now just to confirm, the above does not catch an error. But for some reason, it must fail to send the GET request (http://domain.com/example.php?auth=David+Cameron) to the php file, on the basis that David Cameron is not added to the database after the function is called, as is intended.

I confirm I have the following Android manifest permissions:

<uses-permission android:name="android.permission.INTERNET" />

I'm pretty stumped on this one; any ideas?

Additional information: I am using Windows 7 x86, the latest Android Studio and testing on a Samsung Galaxy S5.

Also please note, I am aware, and it has been brought to my attention, that essentially I should be using PDO for database access, and that I should be taking additional action to prevent MySQL injection vulnerabilities that the code is riddled with. I would like to assure you that I intend on doing so upon solving the central problem discussed herein.

  • 写回答

2条回答 默认 最新

  • duanji5116 2015-06-22 20:58
    关注

    Okay,

    So I'm not exactly too sure how good of a solution this is, but irrespective, it does exactly what I set out to achieve. I was able to successfully send a PHP GET request to the .php file using the following:

    public class phpGET extends AsyncTask<Void, Void, Void> {
            protected Void doInBackground(Void... params) {
    
                try {
    
                    URL url = new URL("http://www.domain.com/example.php?auth=David+Cameron");
                    URLConnection conn = url.openConnection();
                    InputStream is = conn.getInputStream();
                    is.close();
    
                } catch (Exception e) {
    
                    Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_LONG).show();
    
                }
    
                return null;
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决