简体   繁体   中英

HTTP Get/Post on Android doesn't work with Google Apps Script

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

I bet you haven't published the appscript service as public and anonymous. Also make sure to use ContentSevice.

I found out another "Less coding" way to get my Google Apps script to read my input, using URL and 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();
        }
    }
  }
} 

(Source: http://www.vogella.com/articles/AndroidNetworking/article.html )

However for HttpGet and HttpPost, Zig's ContentService was the way to go. I included the return in either doGet() or doPost() in the Google script as the following:

return ContentService.createTextOutput('Success');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM