发送json时,我的Android应用程序没有从服务器收到任何响应

时间:2015-10-23 11:41:21

标签: php android mysql json

我试图使用php webservice将Json数据从mysql发送到Android应用程序 但我没有从服务器得到任何回复,我确信从我的网络服务的路径。 这是我的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);
?>

这里的广告是我的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 "";
        }

任何人都可以告诉我这是什么问题吗?

1 个答案:

答案 0 :(得分:0)

通过调用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());
            }

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