<;Android/Java>;为什么不同的初始化位置会破坏代码?

<;Android/Java>;为什么不同的初始化位置会破坏代码?,java,android,Java,Android,我花了几个小时来解决这个问题,这可以从我的日志中看出。不幸的是,没有人能帮我解决这个问题。解决方案非常简单,只需出于某种原因更改初始化的位置即可。TBH我很幸运它能为我工作,因为我只是尝试一下,却不知道背景中发生了什么。有人能告诉我为什么第一个代码可以工作,而第二个不可以,仅仅是因为初始化位置。因为流是在构造函数中完成的,所以不应该初始化流并且不为null吗 工作代码: private HttpURLConnection httpURLConnection; private URL url; p

我花了几个小时来解决这个问题,这可以从我的日志中看出。不幸的是,没有人能帮我解决这个问题。解决方案非常简单,只需出于某种原因更改初始化的位置即可。TBH我很幸运它能为我工作,因为我只是尝试一下,却不知道背景中发生了什么。有人能告诉我为什么第一个代码可以工作,而第二个不可以,仅仅是因为初始化位置。因为流是在构造函数中完成的,所以不应该初始化流并且不为null吗

工作代码:

private HttpURLConnection httpURLConnection;
private URL url;
private InputStream inputStream;
private BufferedReader bufferedReader;
private BufferedWriter bufferedWriter;
private OutputStream outputStream;

public Connection(String login_url) throws IOException {

    url = new URL(login_url);
    httpURLConnection = (HttpURLConnection)url.openConnection();
    httpURLConnection.setRequestMethod("POST");
    httpURLConnection.setDoOutput(true);
    httpURLConnection.setDoInput(true);

}

public void write(String post_data) throws IOException {
    outputStream = httpURLConnection.getOutputStream();
    bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
    bufferedWriter.write(post_data);
    bufferedWriter.flush();
}

public String read() throws IOException {
    inputStream = httpURLConnection.getInputStream();
    bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
    String result="", line;

    while((line = bufferedReader.readLine()) != null){
        result += line;
    }

    return result;
}

public void close() throws IOException {
        bufferedWriter.close();
        outputStream.close();
        bufferedReader.close();
        inputStream.close();
    }
private HttpURLConnection httpURLConnection;
private URL url;
private InputStream inputStream;
private BufferedReader bufferedReader;
private BufferedWriter bufferedWriter;
private OutputStream outputStream;

public Connection(String login_url) throws IOException {

    url = new URL(login_url);
    httpURLConnection = (HttpURLConnection)url.openConnection();
    httpURLConnection.setRequestMethod("POST");
    httpURLConnection.setDoOutput(true);
    httpURLConnection.setDoInput(true);

    outputStream = httpURLConnection.getOutputStream();
    inputStream = httpURLConnection.getInputStream();

}

public void write(String post_data) throws IOException {

    bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
    bufferedWriter.write(post_data);
    bufferedWriter.flush();
}

public String read() throws IOException {

    bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
    String result="", line;

    while((line = bufferedReader.readLine()) != null){
        result += line;
    }

    return result;
}
public void close() throws IOException {
            bufferedWriter.close();
            outputStream.close();
            bufferedReader.close();
            inputStream.close();
        }
非工作代码:

private HttpURLConnection httpURLConnection;
private URL url;
private InputStream inputStream;
private BufferedReader bufferedReader;
private BufferedWriter bufferedWriter;
private OutputStream outputStream;

public Connection(String login_url) throws IOException {

    url = new URL(login_url);
    httpURLConnection = (HttpURLConnection)url.openConnection();
    httpURLConnection.setRequestMethod("POST");
    httpURLConnection.setDoOutput(true);
    httpURLConnection.setDoInput(true);

}

public void write(String post_data) throws IOException {
    outputStream = httpURLConnection.getOutputStream();
    bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
    bufferedWriter.write(post_data);
    bufferedWriter.flush();
}

public String read() throws IOException {
    inputStream = httpURLConnection.getInputStream();
    bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
    String result="", line;

    while((line = bufferedReader.readLine()) != null){
        result += line;
    }

    return result;
}

public void close() throws IOException {
        bufferedWriter.close();
        outputStream.close();
        bufferedReader.close();
        inputStream.close();
    }
private HttpURLConnection httpURLConnection;
private URL url;
private InputStream inputStream;
private BufferedReader bufferedReader;
private BufferedWriter bufferedWriter;
private OutputStream outputStream;

public Connection(String login_url) throws IOException {

    url = new URL(login_url);
    httpURLConnection = (HttpURLConnection)url.openConnection();
    httpURLConnection.setRequestMethod("POST");
    httpURLConnection.setDoOutput(true);
    httpURLConnection.setDoInput(true);

    outputStream = httpURLConnection.getOutputStream();
    inputStream = httpURLConnection.getInputStream();

}

public void write(String post_data) throws IOException {

    bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
    bufferedWriter.write(post_data);
    bufferedWriter.flush();
}

public String read() throws IOException {

    bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
    String result="", line;

    while((line = bufferedReader.readLine()) != null){
        result += line;
    }

    return result;
}
public void close() throws IOException {
            bufferedWriter.close();
            outputStream.close();
            bufferedReader.close();
            inputStream.close();
        }
用法:

public class LoginBackground extends AsyncTask<String,Void,String> {
protected Context context;
private AlertDialog alertDialog;
private String login_url = "http://192.168.1.195/login.php";

public LoginBackground(Context ctx){
    context = ctx;
}

@Override

protected String doInBackground(String... params){
        try{
            String user = params[0];
            String pass = params[1];

            Connection conn = new Connection(login_url);

            String post_data = URLEncoder.encode("user","UTF-8")+"="+URLEncoder.encode(user,"UTF-8")+
                    "&"+URLEncoder.encode("pass","UTF-8")+"="+URLEncoder.encode(pass,"UTF-8");

            conn.write(post_data);

            String result = conn.read();

            conn.close();

            return result;

        } catch(MalformedURLException e){
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }

    return null;
}
@Override
protected void onPreExecute(){

}

@Override
protected void onPostExecute(String result){

    if(result.equals("Login successful")){
        Intent homeIntent = new Intent(context, HomeActivity.class);
        context.startActivity(homeIntent);
    }
    else{
        alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Alert");
        alertDialog.setMessage(result);
        alertDialog.show();
    }

}

protected void onProgressUpdate(Void... values){
    super.onProgressUpdate(values);
}

} 

错误是什么?发布一些代码,说明如何使用这些对象。这些流、读卡器和写卡器是如何关闭的?@AndrewS done..只是猜测调用
httpUrlConnection.getInputStream()
正在准备读取响应,因此实现可能试图尽可能高效,并在假设写入步骤(POST)已完成的情况下关闭输出流。请尝试查找具体的
HttpUrlConnection
实现的源代码,并查看“getInputStream()”代码的内容。错误是什么?发布一些代码,说明如何使用这些对象。这些流、读卡器和写卡器是如何关闭的?@AndrewS done..只是猜测调用
httpUrlConnection.getInputStream()
正在准备读取响应,因此实现可能试图尽可能高效,并在假设写入步骤(POST)已完成的情况下关闭输出流。尝试查找具体的
HttpUrlConnection
实现的源代码,并查看“getInputStream()”代码是什么。