doudi4014 2016-04-09 16:27
浏览 133

从Android应用程序调用时获取请求返回404错误

I'm working on creating an app and I'm pretty new to doing this. I'm using Android studio. I've created a php script and hosted it on my hostinger account, along with the database it's accessing. If I enter the URL for the script on my browser it gives me a warning saying that it needs a parameter (not sure if there is a way for me to enter a parameter when I test it like this?) and shows the array which I want returned (with all null values of course, since it doesn't have the parameter for the SELECT statement). When I try to call the php script from my code with the parameter it returns an "Error 404 - Page Not Found" error. This is where I store it to the string

RequestHandler rh = new RequestHandler();
            String s = rh.sendGetRequestParam(Config.URL_GET_USER, username);

the URL it's referencing is definitely correct, as I can copy it straight from the code into the browser and it works. And I've printed the username string out on the line before to make sure it's correct and it is. Here's the relevant sections of code from the RequestHandler class

public String sendGetRequestParam(String requestURL, String username){
    StringBuilder sb =new StringBuilder();
    try {
        URL url = new URL(requestURL+username);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

        String s;
        while((s=bufferedReader.readLine())!=null){
            sb.append(s+"
");
        }
    }catch(Exception e){
    }
    return sb.toString();
}

And finally here is the PHP script

<?php



$username = $_GET['username'];

 require_once('dbConnect.php');

 $sql = "SELECT * FROM user_info WHERE username=$username";

 $r = mysqli_query($con,$sql);

 $result = array();
 $row = mysqli_fetch_array($r);
 array_push($result,array(
 "user_id"=>$row['id'],
 "username"=>$row['username'],
 "password"=>$row['password'],
 "dob"=>$row['dob'],
 "forename"=>$row['forename'],
 "surname"=>$row['surname']
 ));

 echo json_encode(array('result'=>$result));

 mysqli_close($con);

Let me know if you need any more information

EDIT: I've now added ?username= to the request so it's retrieving the array but the array is filled with null values. I think the error message I get when I try to access it through the browser is related to why this happens. It says: "Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in" then it has the path to the php file and says line 12, so that's this line:

$row = mysqli_fetch_array($r);
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求指导ADS低噪放设计
    • ¥15 CARSIM前车变道设置
    • ¥50 三种调度算法报错 有实例
    • ¥15 关于#python#的问题,请各位专家解答!
    • ¥200 询问:python实现大地主题正反算的程序设计,有偿
    • ¥15 smptlib使用465端口发送邮件失败
    • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
    • ¥15 对于squad数据集的基于bert模型的微调
    • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
    • ¥20 steam下载游戏占用内存