doudang8824 2015-08-29 11:05
浏览 50

Java android httpURLConnection很奇怪

I'm new to Android programming and I'm trying to do a simple register-login system from a localhost database. The registration works fine but the when I'm trying to get the response from the login I get some weird xml\html kind of text instead of just the data.

this is the Java code:

 @Override
    protected User doInBackground(Void... params)
    {
        HashMap<String, Object> dataToSend = new HashMap<>();
        dataToSend.put("username", user.getUsername());
        dataToSend.put("password", user.getPassword());

        URL server;
        HttpURLConnection httpRequestParams;
        User returnedUser = null;
        String response = "";
        try {
            server = new URL(SERVER_ADDRESS + "FetchUserData.php");
            httpRequestParams  = (HttpURLConnection)server.openConnection();

            httpRequestParams.setReadTimeout(10000);
            httpRequestParams.setConnectTimeout(15000);
            httpRequestParams.setRequestMethod("POST");
            httpRequestParams.setDoInput(true);
            httpRequestParams.setDoOutput(true);
            httpRequestParams.connect();

            OutputStream os = httpRequestParams.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
            System.out.println(dataToSend);
            System.out.println(getPostDataString(dataToSend));
            writer.write(getPostDataString(dataToSend));
            writer.flush();
            writer.close();
            os.close();

            int responseCode = httpRequestParams.getResponseCode();
            System.out.println(responseCode);

            if (responseCode == HttpURLConnection.HTTP_OK)
            {
                String line;
                BufferedReader br = new BufferedReader(new InputStreamReader(httpRequestParams.getInputStream()));
                while ((line = br.readLine()) != null)
                {
                    response += line;
                    System.out.println(response);
                }
                br.close();
            } else
            {
                response = "";

                throw new Exception(responseCode + "");
            }

            System.out.println(response);
            JSONObject jObject = new JSONObject(response.toString());

            if(jObject.length() == 0)
            {
                returnedUser = null;
            }else
            {
                String name = jObject.getString("name");
                int age = jObject.getInt("age");

                returnedUser = new User(name, user.getUsername(), user.getPassword(), age);
            }

        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        } catch (ProtocolException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        } catch (Exception e1) {
            e1.printStackTrace();
        }

        return returnedUser;
    }

This is the php file:

<?php
$con= new mysqli("localhost", "root", "", "users");

if ($con->connect_error) 
{
    die("Connection failed: " . $con->connect_error);
} 

$username = $_POST["username"];
$password = $_POST["password"];

echo "HEY------------" . $username;

$query = "SELECT * FROM users WHERE username='?' AND password='?'";
$statement = $con->prepare($query);
$statement->bind_param("ss", $username, $password);

if ($statement->execute() === TRUE)
{
    echo "User selected successfully.";
} else 
{
    echo "Error: " . $query . "<br>" . $con->error;
}

mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $name, $username, $password, $age);

mysqli_stmt_fetch($statement);
$userDetails = array($name, $username, $password, $age);
mysqli_stmt_close($statement);

echo json_encode($userDetails);

$con->close();
?>

And this is the response I get:

<br /><font size='1'><table class='xdebug-error xe-notice xe-scream' dir='ltr' border='1' cellspacing='0' cellpadding='1'><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> SCREAM: Error suppression ignored for</th></tr><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: username in D:\Programs\WampServer\www\FetchUserData.php on line <i>9</i></th></tr><tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr><tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr><tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0004</td><td bgcolor='#eeeeec' align='right'>684616</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='D:\Programs\WampServer\www\FetchUserData.php' bgcolor='#eeeeec'>..\FetchUserData.php<b>:</b>0</td></tr></table></font><br /><font size='1'><table class='xdebug-error xe-notice xe-scream' dir='ltr' border='1' cellspacing='0' cellpadding='1'><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> SCREAM: Error suppression ignored for</th></tr><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: password in D:\Programs\WampServer\www\FetchUserData.php on line <i>10</i></th></tr><tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr><tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr><tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0004</td><td bgcolor='#eeeeec' align='right'>684616</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='D:\Programs\WampServer\www\FetchUserData.php' bgcolor='#eeeeec'>..\FetchUserData.php<b>:</b>0</td></tr></table></font>HEY------------<br /><font size='1'><table class='xdebug-error xe-warning xe-scream' dir='ltr' border='1' cellspacing='0' cellpadding='1'><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> SCREAM: Error suppression ignored for</th></tr><tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: mysqli_stmt::bind_param() [&lt;a href='mysqli-stmt.bind-param'&gt;mysqli-stmt.bind-param&lt;/a&gt;]: Number of variables doesn't match number of parameters in prepared statement in D:\Programs\WampServer\www\FetchUserData.php on line <i>16</i></th></tr><tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr><tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr><tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0004</td><td bgcolor='#eeeeec' align='right'>684616</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='D:\Programs\WampServer\www\FetchUserData.php' bgcolor='#eeeeec'>..\FetchUserData.php<b>:</b>0</td></tr><tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0111</td><td bgcolor='#eeeeec' align='right'>703168</td><td bgcolor='#eeeeec'>mysqli_stmt->bind_param(  )</td><td title='D:\Programs\WampServer\www\FetchUserData.php' bgcolor='#eeeeec'>..\FetchUserData.php<b>:</b>16</td></tr></table></font>User selected successfully.[null,null,null,null]

This is what I get when I use php file from the www folder:

User selected successfully.[user,username,pass,12]

I have no idea what's wrong... I don't get why the response is like this..

  • 写回答

2条回答 默认 最新

  • dthdlv9777 2015-08-29 11:24
    关注

    The error tells you that the PHP didn't get a username/password in the POST. Remove the httpRequestParams.connect(); - you're connecting too soon, before setting the parameters. The getResponseCode() will do the trick anyway.

    评论

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了