2013-10-09 47 views
0

在下面的Android代碼中,我從getInputStream中得到一個EOFException。getInputStream在發送JSON請求時拋出EOFException

private void downloadUrl() { 
    HttpsURLConnection conn = null; 
    DataOutputStream printout = null; 
    try { 
     try { 
      conn = (HttpsURLConnection) new URL("some url").openConnection(); 
      conn.setRequestMethod("POST"); 
      conn.setDoInput(true); 
      conn.setDoOutput(true); 
      conn.setUseCaches(false); 
      conn.setAllowUserInteraction(false); 
      conn.setRequestProperty("Accept", "application/json"); 
      conn.setRequestProperty("Content-Type", "application/json; charset=utf-8"); 
     } 
     catch (ProtocolException e){ 
     } 

     JSONObject jsonReq = new JSONObject(); 
     jsonReq.put("userID", "id"); 
     jsonReq.put("password", "1234"); 

     OutputStream output = conn.getOutputStream(); 
     try { 
      OutputStreamWriter wr = new OutputStreamWriter(output); 
      wr.write(URLEncoder.encode(jsonReq.toString(),"utf-8")); 
      wr.flush(); 
      wr.close(); 
     } 
     catch (IOException e) { 
     } 

     InputStream in = conn.getInputStream(); //I get EOFException here 
     try { 
      BufferedReader rd = new BufferedReader(new InputStreamReader(in)); 
      String responseSingle = null; 
      while ((responseSingle = rd.readLine()) != null) { 
       response = response + responseSingle; 
      } 
      rd.close(); 
     } 
     catch (IOException e) { 
     } 
     finally { 
      if (in != null) 
      in.close(); 
     } 
    } 
    catch (IOException e){ 
    } 
    catch (JSONException e) { 
    } 
    finally{ 
     if(conn!=null) 
      conn.disconnect(); 
    } 
} 

什麼是造成這種異常?

回答