0

I wrote a Google Apps script that receives data and stores them on a spreadsheet, something like this:

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

The URL works fine on the browser and the data is indeed stored. However, when I try to implement this from an Android app, it doesn't work. I've tried both HttpGet and HttpPost techniques in an AsyncTask and got nothing. This is the code for the 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
        }
    } 

}

Where could the problem be? I've set the privacy of both the script and the spreadsheet to be accessible and editable by anyone and still no response.

Here's the logcat. Not sure if it means something:

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
4

2 回答 2

1

我敢打赌,您还没有将 appscript 服务发布为公开和匿名的。还要确保使用 ContentService。

于 2013-07-24T23:58:02.223 回答
0

我发现了另一种“更少编码”的方法来让我的 Google Apps 脚本读取我的输入,使用URLand HttpURLConnection.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,ZigContentService是要走的路。我在 Google 脚本中的 doGet() 或 doPost() 中包含了返回,如下所示:

return ContentService.createTextOutput('Success');
于 2013-07-25T07:49:20.907 回答