Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 连接上未接受的流结束_Java_Android_Database_Httpurlconnection - Fatal编程技术网

Java 连接上未接受的流结束

Java 连接上未接受的流结束,java,android,database,httpurlconnection,Java,Android,Database,Httpurlconnection,我正在做一个Android应用程序,并试图将一个新数据输入到学生数据库中,这个连接是HttpURLConnection。我遇到了一个问题:java.io.IOException:Connection{…(这里是我的主机地址)上未接受的流结束 我认为主要的问题是conn.getOutputStream(),我已经检查了流是否关闭 public static void createUser(Student user){ //initialise URL url = null;

我正在做一个Android应用程序,并试图将一个新数据输入到学生数据库中,这个连接是HttpURLConnection。我遇到了一个问题:java.io.IOException:Connection{…(这里是我的主机地址)上未接受的流结束

我认为主要的问题是conn.getOutputStream(),我已经检查了流是否关闭

public static void createUser(Student user){
    //initialise
    URL url = null;
    HttpURLConnection conn = null;
    final String methodPath="/friendfinder.student/";
    try {
        Gson gson =new Gson();
        String stringUserJson=gson.toJson(user);
        url = new URL(BASE_URI + methodPath);
//open the connection
        conn = (HttpURLConnection) url.openConnection();
//set the timeout
        conn.setReadTimeout(10000);
        conn.setConnectTimeout(15000);
//set the connection method to POST
        conn.setRequestMethod("POST");
        //set the output to true
        conn.setDoOutput(true);
//set length of the data you want to send
        conn.setFixedLengthStreamingMode(stringUserJson.getBytes().length);
//add HTTP headers
        conn.setRequestProperty("Content-Type", "application/json");
//Send the POST out
        PrintWriter out= new PrintWriter(conn.getOutputStream());
        out.print(stringUserJson);
        out.close();
        Log.i("error",new Integer(conn.getResponseCode()).toString());
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        conn.disconnect();
    }
}

有人能看一下我的代码并给我一个建议吗?

哪一行抛出了错误?如果没有这些信息,可能很难帮助您…很抱歉,我忘了提到错误发生在e.printStackTrace(),e显示java.io.IOException:未接受的连接{….上的流结束。。。。(这是我的主机地址}。异常下面应该有一个堆栈跟踪。此信息对我们帮助您很重要。哪一行抛出错误?如果没有此信息,可能很难帮助您…非常抱歉,我忘了提到错误发生在e.printStackTrace()上,e显示java.io.IOException:Unceptied end of stream on Connection{…(这里是我的主机地址})。在异常下面应该有一个堆栈跟踪。此信息对于我们帮助您很重要。