简体   繁体   English

我的 android 应用程序在发送 json 时没有收到来自服务器的任何响应

[英]My android app didnt receive any response from server while sending json

Im trying to send Json data from mysql to android app using php webservice but I didn't get any response from the server and I'm sure from my path to webservice.我正在尝试使用 php webservice 将 Json 数据从 mysql 发送到 android 应用程序,但我没有得到服务器的任何响应,我确定从我的路径到 webservice。 here is my php code这是我的 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);
?>

ad here is my android code广告这里是我的 android 代码

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 "";
        }

can anybody tell me what the problem is?谁能告诉我问题是什么?

Isolve the problem by calling the resumeWebService() function at doInBackground the resumeWebService() code is通过在 doInBackground 调用 resumeWebService() 函数来解决问题,resumeWebService() 代码是

 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());
            }

and it should be in doInBackground not in onPostExecute() method它应该在 doInBackground 中而不是在 onPostExecute() 方法中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM