PHP URL正在研究broswer,但当我试图从我的Android应用程序运行它时,InputStream为null

时间:2016-01-18 09:53:42

标签: java php android

我是android新手,想从Android应用程序调用PHP编写的Web服务;但我得InputStream为空。但是,当我尝试从浏览器运行相同的URL时,它显示输出。 我的php文件:(userlogin.php)

<?php
$con=mysqli_connect("IP Address of server","Any","","user");

if (mysqli_connect_errno($con))
{
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$username = $_GET['username'];
$password = $_GET['password'];

$result = mysqli_query($con,"SELECT * FROM userLogin where username = '$username' and password = '$password'");


$n_filas = mysqli_num_rows ($result);

$array = array();
    for ($i=0; $i<$n_filas; $i++)
    {
        $fila = mysqli_fetch_array($result);
    $array[$i]['username'] = utf8_encode($fila['username']);
        $array[$i]['password'] = utf8_encode($fila['password']);
    }
    $result= json_encode($array);
    echo $result;


mysqli_close($con);
?>

和我的JSON解析器:

public JSONArray makeHttpRequest(String url, ArrayList<String> params) {

        try
        {
            URL url1 = new URL("http://IP ADDRESS OF SERVER/userlogin.php?username="+params.get(0)+"&password="+params.get(1));
            urlConnection = (HttpURLConnection) url1.openConnection();
            urlConnection.connect();
            is = urlConnection.getInputStream();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        try {
            Log.d("String is",is.toString());
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();

            Log.d("String ===",json);
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try
        {
            Log.d("In JSON Object try ", "yes");
            jObj = new JSONArray(json);
            Log.d("String Json ===",jObj.toString());
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;
        }

任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:0)

以下适用于我:

public static void retrieveData() {

    try {
        // the following line is BAD - do not supply username + password via such an insecure connection!       
        URL url1 = new URL("http://IP ADDRESS OF SERVER/userlogin.php?username="+params.get(0)+"&password="+params.get(1));

        URLConnection urlConnection = url1.openConnection();
        // you may want to adjust the timeout here, like so:
        // urlConnection.setConnectTimeout(timeout);
        urlConnection.connect();
        try (InputStream is = urlConnection.getInputStream()) {
            processData(is);
        } catch (IOException e) {
            // do something smart
        }
    } catch (MalformedURLException e) {
        // do something smart
    } catch (IOException e) {
        // do something smart
    } 

}

private static void processData(InputStream is) {
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(is))) {       
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }

    // rest of your processing logic

    } catch (IOException e) {
        // do something smart
    } 
}

这些方法不一定必须是静态,您当然可以修改其签名(参数,返回值)以满足您的需求。