无法通过我的Android应用访问我的Google电子表格

时间:2015-08-20 13:37:43

标签: android json google-sheets

大家好!

我正在编写一个从Google电子表格(发布到网络)下载数据的Android应用程序,其中我已插入3行进行测试。我使用json格式的url作为电子表格的链接,以便我直接以json格式下载电子表格。当我在浏览器中输入网址时,我得到了json格式的电子表格:

    /*O_o*/
google.visualization.Query.setResponse({"version":"0.6","reqId":"0","status":"ok","sig":"270757910","table":{"cols":[{"id":"A","label":"ID\n","type":"number","pattern":"General"},{"id":"B","label":"Title","type":"string"},{"id":"C","label":"Description","type":"string"},{"id":"D","label":"Picture","type":"string"},{"id":"E","label":"Date","type":"string"},{"id":"F","label":"Time","type":"string"},{"id":"G","label":"Author","type":"string"},{"id":"H","label":"Category","type":"string"}],"rows":[{"c":[{"v":1.0,"f":"1"},{"v":"title1"},{"v":"description1"},{"v":"picture1"},{"v":"date1"},{"v":"time1"},{"v":"author1"},{"v":"category1"}]},{"c":[{"v":2.0,"f":"2"},{"v":"title2"},{"v":"description2"},{"v":"picture2"},{"v":"date2"},{"v":"time2"},{"v":"author2"},{"v":"category2"}]},{"c":[{"v":3.0,"f":"3"},{"v":"title3"},{"v":"description3"},{"v":"picture3"},{"v":"date3"},{"v":"time3"},{"v":"author3"},{"v":"category3"}]}]}});

但是当我尝试使用此网址通过应用程序下载并记录结果时,我得到了这个:

