【问题标题】:JSON from PHP file returns garbagePHP文件中的JSON返回垃圾
【发布时间】:2018-10-19 08:29:36
【问题描述】:

我正在尝试从我的站点 example.net/index.php 获取 JSON 文件,但是我总是在 Android 应用程序中获取某种 HTML 而不是 JSON 对象。

返回字符串:

<html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("674bdf26f9a5fe3df1461aafc3120641");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; location.href="http://example.net/index.php?i=1";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>

调用网址:

<?php
  $results = array(
      "result"   => "success",
      "username" => "some username",
      "projects" => "some other value"
  );
  header('Content-type: application/json');
  echo json_encode($results);
?>

代码:

public String getJSON(String url, int timeout) {
        HttpURLConnection c = null;
        try {
            URL u = new URL(url);
            c = (HttpURLConnection) u.openConnection();
            c.setRequestMethod("GET");
            c.setRequestProperty("Content-length", "0");
            c.setUseCaches(false);
            c.setAllowUserInteraction(false);
            c.setConnectTimeout(timeout);
            c.setReadTimeout(timeout);
            c.connect();
            int status = c.getResponseCode();

            switch (status) {
                case 200:
                case 201:
                    BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
                    StringBuilder sb = new StringBuilder();
                    String line;
                    while ((line = br.readLine()) != null) {
                        sb.append(line+"\n");
                    }
                    br.close();
                    return sb.toString();
            }

        } catch (MalformedURLException ex) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
        } finally {
            if (c != null) {
                try {
                    c.disconnect();
                } catch (Exception ex) {
                    Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return null;
    }

如何获取 JSON 文件而不是此 HTML 代码?

【问题讨论】:

    标签: java php android json


    【解决方案1】:

    你应该传递内容类型,

            c = (HttpURLConnection) u.openConnection();
            c.setRequestMethod("GET");
            c.setRequestProperty("Content-length", "0");
            c.setRequestProperty("Content-type", "application/json; charset=utf-8");
            c.setUseCaches(false);
    

    【讨论】:

    • 谢谢,我试过了,但它仍然返回相同的而不是 JSON。
    • 不,但我尝试了Volley 库并得到了相同的结果。
    • 请尝试使用邮递员应用程序以确保响应是否具有 JSON 格式或使用结果更新问题。另外,尝试在android中使用retrofit lib进行API调用。
    【解决方案2】:

    看看 html 源代码试图告诉你什么:

    此网站需要 Javascript 才能运行,

    您应该首先在该页面上使用 javascript 解释器来获取 json。

    所以错误的网址。它不会给你 json。

    如果您使用浏览器请求该页面,浏览器将在您不知情的情况下调用 javascript 解释器。

    【讨论】:

      猜你喜欢
      • 2015-06-27
      • 2011-06-23
      • 1970-01-01
      • 2013-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-15
      • 2022-01-10
      相关资源
      最近更新 更多