0

我正在创建一个 android 应用程序的活动,用户在其中填写他的详细信息(由 id 的名称、bhawan、房间、手机、书籍等标识),这些值将直接传输到 Google 电子表格。它给了我那个奇怪的错误。你能帮我吗 ?提前致谢。

public class MainActivity extends Activity {

    final EditText nameField = (EditText) findViewById(R.id.EditTextName);  
    String name = nameField.getText().toString(); 

    final EditText bhawanField = (EditText) findViewById(R.id.EditTextBhawan);  
    String bhawan = bhawanField.getText().toString();

    final EditText roomField = (EditText) findViewById(R.id.EditTextRoom);  
    String room = roomField.getText().toString();

    final EditText mobileField = (EditText) findViewById(R.id.EditTextPhone);  
    String mobile = mobileField.getText().toString();

    final EditText emailField = (EditText) findViewById(R.id.EditTextEmail);  
    String email = emailField.getText().toString();

    final EditText bookField = (EditText) findViewById(R.id.EditTextBook);  
    String book = bookField.getText().toString();

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

        Button sendButton = (Button) findViewById(R.id.ButtonSubmit);
        sendButton.setOnClickListener(new OnClickListener() {           
            public void onClick(View v) {
                btn_Click();
            }}
        );
    }   

    public void btn_Click(){
         SendHttpRequset();       
    }

     public void SendHttpRequset(){
         HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost("https://spreadsheets.google.com/spreadsheet/formResponse?hl=en_US&formkey=dDlwZzh4bGFvNFBxUmRsR0d2VTVhYnc6MQ&ifq");

            List<BasicNameValuePair> results = new ArrayList<BasicNameValuePair>(6);
            results.add(new BasicNameValuePair("entry.0.single", name));
            results.add(new BasicNameValuePair("entry.1.single", bhawan));
            results.add(new BasicNameValuePair("entry.2.single", room));
            results.add(new BasicNameValuePair("entry.4.single", mobile));
            results.add(new BasicNameValuePair("entry.6.single", email));
            results.add(new BasicNameValuePair("entry.8.single", book));

            try {
                HttpPost.setEntity(new UrlEncodedFormEntity(results));
            }
            catch (UnsupportedEncodingException e) {
                // writing error to Log
                e.printStackTrace();
            }

            // Making HTTP Request
            try {
                HttpResponse response = HttpClient.execute(post);

                // writing response to log
                Log.d("Http Response:", response.toString());

            } catch (ClientProtocolException e) {
                // writing exception to log
                e.printStackTrace();

            } catch (IOException e) {
                // writing exception to log
                e.printStackTrace();
            }
     }

    /*public void submitDetails() {
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("https://spreadsheets.google.com/spreadsheet/formResponse?hl=en_US&amp;formkey=dDlwZzh4bGFvNFBxUmRsR0d2VTVhYnc6MQ&amp;ifq");

        List<BasicNameValuePair> results = new ArrayList<BasicNameValuePair>(6);
        results.add(new BasicNameValuePair("entry.0.single", name));
        results.add(new BasicNameValuePair("entry.1.single", bhawan));
        results.add(new BasicNameValuePair("entry.2.single", room));
        results.add(new BasicNameValuePair("entry.4.single", mobile));
        results.add(new BasicNameValuePair("entry.6.single", email));
        results.add(new BasicNameValuePair("entry.8.single", book));

        try {
            HttpPost.setEntity(new UrlEncodedFormEntity(results));
        }
        catch (UnsupportedEncodingException e) {
            // writing error to Log
            e.printStackTrace();
        }

        // Making HTTP Request
        try {
            HttpResponse response = HttpClient.execute(post);

            // writing response to log
            Log.d("Http Response:", response.toString());

        } catch (ClientProtocolException e) {
            // writing exception to log
            e.printStackTrace();

        } catch (IOException e) {
            // writing exception to log
            e.printStackTrace();
        }
    }*/
}
4

2 回答 2

2

尝试改变:

HttpPost.setEntity(new UrlEncodedFormEntity(results));

post.setEntity(new UrlEncodedFormEntity(results));

您之前以调用静态函数的方式调用 setEntity(),但它不是静态方法。您可能打算使用post您之前创建的对象来调用它。

于 2013-01-17T13:11:01.703 回答
0

HttpPost 是一个静态类,但 setEntity() 是一个非静态方法。要调用非静态方法,您需要使用new关键字或该类提供的其他方法实例化该类的一个实例,例如myHttpPost。

于 2013-01-17T13:15:12.817 回答