大神来帮我看看把

xiaolv0 2012-08-02 01:31:04
为什么第二个try的BufferedReader reader=new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
这句和is.close();这句话会出错呢
try{
HttpClient httpclient=new DefaultHttpClient();
HttpPost httppost=new HttpPost("http://110.86.69.250/getAllPeopleBornAfter.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response =httpclient.execute(httppost);
HttpEntity entity=response.getEntity();
InputStream is=entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection" + e.toString());
}
try{
BufferedReader reader=new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb=new StringBuilder();
String line =null;
while((line=reader.readLine())!=null){
sb.append(line+"\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag","Error converting result" + e.toString());
}
...全文
54 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
AMinfo 2012-08-02
  • 打赏
  • 举报
回复
或者将is在try外部定义,这样2个try都可以用。

InputStream is;
try{
HttpClient httpclient=new DefaultHttpClient();
HttpPost httppost=new HttpPost("http://110.86.69.250/getAllPeopleBornAfter.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response =httpclient.execute(httppost);
HttpEntity entity=response.getEntity();
is=entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection" + e.toString());
}
try{
BufferedReader reader=new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb=new StringBuilder();
String line =null;
while((line=reader.readLine())!=null){
sb.append(line+"\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag","Error converting result" + e.toString());
}
AMinfo 2012-08-02
  • 打赏
  • 举报
回复
将2个try合并成一个,例如

try{
HttpClient httpclient=new DefaultHttpClient();
HttpPost httppost=new HttpPost("http://110.86.69.250/getAllPeopleBornAfter.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response =httpclient.execute(httppost);
HttpEntity entity=response.getEntity();
InputStream is=entity.getContent();
BufferedReader reader=new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb=new StringBuilder();
String line =null;
while((line=reader.readLine())!=null){
sb.append(line+"\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error in http connection" + e.toString());
}
xiaolv0 2012-08-02
  • 打赏
  • 举报
回复
我上面一个try定义了is了,下面就调用不了了吗?
AMinfo 2012-08-02
  • 打赏
  • 举报
回复
你都没有定义is这个对象,当然就出错了。

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