private String getTestContent(String url) {
HttpGet httpRequest = new HttpGet(url);
String result = null;
BufferedReader br = null;
InputStream is = null;
try {
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
int response = httpResponse.getStatusLine().getStatusCode();
if (response == 200) {
HttpEntity entity = httpResponse.getEntity();
StringBuffer sb = new StringBuffer();
is = entity.getContent();
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String data = "";
boolean found = false;
while ((data = br.readLine()) != null) {
if(data != null && data.indexOf(TEST_CONTENT_START_TAG) != -1) {
found = true;
}
else {
if (found) {
if ((data!=null) && (data.indexOf(TEST_CONTENT_END_TAG)!=-1)) {
found = false;
break;
}
sb.append(data);
sb.append("\n");
}
}
}
result = sb.toString();
return result;
} else {
return result;
}
} catch (ClientProtocolException e) {
return null;
} catch (HttpHostConnectException e) {
return null;
} catch (UnknownHostException e) {
return null;
} catch (IOException e) {
return null;
} catch (Exception e) {
return null;
} finally {
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
HttpGet httpRequest = new HttpGet(url);
String result = null;
BufferedReader br = null;
InputStream is = null;
try {
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
int response = httpResponse.getStatusLine().getStatusCode();
if (response == 200) {
HttpEntity entity = httpResponse.getEntity();
StringBuffer sb = new StringBuffer();
is = entity.getContent();
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String data = "";
boolean found = false;
while ((data = br.readLine()) != null) {
if(data != null && data.indexOf(TEST_CONTENT_START_TAG) != -1) {
found = true;
}
else {
if (found) {
if ((data!=null) && (data.indexOf(TEST_CONTENT_END_TAG)!=-1)) {
found = false;
break;
}
sb.append(data);
sb.append("\n");
}
}
}
result = sb.toString();
return result;
} else {
return result;
}
} catch (ClientProtocolException e) {
return null;
} catch (HttpHostConnectException e) {
return null;
} catch (UnknownHostException e) {
return null;
} catch (IOException e) {
return null;
} catch (Exception e) {
return null;
} finally {
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
解决方案 »
- 【超级菜鸟问题】怎样在原有的目录下面增加一个xxx.java文件并编译它
- eclipse工具点击DDMS里面没有内容
- android怎么通过某键盘按键实现输入法(不同应用/不同语言)的来回切换
- android service中的Thread能使用Service中的数据么?
- 有没人知道如何通过gsm/gprs获得地理位置啊?
- 新手学习android~求帮助~
- 点击ListView中的item时没有反映
- 求指点 为什么这个json数组安卓客户端接收不到
- Android 怎样接收十六进制数据?急!!!
- andriod studio怎么快捷跳到下一行,不用光标移到末尾
- 急急急----PAD端如何正确的显示WORD和EXCEL表单的样式
- android setBackGround()方法不兼容4.1以下版本
因为线程直接操作UI不安全,现在的语言都不支持在线程中操作UI!