Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我的android应用程序在发送json时未收到服务器的任何响应_Php_Android_Mysql_Json - Fatal编程技术网

Php 我的android应用程序在发送json时未收到服务器的任何响应

Php 我的android应用程序在发送json时未收到服务器的任何响应,php,android,mysql,json,Php,Android,Mysql,Json,我正在尝试使用php webservice将Json数据从mysql发送到android应用程序 但是我没有从服务器上得到任何响应,我确信从我到webservice的路径上。 这是我的php代码 <?php header("Content-Type: application/json;charset=utf-8"); $host = 'localhost'; $uname = 'root'; $pwd = ''; $db = 'test';

我正在尝试使用php webservice将Json数据从mysql发送到android应用程序 但是我没有从服务器上得到任何响应,我确信从我到webservice的路径上。 这是我的php代码

<?php
    header("Content-Type: application/json;charset=utf-8");
    $host = 'localhost';
    $uname = 'root';
    $pwd = '';
    $db = 'test';
    $flag = array();
    $con = mysqli_connect($host, $uname, $pwd,$db) or die('Connection Failed');
    mysqli_query($con,"SET NAME 'utf8'");
    mysqli_query($con,'SET CHARACTER SET utf8');
    $query = mysqli_query($con,"SELECT BRANCH,SUBSCRIBER,REGION,LOCATION_NO,ORG,TYPE,FOR_MONTH,PHASE_TYPE,NAME,KW_COUNTER_NO
         ,KW_LAST_READ,MULTIPLY_FACTOR,KV_COUNTER_NO,KV_LAST_READ,KV_MULTIPLY_FACTOR,KW_OPSERVER_1_LAST_READ,OP_MULTIPLY_FACTOR,
         KW_OPSERVER_2_LAST_READ,KW_OPSERVER_3_LAST_READ,NOTES,Is_Auto_Pay,Counter_Read_Digit,M_B,AMBIR,IS_NEW_COUNTER FROM last_reads");

    while($row = mysqli_fetch_array($query)) {

        $flag[] = $row;

    }

    echo json_encode($flag,JSON_UNESCAPED_UNICODE);
    mysqli_close($con);
?>

广告这里是我的安卓代码

private void webservice() {
        // TODO Auto-generated method stub
        new TheTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

    }
class TheTask extends AsyncTask<String,String,String>
    {

        @Override
        protected void onPostExecute(String result) {
            pb.setVisibility(View.GONE);
            Toast.makeText(getApplicationContext(), "command sent", Toast.LENGTH_LONG).show();
            resumeWebService();

        }
        protected void onProgressUpdate(Integer... progress){
            pb.setProgress(progress[0]);
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
        }

        @Override
        protected String doInBackground(String... params) {
            try {

                Log.d("1","1");
                HttpClient httpClient = new DefaultHttpClient();
                Log.d("1", "2");
                HttpPost httpPost = new HttpPost(ip);
                Log.d("1","3");
                HttpResponse response = httpClient.execute(httpPost);
                Log.d("1","4");
                HttpEntity entity = response.getEntity();
                Log.d("","length"+response.getEntity().getContentLength()); //return 0 !

                is = entity.getContent();
                Log.d("1","5");

            }
            catch (Exception e) {
                Log.e("Webservice 1", e.toString());
            }
            return "";
        }
private void webservice(){
//TODO自动生成的方法存根
新建任务().executeOnExecutor(异步任务.THREAD\u POOL\u EXECUTOR);
}
类任务扩展异步任务
{
@凌驾
受保护的void onPostExecute(字符串结果){
pb.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(),“已发送命令”,Toast.LENGTH_LONG.show();
resumeWebService();
}
受保护的void onProgressUpdate(整数…进度){
pb.setProgress(progress[0]);
}
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
试一试{
日志d(“1”、“1”);
HttpClient HttpClient=新的DefaultHttpClient();
日志d(“1”、“2”);
HttpPost HttpPost=新的HttpPost(ip);
日志d(“1”、“3”);
HttpResponse response=httpClient.execute(httpPost);
日志d(“1”、“4”);
HttpEntity=response.getEntity();
Log.d(“,”length“+response.getEntity().getContentLength());//返回0!
is=entity.getContent();
日志d(“1”、“5”);
}
捕获(例外e){
Log.e(“webservice1”,e.toString());
}
返回“”;
}

有人能告诉我问题出在哪里吗?

通过在doInBackground调用resumeWebService()函数来解决问题resumeWebService()代码是


它应该在doInBackground而不是onPostExecute()方法中

您的web服务在哪里?请尝试先在postman中运行您的URL..并检查是否有响应。我尝试了它,它的工作正常,并响应响应
 try {

                BufferedReader reader = new BufferedReader(new InputStreamReader(is,  "utf-8" ), 8);
                StringBuilder sb = new StringBuilder();

                int i = 0;
                while((line = reader.readLine()) != null) {
                    Log.d("readline","rr"+i++);
                    sb.append(line + "\n");
                }

                is.close();
                resultSb = sb.toString();
                Log.d("sb",""+resultSb);
            }
            catch (Exception e) {
                Log.e("Webservice 2", e.toString());
            }