获取错误“无法从类型HttpEntityEnclosingRequestBase中对非静态方法setEntity(HttpEntity)进行静态引用”

时间:2013-01-17 13:09:09

标签: android spreadsheet http-request

我正在创建一个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();
        }
    }*/
}

2 个答案:

答案 0 :(得分:2)

尝试更改:

HttpPost.setEntity(new UrlEncodedFormEntity(results));

post.setEntity(new UrlEncodedFormEntity(results));

当你不是一个静态方法时,你以前用来调用静态函数的方式调用setEntity()。您可能想要使用之前创建的post对象来调用它。

答案 1 :(得分:0)

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