【发布时间】:2015-05-27 01:54:57
【问题描述】:
我需要从 php web 服务获取信息,但是当我在 android 中执行 HttpPost 时 应用程序在 HttpResponse response = httpclient.execute(httppost); 中中断了 jusrt;
这是我的安卓代码
public String getHttp(){
String jsonResult ="";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://android.0fees.us/escuela.php");
try {
HttpResponse response = httpclient.execute(httppost);
jsonResult = convertStreamtoString(
response.getEntity().getContent()).toString();
}
catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jsonResult;
}
private String convertStreamtoString(InputStream resultado) throws IOException{
if(resultado != null){
StringBuilder sb = new StringBuilder();
String con = "";
try{
BufferedReader br = new BufferedReader(
new InputStreamReader(resultado, "UTF-8"));
while((con = br.readLine())!= null){
sb.append(con).append("'n");
}
}finally{
resultado.close();
}
return sb.toString();
}
else{
return "";
}
}
这是我的 PHP 代码
<?php
include ('conex.php');
$result = mysql_query("Select nombre FROM escuela") or die("No se realizo");
$json = array();
if(mysql_num_rows($result)){
while ($row = mysql_fetch_assoc($result)) {
$json[]= $row;
}
}
echo json_encode($json);
?>
希望你能帮帮我,谢谢
【问题讨论】:
-
应用中断是什么意思?你有例外吗?
-
是的,我遇到了一个关闭我的应用程序的异常
-
好的,有什么异常?你为什么不发布一些日志输出,也许是堆栈跟踪?
-
好吧,我搞不明白,当我执行方法时不返回任何有关它的信息,我的意思是应用程序刚刚结束
-
你在使用 Logcat 吗?