我遇到了几个似乎无法修复的错误。。。这是示例代码,所以我很困惑发生了什么。错误被注释到它们显示的行的一侧。
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
public class Connect {
URL url;
URLConnection urlConnection;
DataOutputStream outStream;
DataInputStream inStream;
// Build request body
String body = "fname=" + URLEncoder.encode("Atli", "UTF-8"); //Syntax error on token ";", { expected after this token
// Create connection
url = new URL("http://192.168.1.68/test/POST/post.php");
urlConnection = url.openConnection();
((HttpURLConnection)urlConnection).setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlConnection.setRequestProperty("Content-Length", ""+ body.length());
// Create I/O streams
outStream = new DataOutputStream(urlConnection.getOutputStream());
inStream = new DataInputStream(urlConnection.getInputStream());
// Send request
outStream.writeBytes(body);
outStream.flush();
outStream.close();
// Get Response
// - For debugging purposes only!
String buffer;
while((buffer = inStream.readLine()) != null) {
System.out.println(buffer);
}
// Close I/O streams
inStream.close();
outStream.close();
}
给出错误:
Syntax error, insert "}" to complete ClassBody
有人看到我没有看到的明显的东西吗?
附带说明一下,如果php返回html代码,那么在运行此类时,java是否能够理解html代码?
不能将代码直接放在类主体中,必须将其封装在成员函数中。
如果这是一个独立的程序,那么成员功能应该是:
public static void main(String[] args) {
...
}
如果您希望在构造"Connect"实例时执行该代码,则应将其放入构造函数中。这也修复了您的所有错误。
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
public class Connect {
URL url;
URLConnection urlConnection;
DataOutputStream outStream;
DataInputStream inStream;
@SuppressWarnings("deprecation")
public Connect() {
// Build request body
try {
String body = "fname=" + URLEncoder.encode("Atli", "UTF-8");
// Create connection
url = new URL("http://192.168.1.68/test/POST/post.php");
urlConnection = url.openConnection();
((HttpURLConnection) urlConnection).setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlConnection.setRequestProperty("Content-Length", "" + body.length());
// Create I/O streams
outStream = new DataOutputStream(urlConnection.getOutputStream());
inStream = new DataInputStream(urlConnection.getInputStream());
// Send request
outStream.writeBytes(body);
outStream.flush();
outStream.close();
// Get Response
// - For debugging purposes only!
String buffer;
// DataInputStream.readLine is deprecated
// use the BufferedReader class instead
while ((buffer = inStream.readLine()) != null) {
System.out.println(buffer);
}
// Close I/O streams
inStream.close();
outStream.close();
} catch (IOException iOException) {
// there are many methods that can throw errors in this code,
// you should catch those errors
}
}
}