java - URL类里面的方法openConnection方法报错
PHPz
PHPz 2017-04-18 10:29:37
0
4
418

我按照教程来却发现总是报错;
代码如下
private String url; //请求的地址

public httpThread(String url) {
    this.url = url;
}
public String doGet() {
    URL httpUrl = null;
    try {
        httpUrl = new URL(url);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    HttpURLConnection httpURLConnection;
    httpURLConnection = (HttpURLConnection) new httpUrl.openConnection();
    try {
        httpURLConnection.setRequestMethod("GET");      //请求get方法
    } catch (ProtocolException e) {
        e.printStackTrace();
    }
    httpURLConnection.setReadTimeout(5000);
    //接受返回来的数据
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
    } catch (IOException e) {
        e.printStackTrace();
    }
    String str;
    StringBuffer sb = new StringBuffer();
    try {
        while ((str = reader.readLine()) != null) {
            sb.append(str);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return sb.toString();
}
![图片描述][2]
PHPz
PHPz

学习是最好的投资!

reply all(4)
Peter_Zhu

Excuse me, what kind of tutorial is this? Use a browser to see if the request works. It may be that the author had access when writing but not later

小葫芦

Why is there new in front of httpUrl

黄舟
    public URLConnection openConnection() throws java.io.IOException {
        return handler.openConnection(this);
    }

This is a way to remove the new

Peter_Zhu
URL httpUrl = null;
HttpURLConnection httpURLConnection;
try {
    httpUrl = new URL(url);
    httpURLConnection = (HttpURLConnection) httpUrl.openConnection();
    httpURLConnection.setRequestMethod("GET");      //请求get方法
    httpURLConnection.setReadTimeout(5000);
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} catch (ProtocolException e) {
    e.printStackTrace();
}

Change the first few sentences until "//Accept the returned data" to the above and take a look again
Two questions, one, why add new before the calling method? Do you want it every day? The IDE reported an error. Move the mouse up or put the cursor on Ctrl+F1 to take a closer look at what it is. In most cases, you can see the reason. Second, the variables outside the try code block are initially null, and the values ​​are assigned in the try. The catch code block is not properly To handle exceptions, just use printStackTrace to report the exception. Then when you later call the method that refers to the variable, a NullPointException will be reported. This is the httpUrl object. Or write those few sentences into try. If an error occurs, don't run it. Do you want to verify whether it is null when calling outside? In short, the exception handling here is not good. Newbies must learn to handle exceptions properly and develop good habits like you write comments. I will not repeat the code behind you. I changed it. In addition, I didn’t write the processing code, but the idea is like this. Don’t printStackTrace to make trouble. Log when you should log, and throw when you should throw. Otherwise, in the future, when your code is in the production environment, operation and maintenance will rely entirely on telepathy. Not being scolded to death.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template