2015-08-29 64 views
1

我的Android應用程序出現問題。我正在嘗試讓用戶在應用程序中填寫表單,並將其回覆發送到Google在線表單,並將回覆記錄在Google電子表格中。在應用程序本身中一切似乎都很好,我在那裏沒有遇到任何錯誤,但只有時間戳顯示在響應電子表格中,並且沒有文本。Android應用程序 - 將數據發佈到Google表單

這是我的POSTDATA方法的代碼:

public void postData(){ 
    String url = getString(R.string.post_URL); 
    String name = "testname"; 
    Log.i("test",name); 
    String lunch = "testlunch"; 
    Log.i("test", lunch); 
    String vegetarian="testveg"; 
    Log.i("test",vegetarian); 
    String allergies = "testaler"; 
    Log.i("test",allergies); 
    String special = "testspec"; 
    Log.i("test",special); 
    HttpRequest request = new HttpRequest(); 
     try { 
      String data = R.string.entry_name + URLEncoder.encode(name, "UTF-8") + "&" + 
       R.string.entry_lunch + URLEncoder.encode(lunch, "UTF-8") + "&" + 
       R.string.entry_vegetarian + URLEncoder.encode(vegetarian, "UTF-8") + "&" + 
       R.string.entry_allergies + URLEncoder.encode(allergies, "UTF-8") + "&" + 
       R.string.entry_special + URLEncoder.encode(special,"UTF-8"); 

     String response = request.sendPost(url,data); 
     Log.i("response",response); 
    } 
    catch(UnsupportedEncodingException e){ 
     Log.d("Unsupported exception", e.toString()); 
    }} 

我使用谷歌的形式,此網址:

https://docs.google.com/forms/d/1XPaQe0j86XwcxbO13uxZRawCwUexyJMHdPlPLoQaD1A/formResponse 

針對這些項目,我使用此格式的字符串:

entry.569049980 

這是我如何調用該函數(只是暫時的):

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    new Connection().execute(); 
} 

private class Connection extends AsyncTask { 
    @Override 
    protected Object doInBackground(Object...arg0){ 
     postData(); 
     return null; 
    } 

的HttpRequest的文件,我使用的是來自here

import android.util.Log; 
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 java.io.IOException; 
import java.io.InputStream; 
import java.io.UnsupportedEncodingException; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.net.URLConnection; 

/* 
* This helper class was created by StackOverflow user: MattC  https://stackoverflow.com/users/21126/mattc 
* IT was posted as an Answer to this question:  https://stackoverflow.com/questions/2253061/secure-http-post-in-android 
*/ 

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 data) { 
    return sendPost(url, data, null); 
} 

public String sendJSONPost(String url, JSONObject data) { 
    return sendPost(url, data.toString(), "application/json"); 
} 

public String sendPost(String url, String data, String contentType) { 
    ret = null; 

    // httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109); 
    httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, 
      CookiePolicy.NETSCAPE); 
    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(data,"UTF-8"); 
    } catch (UnsupportedEncodingException e) { 
     Log.e("Your App Name Here", "HttpUtils : UnsupportedEncodingException : " + e); 
    } 

    httpPost.setEntity(tmp); 

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

    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; 
    } 
} 

我需要使用的驅動器API或我可以做一些修改,以使這項工作? (這似乎更容易,我根本沒有使用Drive API的經驗)

回答

0

您的條目格式應該是「entry_number」,並在您的數據中進行一些更正。希望這會幫助你。

String data = "entry_number1lkjhgfdrtyuo=" + URLEncoder.encode(col1) + "&" + 
      "entry_number2=" + URLEncoder.encode(col2) + "&" + 
      "entry_number3=" + URLEncoder.encode(col3); 
相關問題