如何解决此错误,无法从静态上下文中引用非静态方法
how to solve this error, non static method cannot be referenced from a static context
这是代码
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);错误说无法从静态上下文中引用非静态方法。
这一定是个愚蠢的错误,但我就是想不通所以有人可以帮我解决这个问题吗?
使用您在调用 openConnection
时获得的实例:
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
请注意 Java 区分大小写。 HttpURLConnection
是 class 名称。 httpURLConnection
是引用 class.
实例的变量
HttpURLConnection.setRequestMethod("POST");
HttpURLConnection.setDoOutput(True);
您正在使用静态方法语法访问非静态方法
使用参考 httpURLConnection
也访问方法
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
您必须在 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;
}
这是代码
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);错误说无法从静态上下文中引用非静态方法。 这一定是个愚蠢的错误,但我就是想不通所以有人可以帮我解决这个问题吗?
使用您在调用 openConnection
时获得的实例:
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
请注意 Java 区分大小写。 HttpURLConnection
是 class 名称。 httpURLConnection
是引用 class.
HttpURLConnection.setRequestMethod("POST");
HttpURLConnection.setDoOutput(True);
您正在使用静态方法语法访问非静态方法
使用参考 httpURLConnection
也访问方法
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
您必须在 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;
}