Created
April 11, 2022 07:44
-
-
Save tauheed0007/934c547670752eb504c0951cb5668973 to your computer and use it in GitHub Desktop.
method to post data in Google sheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.androidtogooglesheetdatabase.Post; | |
import android.app.ProgressDialog; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import androidx.appcompat.app.AppCompatActivity; | |
import org.json.JSONObject; | |
import java.io.BufferedReader; | |
import java.io.BufferedWriter; | |
import java.io.InputStreamReader; | |
import java.io.OutputStream; | |
import java.io.OutputStreamWriter; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.net.URLEncoder; | |
import java.util.Iterator; | |
import javax.net.ssl.HttpsURLConnection; | |
import com.example.androidtogooglesheetdatabase.R; | |
public class PostData extends AppCompatActivity { | |
private ProgressDialog progress; | |
TextView tvName; | |
TextView tvCountry; | |
Button button; | |
String name; | |
String country; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.form); | |
button=(Button)findViewById(R.id.btn_signup); | |
tvName=(EditText)findViewById(R.id.input_name); | |
tvCountry=(EditText)findViewById(R.id.input_country); | |
button.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
name = tvName.getText().toString(); | |
country=tvCountry.getText().toString(); | |
new SendRequest().execute(); | |
} | |
} ); | |
} | |
public class SendRequest extends AsyncTask<String, Void, String> { | |
protected void onPreExecute(){} | |
protected String doInBackground(String... arg0) { | |
try{ | |
//Change your web app deployed URL or u can use this for attributes (name, country) | |
URL url = new URL("https://script.google.com/macros/s/AKfycbyIC5DRDQZJhkSQnRO1S1IkFvap1KER0oKoGio9pGPVtmMs8TpgwLvNJXZixt7CCENa/exec"); | |
JSONObject postDataParams = new JSONObject(); | |
//int i; | |
//for(i=1;i<=70;i++) | |
// String usn = Integer.toString(i); | |
String id= "1ap2FIwNKptzm4pJLoMR4Xd68hjPAP2Wv-1hiqXzOEGI"; | |
postDataParams.put("name",name); | |
postDataParams.put("country",country); | |
postDataParams.put("id",id); | |
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) { | |
Toast.makeText(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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment