如何从android

时间:2016-07-16 11:09:27

标签: android httprequest httpresponse

我创建了一个Android应用程序,我在这里发出了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) {

}

}

这里我想在返回字符串构建器后显示错误和成功消息。

1 个答案:

答案 0 :(得分: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
    }