Android 초보

HttpURLConnection getInputStream FileNotFoundException

돌비 2014. 7. 4. 13:02

    try

    {

      // 서버 연결

      URL url = new URL(this.sURL);

      HttpURLConnection conn = (HttpURLConnection)url.openConnection();

      conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");

      conn.setRequestMethod("POST");         // 전송 방식은 POST

      conn.setDefaultUseCaches(false);

      conn.setDoInput(true);                 // 서버에서 읽기 모드 지정

      conn.setDoOutput(true);                // 서버로 쓰기 모드 지정

      conn.connect();

 

      if(htPostData != null)

      {

        // 전송할 데이타 준비

        StringBuffer sbData = new StringBuffer();

        String sKey = null;

        String sValue = null;

        Enumeration e = htPostData.keys();

        while (e.hasMoreElements()) {

          sKey = (String) e.nextElement();

          sValue = (String) htPostData.get(sKey);

 

          sbData.append(sKey + "=" + sValue + "&");

          lib_common.Log("sKey=[" + sKey + "] sValue=[" + sValue + "]");

        }

 

        // 데이타 전송

        OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");

        PrintWriter pw = new PrintWriter(osw);

        pw.write(sbData.toString());

        pw.flush();

      }

 

      InputStream is = null;

      int iResponseCode = conn.getResponseCode();

      lib_common.Log("*** lib_post... response_code = [" + iResponseCode + "]");

 

      // 서버응답 받음

      InputStreamReader isr = null;

      if(iResponseCode >= HttpStatus.SC_BAD_REQUEST)

        isr = new InputStreamReader(conn.getErrorStream(), "UTF-8");

      else

        isr = new InputStreamReader(conn.getInputStream(), "UTF-8");

      BufferedReader br = new BufferedReader(isr);

      sbReturnValue = new StringBuilder();

      String s = "";

      while((s = br.readLine()) != null)

      {

        sbReturnValue.append(s+"\n");

      }

    }

    catch(Exception exp)

    {

      lib_common.Log("*** lib_post... onInBackgroun..."+exp.toString());

    }

 

    lib_common.Log("*** lib_post... onInBackgroun... return value=["+sbReturnValue.toString()+"]");

    return null;

반응형