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 逻辑谓词和消解原理的运用
  • ¥15 请求分析基于spring boot+vue的前后端分离的项目
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?