08-20 15:45:51.706: D/test(14095):   <meta name="description" content="Create a new spreadsheet and edit with others at the same time -- from your computer, phone or tablet. Get stuff done with or without an internet connection. Use Sheets to edit Excel files. Free from Google.">
08-20 15:45:51.706: D/test(14095):   <meta name="google-site-verification" content="LrdTUW9psUAMbh4Ia074-BPEVmcpBxF6Gwf0MSgQXZs">
08-20 15:45:51.706: D/test(14095):   <title>Google Sheets - create and edit spreadsheets online, for free.</title>
08-20 15:45:51.706: D/test(14095):   <style>
08-20 15:45:51.706: D/test(14095):   @font-face {
08-20 15:45:51.706: D/test(14095):   font-family: 'Open Sans';
08-20 15:45:51.706: D/test(14095):   font-style: normal;
08-20 15:45:51.706: D/test(14095):   font-weight: 300;
08-20 15:45:51.706: D/test(14095):   src: url(//fonts.gstatic.com/s/opensans/v13/DXI1ORHCpsQm3Vp6mXoaTZS3E-kSBmtLoNJPDtbj2Pk.ttf) format('truetype');
08-20 15:45:51.706: D/test(14095): }
08-20 15:45:51.706: D/test(14095): @font-face {
08-20 15:45:51.706: D/test(14095):   font-family: 'Open Sans';
08-20 15:45:51.706: D/test(14095):   font-style: normal;
08-20 15:45:51.706: D/test(14095):   font-weight: 400;
08-20 15:45:51.706: D/test(14095):   src: url(//fonts.gstatic.com/s/opensans/v13/cJZKeOuBrn4kERxqtaUH3SZ2oysoEQEeKwjgmXLRnTc.ttf) format('truetype');
08-20 15:45:51.706: D/test(14095): }
08-20 15:45:51.706: D/test(14095):   </style>
08-20 15:45:51.706: D/test(14095):   <style>
08-20 15:45:51.706: D/test(14095):   h1, h2 {
08-20 15:45:51.706: D/test(14095):   -webkit-animation-duration: 0.1s;
08-20 15:45:51.706: D/test(14095):   -webkit-animation-name: fontfix;
08-20 15:45:51.706: D/test(14095):   -webkit-animation-iteration-count: 1;
08-20 15:45:51.706: D/test(14095):   -webkit-animation-timing-function: linear;
08-20 15:45:51.706: D/test(14095):   -webkit-animation-delay: 0;
08-20 15:45:51.706: D/test(14095):   }
08-20 15:45:51.706: D/test(14095):   @-webkit-keyframes fontfix {
08-20 15:45:51.706: D/test(14095):   from {
08-20 15:45:51.706: D/test(14095):   opacity: 1;
08-20 15:45:51.706: D/test(14095):   }
08-20 15:45:51.706: D/test(14095):   to {
08-20 15:45:51.706: D/test(14095):   opacity: 1;
08-20 15:45:51.706: D/test(14095):   }
08-20 15:45:51.706: D/test(14095):   }
08-20 15:45:51.706: D/test(14095):   </style>
08-20 15:45:51.706: D/test(14095): <style>
08-20 15:45:51.706: D/test(14095):   html, body {
08-20 15:45:51.706: D/test(14095):   font-family: Arial, sans-serif;
08-20 15:45:51.706: D/test(14095):   background: #fff;
08-20 15:45:51.706: D/test(14095):   margin: 0;
08-20 15:45:51.706: D/test(14095):   padding: 0;
08-20 15:45:51.706: D/test(14095):   border: 0;
08-20 15:45:51.706: D/test(14095):   position: absolute;
08-20 15:45:51.706: D/test(14095):   height: 100%;
08-20 15:45:51.706: D/test(14095):   min-width: 100%;
08-20 15:45:51.706: D/test(14095):   font-size: 13px;
08-20 15:45:51.706: D/test(14095):   color: #404040;
08-20 15:45:51.706: D/test(14095):   direction: ltr;
08-20 15:45:51.706: D/test(14095):   -webkit-text-size-adjust: none;
08-20 15:45:51.706: D/test(14095):   }
08-20 15:45:51.706: D/test(14095):   button,
08-20 15:45:51.706: D/test(14095):   input[type=button],
08-20 15:45:51.706: D/test(14095):   input[type=submit] {
08-20 15:45:51.706: D/test(14095):   font-family: Arial, sans-serif;
08-20 15:45:51.706: D/test(14095):   font-size: 13px;
08-20 15:45:51.706: D/test(14095):   }
08-20 15:45:51.706: D/test(14095):   a,
08-20 15:45:51.706: D/test(14095):   a:hover,
08-20 15:45:51.706: D/test(14095):   a:visited {
08-20 15:45:51.706: D/test(14095):   color: #427fed;
08-20 15:45:51.706: D/test(14095):   cursor: pointer;
08-20 15:45:51.706: D/test(14095):   text-decoration: none;
08-20 15:45:51.706: D/test(14095):   }
08-20 15:45:51.706: D/test(14095):   a:hover {
08-20 15:45:51.706: D/test(14095):   text-decoration: underline;
08-20 15:45:51.706: D/test(14095):   }
08-20 15:45:51.706: D/test(14095):   h1 {
08-20 15:45:51.706: D/test(14095):   font-size: 20px;
08-20 15:45:51.706: D/test(14095):   color: #262626;
08-20 15:45:51.706: D/test(14095):   margin: 0 0 15px;
08-20 15:45:51.706: D/test(14095):   font-weight: normal;
08-20 15:45:51.706: D/test(14095):   }
08-20 15:45:51.706: D/test(14095):   h2 {
08-20 15:45:51.706: D/test(14095):   font-size: 14px;
08-20 15:45:51.706: D/test(14095):   color: #262626;
08-20 15:45:51.706: D/test(14095):   margin: 0 0 15px;
08-20 15:45:51.706: D/test(14095):   font-weight: bold;
08-20 15:45:51.706: D/test(14095):   }
08-20 15:45:51.706: D/test(14095):   input[type=email],
08-20 15:45:51.706: D/test(14095):   input[type=number],
08-20 15:45:51.706: D/test(14095):   input[type=password],
08-20 15:45:51.706: D/test(14095):   input[type=tel],
08-20 15:45:51.706: D/test(14095):   input[type=text],
08-20 15:45:51.706: D/test(14095):   input[type=url] {
08-20 15:45:51.706: D/test(14095):   -moz-appearance: none;
08-20 15:45:51.706: D/test(14095):   -webkit-appearance: none;
08-20 15:45:51.706: D/test(14095):   appearance: none;
08-20 15:45:51.706: D/test(14095):   display: inline-block;
08-20 15:45:51.706: D/test(14095):   height: 36px;
08-20 15:45:51.706: D/test(14095):   padding: 0 8px;
08-20 15:45:51.706: D/test(14095):   margin: 0;
08-20 15:45:51.706: D/test(14095):   background: #fff;
08-20 15:45:51.706: D/test(14095):   border: 1px solid #d9d9d9;
08-20 15:45:51.706: D/test(14095):   border-top: 1px solid #c0c0c0;
08-20 15:45:51.706: D/test(14095):   -moz-box-sizing: border-box;
08-20 15:45:51.706: D/test(14095):   -webkit-box-sizing: border-box;
08-20 15:45:51.706: D/test(14095):   box-sizing: border-box;
08-20 15:45:51.706: D/test(14095):   -moz-border-radius: 1px;
08-20 15:45:51.706: D/test(14095):   -webkit-border-radius: 1px;
08-20 15:45:51.706: D/test(14095):   border-radius: 1px;
08-20 15:45:51.706: D/test(14095):   font-size: 15px;
08-20 15:45:51.706: D/test(14095):   color: #404040;
08-20 15:45:51.706: D/test(14095):   }
08-20 15:45:51.706: D/test(14095):   input[type=email]:hover,
08-20 15:45:51.706: D/test(14095):   input[type=number]:hover,
08-20 15:45:51.706: D/test(14095):   input[type=password]:hover,
08-20 15:45:51.706: D/test(14095):   input[type=tel]:hover,
08-20 15:45:51.706: D/test(14095):   input[type=text]:hover,
08-20 15:45:51.706: D/test(14095):   input[type=url]:hover {
08-20 15:45:51.706: D/test(14095):   border: 1px solid #b9b9b9;
08-20 15:45:51.706: D/test(14095):   border-top: 1px solid #a0a0a0;
08-20 15:45:51.706: D/test(14095):   -moz-box-shadow: inset 0 1px 2px rgba(0,0,0,0.1);
08-20 15:45:51.706: D/test(14095):   -webkit-box-shadow: inset 0 1px 2px rgba(0,0,0,0.1);
08-20 15:45:51.706: D/test(14095):   box-shadow: inset 0 1px 2px rgba(0,0,0,0.1);
08-20 15:45:51.706: D/test(14095):   }
08-20 15:45:51.706: D/test(14095):   input[type=email]:focus,
08-20 15:45:51.706: D/test(14095):   input[type=number]:focus,
08-20 15:45:51.706: D/test(14095):   input[type=password]:focus,
08-20 15:45:51.706: D/test(14095):   input[type=tel]:focus,
08-20 15:45:51.706: D/test(14095):   input[type=text]:focus,
08-20 15:45:51.706: D/test(14095):   input[type=url]:focus {
08-20 15:45:51.706: D/test(14095):   outline: none;
08-20 15:45:51.706: D/test(14095):   border: 1px solid #4d90fe;
08-20 15:45:51.706: D/test(14095):   -moz-box-shadow: inset 0 1px 2px rgba(0,0,0,0.3);
08-20 15:45:51.706: D/test(14095):   -webkit-box-shadow: inset 0 1px 2px rgba(0,0,0,0.3);
08-20 15:45:51.706: D/test(14095):   box-shadow: inset 0 1px 2px rgba(0,0,0,0.3);
08-20 15:45:51.706: D/test(14095):   }
08-20 15:45:51.706: D/test(14095):   input[type=checkbox],
08-20 15:45:51.706: D/test(14095):   input[type=radio] {
08-20 15:45:51.706: D/test(14095):   -webkit-appearance: none;
08-20 15:45:51.706: D/test(14095):   display: inline-block;
08-20 15:45:51.706: D/test(14095):   width: 13px;
08-20 15:45:51.706: D/test(14095):   height: 13px;
08-20 15:45:51.706: D/test(14095):   margin: 0;
08-20 15:45:51.706: D/test(14095):   cursor: pointer;
08-20 15:45:51.706: D/test(14095):   vertical-align: bottom;
08-20 15:45:51.706: D/test(14095):   background: #fff;
08-20 15:45:51.706: D/test(14095):   border: 1px solid #c6c6c6;
08-20 15:45:51.706: D/test(14095):   -moz-border-radius: 1px;
08-20 15:45:51.706: D/test(14095):   -webkit-border-radius: 1px;
08-20 15:45:51.706: D/test(14095):   border-radius: 1px;
08-20 15:45:51.706: D/test(14095):   -moz-box-sizing: border-box;
08-20 15:45:51.706: D/test(14095):   -webkit-box-sizing: border-box;
08-20 15:45:51.706: D/test(14095):   box-sizing: border-box;
08-20 15:45:51.706: D/test(14095):   position: relative;
08-20 15:45:51.706: D/test(14095):   }
08-20 15:45:51.706: D/test(14095):   input[type=checkbox]:active,
08-20 15:45:51.706: D/test(14095):   input[type=radio]:active {
08-20 15:45:51.706: D/test(14095):   background: #ebebeb;
08-20 15:45:51.706: D/test(14095):   }
08-20 15:45:51.706: D/test(14095):   input[type=checkbox]:hover {
08-20 15:45:51.706: D/test(14095):   border-color: #c6c6c6;
08-20 15:45:51.706: D/test(14095):   -moz-box-shadow: inset 0 1px 2px rgba(0,0,0,0.1);
08-20 15:45:51.706: D/test(14095):   -webkit-box-shadow: inset 0 1px 2px rgba(0,0,0,0.1);
08-20 15:45:51.706: D/test(14095):   box-shadow: inset 0 1px 2px rgba(0,0,0,0.1);
08-20 15:45:51.706: D/test(14095):   }
08-20 15:45:51.706: D/test(14095):   input[type=radio] {
08-20 15:45:51.706: D/test(14095):   -moz-bo
08-20 15:45:51.708: D/test(14095): {"font-family":"Open Sans","src":"url(","font-weight":400,"font-style":"normal"}

以下是我下载电子表格的代码:

public class DownloadWebpageTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... urls) {

        try {
            return downloadUrl(urls[0]);
        } catch (IOException e) {
            return "Unable to download the requested page.";
        }
    }

    @Override
    protected void onPostExecute(String result) {

        Log.d("test", "String: " + result);
        int start = result.indexOf("{", result.indexOf("{") + 1);
        int end = result.lastIndexOf("}");
        String jsonResponse = result.substring(start, end);
        try {
            JSONObject table = new JSONObject(jsonResponse);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private String downloadUrl(String urlString) throws IOException {
        InputStream is = null;

        try {
            URL url = new URL(urlString);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(10000);
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            conn.connect();
            int responseCode = conn.getResponseCode();
            is = conn.getInputStream();
            String contentAsString = convertStreamToString(is);
            return contentAsString;
        } finally {
            if (is != null) {
                is.close();
            }
        }
    }

    private String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}

我仍然没有完成代码,我只想看看我的应用以json格式正确下载电子表格。

我已经被困在这里超过3天,仍然无法找到问题。

我会感激我能得到的每一个帮助!

1 个答案:

答案 0 :(得分:1)

我并不孤单!

我创建了一个从Google电子表格中提取数据的信息中心。几个小时前,我试图运行我的仪表板,收到一些错误,告诉我JSON格式有问题。我看一下&#39; / O_o /&#39;出现在所有文件的顶部。

这必须是谷歌手中的内部变化,无论是否有意,它可能会有望得到改变。