0

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

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

4

2 回答 2

13

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

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

public static void main(String[] args) {
    ...
}
于 2012-07-13T14:13:00.877 回答
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
        }
    }
}
于 2012-07-13T14:23:26.653 回答