Connection上未被截断的流结束

时间:2017-05-01 18:19:51

标签: java android database httpurlconnection

我正在做一个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();
    }
}

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

0 个答案:

没有答案