【问题标题】:how to solve this error, non static method cannot be referenced from a static context [duplicate]如何解决此错误,无法从静态上下文中引用非静态方法[重复]
【发布时间】:2015-12-13 11:50:36
【问题描述】:

这是代码

protected Void doInBackground(String... params) {
   String reg_url = "http://10.0.2.2/";
    String method = params [0];
    if (method.equals("register") ){
        String first_name = params [1];
        String last_name = params [2];
        String address = params [3];
        String email = params [4];
        String password = params [5];

        try {
            URL url = new URL(reg_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            HttpURLConnection.setRequestMethod("POST");
            HttpURLConnection.setDoOutput(True);

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    return null;
}

我在这两行得到了错误

HttpURLConnection.setRequestMethod("POST");
            HttpURLConnection.setDoOutput(True);

关于 set.RequestMethod("POST") 和 "setDoOutput(true); 错误说不能从静态上下文中引用非静态方法。 这一定是个愚蠢的错误,但我就是想不通,所以有人可以帮我解决这个问题吗?

【问题讨论】:

  • 感谢所有帮助我解决问题的三位成员。你是最棒的
  • 以后请搜索类似问题。这个问题每周至少被问 5 到 10 次,我不认为该网站会从再添加一个中受益。例如:simple search on error message.

标签: java methods non-static


【解决方案1】:

使用你在调用openConnection时获得的实例:

        HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setDoOutput(true);

请注意,Java 区分大小写。 HttpURLConnection 是类名。 httpURLConnection 是一个引用类实例的变量。

【讨论】:

    【解决方案2】:
    HttpURLConnection.setRequestMethod("POST");
    HttpURLConnection.setDoOutput(True);
    

    您正在使用静态方法语法访问非静态方法

    使用参考 httpURLConnection 也可以访问方法

    httpURLConnection.setRequestMethod("POST");
    httpURLConnection.setDoOutput(true);
    

    【讨论】:

      【解决方案3】:

      您必须在 URLConnection 实例上调用 setRequestMethod 和 setDoOutput 方法。

      protected Void doInBackground(String... params) {
         String reg_url = "http://10.0.2.2/";
          String method = params [0];
          if (method.equals("register") ){
              String first_name = params [1];
              String last_name = params [2];
              String address = params [3];
              String email = params [4];
              String password = params [5];
      
              try {
                  URL url = new URL(reg_url);
                  HttpURLConnection con = (HttpURLConnection)url.openConnection();
                  con.setRequestMethod("POST");
                  con.setDoOutput(True);
      
              } catch (MalformedURLException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
      
          }
          return null;
      }
      

      【讨论】:

        猜你喜欢
        • 2015-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-03
        • 1970-01-01
        相关资源
        最近更新 更多