DELTALABSITE https://www.deltalabsite.com/2019/02/guide-post-to-google-spreadsheet.html

[GUIDE] POST TO GOOGLE SPREADSHEET


  • SERVER - GOOGLE SPREADSHEET
    1. Create new Spreadsheet file
    2. Select Tools - Script Editor
    3. Remove all code and fill with this code ( CHANGE_WITH_YOUR_GOOGLE_SHEET_URL with your spreadsheet url on step 1) :
      
      function doPost(e){
        var op = e.parameter.action;
      
        var ss = SpreadsheetApp.openByUrl("CHANGE_WITH_YOUR_GOOGLE_SHEET_URL");
        var sheet = ss.getSheetByName("Sheet1");
        
        if (op == "insert")
              return insert(e, sheet);
      
      }
      
      function insert(e,sheet) {
        
        var device = e.parameter.device;
        var package = e.parameter.package;
      
          var flag = 1;
          var lr = sheet.getLastRow();
          for (var i = 1; i <= lr; i++) {
              var id1 = sheet.getRange(i, 2).getValue();
              if (id1 == device) {
                  flag = 0;
                  var result = "Error";
              }
          }
          //add new row with recieved parameter from client
          if (flag == 1) {
              var d = new Date();
              var currentTime = d.toLocaleString();
              var rowData = sheet.appendRow([currentTime, device, package]);
      
      
              var result = "Success";
          }
       
          result = JSON.stringify({
      
              "result": result
      
          });
      
          return ContentService.createTextOutput(result).setMimeType(ContentService.MimeType.JAVASCRIPT); 
       
      }
      
      
    4. Save and publish to Deploy as web app
    5. Enter your project name
    6. Who has access to the app: change to anyone even anonymous 
    7. Deploy
    8. Review permission
    9. if show This app isn't verified select advanced Go to "your project name" (unsafe) and Allow
    10. Copy Current web app URL:
  • CLIENT - ANDROID DEVICE
    1. Create new Android Studio Project
    2. Create new java file with name PostRequest.java (CHANGE_WITH_YOUR_GOOGLE_SCRIPT_URL = step 10)
      
      public class PostRequest {
      
          Activity activity;
      
          public PostRequest(Activity activity){
              this.activity = activity;
          }
      
          public void Post(){
              new SendRequest().execute();
          }
      
          public class SendRequest extends AsyncTask<String, Void, String> {
              // Google Script URL
              String urls = "CHANGE_WITH_YOUR_GOOGLE_SCRIPT_URL";
      
              protected void onPreExecute(){}
      
              protected String doInBackground(String... arg0) {
      
                  try{
      
                      URL url = new URL(urls+"?action=insert");
                      JSONObject postDataParams = new JSONObject();
      
                      String mModel = android.os.Build.MODEL;
      
                      postDataParams.put("device", mModel);
                      postDataParams.put("package", activity.getPackageName());
      
                      Log.e("params",postDataParams.toString());
      
                      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                      conn.setReadTimeout(15000 /* milliseconds */);
                      conn.setConnectTimeout(15000 /* milliseconds */);
                      conn.setRequestMethod("POST");
                      conn.setDoInput(true);
                      conn.setDoOutput(true);
      
                      OutputStream os = conn.getOutputStream();
                      BufferedWriter writer = new BufferedWriter(
                              new OutputStreamWriter(os, "UTF-8"));
                      writer.write(getPostDataString(postDataParams));
      
                      writer.flush();
                      writer.close();
                      os.close();
      
                      int responseCode=conn.getResponseCode();
      
                      if (responseCode == HttpsURLConnection.HTTP_OK) {
      
                          BufferedReader in = new BufferedReader( new InputStreamReader(conn.getInputStream()));
                          StringBuffer sb = new StringBuffer("");
                          String line = "";
      
                          while((line = in.readLine()) != null) {
      
                              sb.append(line);
                              break;
                          }
      
                          in.close();
                          return sb.toString();
      
                      }
                      else {
                          return new String("false : "+responseCode);
                      }
                  }
                  catch(Exception e){
                      return new String("Exception: " + e.getMessage());
                  }
              }
      
              @Override
              protected void onPostExecute(String result) {
                  //Notifikasi untuk hasil post ke server
                /*  Toast.makeText(activity.getApplicationContext(), result,
                          Toast.LENGTH_LONG).show();
                          */
              }
          }
      
          public String getPostDataString(JSONObject params) throws Exception {
      
              StringBuilder result = new StringBuilder();
              boolean first = true;
      
              Iterator<String> itr = params.keys();
      
              while(itr.hasNext()){
      
                  String key= itr.next();
                  Object value = params.get(key);
      
                  if (first)
                      first = false;
                  else
                      result.append("&");
      
                  result.append(URLEncoder.encode(key, "UTF-8"));
                  result.append("=");
                  result.append(URLEncoder.encode(value.toString(), "UTF-8"));
      
              }
      
              return result.toString();
          }
      
      }
      
      
    3. On your MainActivity put this code
      
      new PostRequest(this).Post();
      
      

Share this post:

DELTALABS
Posted by: DELTALABS
Label:
Reaksi:

Subscribe to our newsletter

Notification