【问题标题】:How can I read the response from the http post object in android如何从 android 中的 http post 对象读取响应
【发布时间】:2016-07-16 11:09:27
【问题描述】:

我已经创建了一个 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) {

}

}

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

【问题讨论】:

  • StrictMode.ThreadPolicy policy。糟糕的。从该任务中删除所有 StrictMode 代码。从您的项目中删除所有 StrictMode 代码。
  • 如果可能的话,您能否建议或发送该部分的代码
  • 为什么?你的代码工作不是吗?您只添加了一些代码来读取返回的页面。所以如果你现在得到这个页面,你应该报告。
  • 一切正常我已经添加了代码输入和输出流来获得响应,就好像发生了任何错误一样,所以它会告诉我错误是什么,它是为了得到响应,如果有任何虚假数据
  • 好吧,如果一切正常,那么您就准备好了。但是删除所有严格模式!!!

标签: android httprequest httpresponse


【解决方案1】:
                        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
    }

【讨论】:

  • 我应该如何在私有类中包含 List nameValuePairs = new ArrayList 因为没有它什么都不会发生
  • 您可以像添加 numvalue 一样在 Async tast 中添加 json
  • 很明白,但是太复杂了,RES 的注视点和 ERROEMESSAGE 说明在哪里
  • 非常糟糕的代码。不要在 doIBackground 中启动线程。 DoInBackground 已经是一个线程了。
  • 我想知道你为什么再次发布所有代码并添加了很多错误代码。原代码没问题。您只需按照要求添加阅读返回的页面。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-25
  • 1970-01-01
  • 2013-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多