【问题标题】:How to get information from Web Service?如何从 Web Service 获取信息?
【发布时间】: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 吗?

标签: php android web service


【解决方案1】:

谢谢你帮助我。

我要解决它,我创建了一个新类,让我可以毫无问题地完成它,我只是扩展 AsyncTask,并将我的代码放在 doInBackground 中:

public class HttpRequest extends AsyncTask {

@Override
protected String doInBackground(Object[] objects) {
    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();
    }

    MainActivity.a = jsonResult;
    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 "";
    }
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-17
    • 1970-01-01
    • 2012-04-04
    • 2020-09-07
    相关资源
    最近更新 更多