2013-07-24 94 views
0

我寫了接收數據並存儲它們放在電子表格中的谷歌Apps的腳本,這樣的事情:HTTP GET/POST在Android上不與谷歌Apps腳本工作

https://script.google.com/macros/s/.../exec?t1=hi&t2=foo 

的URL工作正常上瀏覽器和數據確實存儲。但是,當我嘗試從Android應用程序實現此功能時,它不起作用。我已經在AsyncTask中嘗試了HttpGetHttpPost技術,但什麼也沒得到。這是HttpPost的代碼:

class sendData extends AsyncTask<String, Void, String> { 



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

     String [] data = params[0].split("-"); 

     postData(data[0],data[1]); 

    }  

    @Override 
    protected void onPostExecute(String result) {  
     // Update Ui here 

    } 

    public void postData(String l1, String l2) { 
     // Create a new HttpClient and Post Header 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("https://script.google.com/macros/s/.../exec"); 

     try { 
      // Add your data 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
      nameValuePairs.add(new BasicNameValuePair("test", l1)); 
      nameValuePairs.add(new BasicNameValuePair("testt", l2)); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

      // Execute HTTP Post Request 
      HttpResponse response = httpclient.execute(httppost); 

     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
     } 
    } 

} 

問題在哪裏?我已經將腳本和電子表格的隱私設置爲可供任何人訪問和編輯,並且仍然沒有任何迴應。我對web開發人員有點新,所以對我一無所知!

編輯:這裏是logcat。不知道這是否意味着什麼:

07-25 00:17:36.400: D/dalvikvm(31262): Late-enabling CheckJNI 
07-25 00:17:36.415: I/dalvikvm(31262): Turning on JNI app bug workarounds for target SDK version 10... 
07-25 00:17:36.420: E/jdwp(31262): Failed sending reply to debugger: Broken pipe 
07-25 00:17:36.420: D/dalvikvm(31262): Debugger has detached; object registry had 1 entries 
07-25 00:17:40.930: D/dalvikvm(31262): GC_CONCURRENT freed 220K, 7% free 12377K/13191K, paused 5ms+2ms, total 28ms 
07-25 00:17:42.075: D/dalvikvm(31262): GC_CONCURRENT freed 314K, 7% free 12517K/13447K, paused 9ms+3ms, total 67ms 
07-25 00:17:44.850: D/dalvikvm(31262): GC_CONCURRENT freed 341K, 7% free 12630K/13575K, paused 12ms+3ms, total 34ms 
07-25 00:17:59.815: D/dalvikvm(31262): GC_CONCURRENT freed 435K, 8% free 12638K/13703K, paused 2ms+2ms, total 26ms 
07-25 00:18:05.000: D/dalvikvm(31262): GC_CONCURRENT freed 380K, 8% free 12693K/13703K, paused 15ms+5ms, total 54ms 
07-25 00:18:15.030: D/dalvikvm(31262): GC_CONCURRENT freed 366K, 8% free 12776K/13767K, paused 19ms+2ms, total 61ms 
07-25 00:18:56.390: E/SpannableStringBuilder(31262): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
07-25 00:18:56.390: E/SpannableStringBuilder(31262): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
+0

這將是有益的,如果你可以張貼logcat的運行系統。只是爲了理智檢查,你確實給你的應用程序互聯網權限? – roostertech

+0

我確實給了它互聯網許可。我將馬上發佈logcat –

回答

1

我敢打賭,你還沒有發佈公共和匿名的appscript服務。另外請確保使用ContentSevice。

+0

我確實發佈了它作爲Web應用程序的服務公開和匿名,但ContentService是新的部分。我現在就去看看。 –

+0

從快速閱讀中,它是一個服務器響應機制,而不是服務器響應機制。我只關心客戶端發送而不是客戶端接收(儘管ContentService可以用於進一步診斷問題) –

+0

使用它之後,腳本確實已經響應並且完成了它應該具有的邏輯!我甚至發現,因爲我的腳本做了一個'doGet'方法,它只能用於'HttpGet',這是另一回事。謝謝! –

0

我發現了另一個 「少編碼」 的方式讓我的谷歌應用程序腳本讀取我的輸入,使用URLHttpURLConnection.openConnection()

try { 
    URL url = new URL("https://script.google.com/macros/s/.../exec?t1=hi&t2=foo"); 
    HttpURLConnection con = (HttpURLConnection) url 
    .openConnection(); 
    readStream(con.getInputStream()); 
    } catch (Exception e) { 
    e.printStackTrace(); 
} 



private void readStream(InputStream in) { 
    BufferedReader reader = null; 
    try { 
    reader = new BufferedReader(new InputStreamReader(in)); 
    String line = ""; 
    while ((line = reader.readLine()) != null) { 
     System.out.println(line); 
    } 
    } catch (IOException e) { 
    e.printStackTrace(); 
    } finally { 
    if (reader != null) { 
     try { 
     reader.close(); 
     } catch (IOException e) { 
     e.printStackTrace(); 
     } 
    } 
    } 
} 

(來源:http://www.vogella.com/articles/AndroidNetworking/article.html

然而,對於HTTPGET和HttpPost ,Zig的ContentService是要走的路。我包含在谷歌腳本以下任一的doGet()或doPost()的返回:

return ContentService.createTextOutput('Success');