Java:如何检查php脚本输出的内容


Java: how to check the content of a php script output?

很抱歉这个愚蠢的问题,但是我对javanet的了解很糟糕。基本上在我的android应用程序调用许多php脚本从mysql数据库获取数据。这些数据以json格式返回,我使用谷歌json库来解析它们。一切都很好,但知道在每个php页面我必须添加一个测试。如果测试成功,那么脚本继续并返回json文件,但是如果测试失败,脚本返回字符串"false",或者值false(这取决于我),我的应用程序而不是显示数据必须将用户重定向到登录页面。

代码如下:

        URL url = new URL(Costanti.IP_SERVER+"myApps.php"+"?userId="+this.userId);
        try 
        {
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            int status = conn.getResponseCode();
            if (status >= 200 && status <= 299)
            {
                Reader r = new InputStreamReader(conn.getInputStream());
                Applicazioni dati = new Applicazioni();
                try 
                {
                    dati = gsonReader.fromJson(r, Applicazioni.class);
                    return dati;
                } 
                catch (Exception e) 
                {
                    System.out.println("Ho fallito a prendere i dati");
                    Log.e("JSON_Parser",e.toString());
                }
            }   
        } 
        catch (IOException e) 
        {
            System.out.println("Ho fallito la connnection");
            e.printStackTrace();
        }
    } 

所以基本上我使用这个谷歌库读取json文件在imputStreamReader和填充Applicazioni对象与我的数据。

我怎么能检查如果imputStreamReader的内容是字符串"false"或布尔值false,如果它是不同的解析它与json库????

在php的最后我做回声json_encode ($ applicazione);在一种情况下在另一种情况下回显"false"Tnx

InputStream in = new URL(url).openStream();
Scanner scanner = new Scanner(new InputStreamReader(in));
String result = scanner.useDelimiter("''Z").next(); // this reads the whole
                                                    // script output in a string
if(result.equals("false"))
 handle the false value...
else
  dati = gsonReader.fromJson(result, Applicazioni.class);

你可以json编码错误的结果也像["result"=>"false"]从PHP,这样你总是可以json解码在你的Java程序,然后寻找结果值

您可以将两种情况下的结果值放在输出中。