问题描述:

I tried using the code sent on Secure HTTP Post in Android

but my data does not get updated on my spreadsheet. My app reads QR codes and I want to send the data and time onto the spreadsheet.

My MainActivity.java

package balaji.qrcode_scan;

import java.util.*;

import android.net.*;

import android.app.*;

import android.content.*;

import java.text.*;

import android.os.*;

import android.view.*;

import android.widget.*;

import com.squareup.okhttp.*;

import java.io.*;

import android.support.v7.app.ActionBarActivity;

public class MainActivity extends Activity {

final String myTag = "DocsUpload";

private static final int ACTIVITY_RESULT_QR_DRDROID = 0;

TextView report,time;

Button scan1,scan2,scan3,scan4;

String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());

String result;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

report = (TextView) findViewById(R.id.textView2);

time = (TextView) findViewById(R.id.textView3);

scan1 = (Button) findViewById(R.id.button1);

scan2 = (Button) findViewById(R.id.button2);

scan3 = (Button) findViewById(R.id.button5);

scan4 = (Button) findViewById(R.id.button6);

scan1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

Intent i = new Intent("la.droid.qr.scan");

try {

startActivityForResult(i, ACTIVITY_RESULT_QR_DRDROID);

}

catch (ActivityNotFoundException activity) {

MainActivity.qrDroidRequired(MainActivity.this);

}

}

});

scan2.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

Intent i = new Intent("la.droid.qr.scan");

try {

startActivityForResult(i, ACTIVITY_RESULT_QR_DRDROID);

}

catch (ActivityNotFoundException activity) {

MainActivity.qrDroidRequired(MainActivity.this);

}

}

});

scan3.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

Intent i = new Intent("la.droid.qr.scan");

try {

startActivityForResult(i, ACTIVITY_RESULT_QR_DRDROID);

}

catch (ActivityNotFoundException activity) {

MainActivity.qrDroidRequired(MainActivity.this);

}

}

});

scan4.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

Intent i = new Intent("la.droid.qr.scan");

try {

startActivityForResult(i, ACTIVITY_RESULT_QR_DRDROID);

} catch (ActivityNotFoundException activity) {

MainActivity.qrDroidRequired(MainActivity.this);

}

}

});

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if( ACTIVITY_RESULT_QR_DRDROID == requestCode

&& data != null && data.getExtras() != null ) {

result = data.getExtras().getString("la.droid.qr.result");

report.setText(result);

report.setVisibility(View.VISIBLE);

time.setText(currentDateTimeString);

time.setVisibility(View.VISIBLE);

postData();

}

}

public void postData() {

String fullUrl = "https://docs.google.com/forms/d/1KQu4UyNMdwlj2PmXgRQWST_V78iV5lGQv6G0rAGpY5M/formResponse";

HttpRequest mReq = new HttpRequest();

String col1 = result;

String col2 =currentDateTimeString ;

String data1 = "entry_1618803678=" + URLEncoder.encode(col1) + "&" +

"entry_1175391052=" + URLEncoder.encode(col2);

String response = mReq.sendPost(fullUrl, data1);

Log.i(myTag, response);

}

/*

*

* If we don't have QRDroid Application in our Device,

* It will call below method (qrDroidRequired)

*

*/

protected static void qrDroidRequired(final MainActivity activity) {

AlertDialog.Builder AlertBox = new AlertDialog.Builder(activity);

AlertBox.setMessage("QRDroid Missing");

AlertBox.setPositiveButton("Direct Download", new OnClickListener() {

@Override

public void onClick(DialogInterface arg0, int arg1) {

activity.startActivity(new Intent(Intent.ACTION_VIEW,

Uri.parse("http://droid.la/apk/qr/")));

}

});

AlertBox.setNeutralButton("From Market", new OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

activity.startActivity(new Intent(Intent.ACTION_VIEW,

Uri.parse("http://market.android.com/details? id=la.droid.qr")));

}

});

AlertBox.setNegativeButton("Cancel", new OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int which) {

dialog.cancel();

}

});

AlertBox.create().show();

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

public void close(View v){

v.setVisibility(View.INVISIBLE);

System.exit(1);

}

}

HttpRequest.java is what i got from a stackoverflow answer,

package balaji.qrcode_scan;

import java.io.IOException;

import java.io.InputStream;

import java.io.UnsupportedEncodingException;

import java.net.HttpURLConnection;

import java.net.URL;

import java.net.URLConnection;

import org.apache.http.HttpResponse;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.client.params.ClientPNames;

import org.apache.http.client.params.CookiePolicy;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.params.BasicHttpParams;

import org.apache.http.params.HttpConnectionParams;

import org.apache.http.params.HttpParams;

import org.apache.http.protocol.BasicHttpContext;

import org.apache.http.protocol.HttpContext;

import org.apache.http.util.EntityUtils;

import org.json.JSONObject;

import android.util.Log;

