将包含空格的字符串作为响应从php发送到Android

时间:2019-04-13 06:03:37

标签: php android python android-asynctask

我想将包含空格的字符串从php发送到Android Studio。我正在使用此onPostExecute(String result)处理包含空格的字符串响应,但是它返回句子的第一个单词。

这是我的代码-

public class PostData extends AsyncTask<String, Void, String>{
        @Override
        protected  String doInBackground(String... args){
             byte[] result = null;
            String str = " ";
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://192.168.42.98:81/launch2.php");

            try{
                List<NameValuePair> nameValuePairs = new ArrayList<>(1);
                nameValuePairs.add(new BasicNameValuePair("input",str_input ));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                HttpResponse response = httpclient.execute(httppost);
                StatusLine statusLine = response.getStatusLine();
                if(statusLine.getStatusCode()== HttpURLConnection.HTTP_OK){
                    result = EntityUtils.toByteArray(response.getEntity());
                    str = new String(result, "UTF-8");

                }
            }catch(ClientProtocolException e){
                //TODO
                tv_result.setText(e.getMessage());
            }
            catch(IOException e){
                //TODO
                tv_result.setText(e.getMessage());
            }
            return str;
        }
        protected void onPostExecute(String result){
            for(String word : result.split(" ")) {
                tv_result.setText(word);
            }
        }
    } 

这是我的php代码-

<?php
    $var= $_POST["input"];
    $output= exec("python C:\Users\hp\AppData\Local\Programs\Python\Python36\Stem.py .$var");
    echo $output;
?>

0 个答案:

没有答案