这是代码
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);
on set.RequestMethod(“POST”)和“setDoOutput(true);错误表示非静态方法无法从静态上下文引用。 它一定是愚蠢的错误,但我无法弄明白,所以有人可以帮我这个吗?
答案 0 :(得分:1)
使用您在openConnection
的调用中获得的实例:
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
请注意,Java区分大小写。 HttpURLConnection
是类名。 httpURLConnection
是一个引用类实例的变量。
答案 1 :(得分:0)
HttpURLConnection.setRequestMethod("POST");
HttpURLConnection.setDoOutput(True);
您正在使用静态方法语法访问非静态方法
使用引用httpURLConnection
也可以访问方法
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
答案 2 :(得分:0)
您必须在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;
}