public class HttpRequest{

DefaultHttpClient httpClient;

HttpContext localContext;

private String ret;

HttpResponse response = null;

HttpPost httpPost = null;

HttpGet httpGet = null;

public HttpRequest(){

HttpParams myParams = new BasicHttpParams();

HttpConnectionParams.setConnectionTimeout(myParams, 10000);

HttpConnectionParams.setSoTimeout(myParams, 10000);

httpClient = new DefaultHttpClient(myParams);

localContext = new BasicHttpContext();

}

public void clearCookies() {

httpClient.getCookieStore().clear();

}

public void abort() {

try {

if (httpClient != null) {

System.out.println("Abort.");

httpPost.abort();

}

} catch (Exception e) {

System.out.println("Your App Name Here" + e);

}

}

public String sendPost(String url, String data1) {

return sendPost(url, data1, null);

}

public String sendJSONPost(String url, JSONObject data) {

return sendPost(url, data.toString(), "application/json");

}

public String sendPost(String url, String data1, String contentType) {

ret = null;

httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);

httpPost = new HttpPost(url);

response = null;

StringEntity tmp = null;

Log.d("Your App Name Here", "Setting httpPost headers");

httpPost.setHeader("User-Agent", "SET YOUR USER AGENT STRING HERE");

httpPost.setHeader("Accept", "text/html,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*;q=0.5");

if (contentType != null) {

httpPost.setHeader("Content-Type", contentType);

} else {

httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");

}

try {

tmp = new StringEntity(data1,"UTF-8");

} catch (UnsupportedEncodingException e) {

Log.e("Your App Name Here", "HttpUtils : UnsupportedEncodingException : "+e);

}

httpPost.setEntity(tmp);

Log.d("Your App Name Here", url + "?" + data1);

try {

response = httpClient.execute(httpPost,localContext);

if (response != null) {

ret = EntityUtils.toString(response.getEntity());

}

} catch (Exception e) {

Log.e("Your App Name Here", "HttpUtils: " + e);

}

Log.d("Your App Name Here", "Returning value:" + ret);

return ret;

}

public String sendGet(String url) {

httpGet = new HttpGet(url);

try {

response = httpClient.execute(httpGet);

} catch (Exception e) {

Log.e("Your App Name Here", e.getMessage());

}

//int status = response.getStatusLine().getStatusCode();

// we assume that the response body contains the error message

try {

ret = EntityUtils.toString(response.getEntity());

} catch (IOException e) {

Log.e("Your App Name Here", e.getMessage());

}

return ret;

}

public InputStream getHttpStream(String urlString) throws IOException {

InputStream in = null;

int response = -1;

URL url = new URL(urlString);

URLConnection conn = url.openConnection();

if (!(conn instanceof HttpURLConnection))

throw new IOException("Not an HTTP connection");

try{

HttpURLConnection httpConn = (HttpURLConnection) conn;

httpConn.setAllowUserInteraction(false);

httpConn.setInstanceFollowRedirects(true);

httpConn.setRequestMethod("GET");

httpConn.connect();

response = httpConn.getResponseCode();

if (response == HttpURLConnection.HTTP_OK) {

in = httpConn.getInputStream();

}

} catch (Exception e) {

throw new IOException("Error connecting");

} // end try-catch

return in;

}

}

and my activity_main.xml is this=

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity" >

<TextView

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_centerHorizontal="true"

android:layout_marginTop="23dp"

android:text="Attendance Manager"

android:textColor="#4169E1"

android:textSize="20sp" />

<TextView

android:id="@+id/textView3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/textView2"

android:layout_centerHorizontal="true"

android:layout_marginTop="15dp"

android:text="Time" />

<Button

android:id="@+id/button3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:layout_alignParentRight="true"

android:layout_marginBottom="42dp"

android:layout_marginRight="20dp"

android:onClick="close"

android:text="Close App" />

<Button

android:id="@+id/button4"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignBaseline="@+id/button3"

android:layout_alignBottom="@+id/button3"

android:layout_toLeftOf="@+id/button3"

android:text="New Regestrations" />

<Button

android:id="@+id/button5"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/button2"

android:layout_alignRight="@+id/button3"

android:layout_below="@+id/button2"

android:text="Start Break" />

<TextView

android:id="@+id/textView2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/button5"

android:layout_centerHorizontal="true"

android:layout_marginTop="71dp"

android:text="Scan Report"

android:textSize="18sp" />

<Button

android:id="@+id/button6"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/button5"

android:layout_alignRight="@+id/button3"

android:layout_below="@+id/button5"

android:text="End break" />

<Button

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/button4"

android:layout_alignRight="@+id/button3"

android:layout_below="@+id/textView1"

android:text="Enter Work" />

<Button

android:id="@+id/button2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/button1"

android:layout_alignRight="@+id/button3"

android:layout_below="@+id/button1"

android:text="Exit Work" />

</RelativeLayout>

相关阅读:
Top