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 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决