Java类不会编译

时间:2012-07-13 14:11:18

标签: java httpconnection

我得到了一些我似乎无法修复的错误......这是示例代码,所以我很困惑发生了什么。错误被注释到它们出现的行的一侧。

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代码?

2 个答案:

答案 0 :(得分:13)

您不能将代码直接放在类体中 - 您必须将其包装在成员函数中。

如果这是一个独立的程序,成员函数应该是:

public static void main(String[] args) {
    ...
}

答案 1 :(得分:0)

如果您希望在构建“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
        }
    }
}