2016-07-16 1 views
2

http 요청을 작성한 안드로이드 응용 프로그램을 만들었습니다. 어떻게 서버에서 응답을 읽고 오류 및 성공 메시지를 표시 할 수 있는지 알고 싶습니다. 스트링 빌더를 반환 한 후 : 여기안드로이드의 http 게시 객체에서 응답을 읽을 수있는 방법

 try{ 
      HttpResponse httpResponse = httpclient.execute(post); 
      InputStream inputStream = httpResponse.getEntity().getContent(); 

      InputStreamReader inputStreamReader = new InputStreamReader(inputStream); 

      BufferedReader bufferedReader = new BufferedReader(inputStreamReader); 

      StringBuilder stringBuilder = new StringBuilder(); 

      String bufferedStrChunk = null; 

      while((bufferedStrChunk = bufferedReader.readLine()) != null){ 
       stringBuilder.append(bufferedStrChunk); 
      } 
      return stringBuilder.toString(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } 

    //}catch(Exception e){ 
    // e.printStackTrace(); 
    //} 
    //return 0L; 
    return null; 
} 

protected void onProgressUpdate(Integer... progress) { 

} 

protected void onPostExecute(String result) { 

} 

}

내가 문자열 빌더를 반환 한 후에 오류 및 성공 메시지를 표시합니다.

+0

'StrictMode.ThreadPolicy policy'. 무서운. 해당 작업에서 모든 StrictMode 코드를 제거하십시오. 프로젝트에서 모든 StrictMode 코드를 제거하십시오. – greenapps

+0

가능한 경우 해당 섹션에 대한 코드를 제안하거나 보낼 수 있습니까 – Moulick

+0

왜? 코드가 제대로 작동하지 않습니까? 당신은 반환 된 페이지를 읽을 수있는 코드만을 제공했습니다. 따라서 지금 페이지를 받으면보고해야합니다. – greenapps

답변

2
     String response = null; 
         try { 
          response = SimpleHttpClient 
            .executeHttpPost("url", 
              postParameters); 
          res = response.toString(); 

          return res; 

         } catch (Exception e) { 
          e.printStackTrace(); 
          errorMsg = e.getMessage(); 
         } 
        } 
       }).start(); 
       try { 
        Thread.sleep(3000); 


       // error.setText(resp); 
        if (null != errorMsg && !errorMsg.isEmpty()) { 

        } 
       } catch (Exception e) { 
       } 

      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) 
     { 
      super.onPostExecute(result); 
      if(regDialog!=null) 
      { 

       regDialog.dismiss(); 

      //do you code here you want 

       } 

    // do what u do 
    } 
+0

@moulick 또한 json 문자열을 인코딩 할 필요가 없습니다. –

+0

List nameValuePairs = new ArrayList 를 해당 Private 클래스에 어떻게 포함해야합니까? 그게 아무것도 일어나지 않고있다 – Moulick

+0

내 업데이트 대답을 시도 ..... –