如何在我的Android应用程序上打印PHP echo?

时间:2014-03-04 16:43:12

标签: php android bufferedinputstream

我是Android和PHP编程的新手,我目前遇到的问题是从我的php页面打印我的echo语句,这很简单:

    <?php
    echo "Hey there response!";
    ?>

我目前在MainActivity中使用的是:

        setContentView(R.layout.activity_main);

    TextView txtView = (TextView) findViewById(R.id.txt);

    //ASYNC WAY
    new GetData(txtView).execute("");

AsyncTask定义于:

private class GetData extends AsyncTask<String, Void, String>{
    private TextView display;


    GetData(TextView view){
        this.display = view;
        display = (TextView) findViewById(R.id.txt);
    }

    protected String doInBackground(String... message){
        HttpClient httpclient;
        HttpGet request;
        HttpResponse response = null;
        String result = "error0";
        try{
            httpclient = new DefaultHttpClient();
            request = new HttpGet("http://localhost/php/wamp.php");
            response = httpclient.execute(request);
        } catch (Exception e){
            result = "error1";
        }

        try{
            BufferedReader rd = new BufferedReader(new InputStreamReader(
                    response.getEntity().getContent()));
            String line="";
            while((line = rd.readLine()) != null){
                result = result + line;
            }
        } catch(Exception e){
            result = "error2";
        }
        return result;
    }

    protected void onPostExecute(String result){
        this.display.setText(result);
    }
}

txtView中的消息变为error2,我不明白为什么。

修改 我最初使用非AsyncTask方法读取输入流,但由于网络错误,我已切换到AsyncTask。问题仍然存在,因为我没有在我的应用程序中获得正确的回声。

3 个答案:

答案 0 :(得分:1)

这对于yanki来说可能为时已晚,但对于遇到此页面的其他任何人来说都是如此。

我用yanki的代码尝试了它并得到了与他“错误2”相同的错误。我注意到我没有更新清单文件以允许互联网权限。

 <uses-permission android:name="android.permission.INTERNET" />

我又跑了一次,效果很好。因此,使用yanki的代码并在清单文件中输入Internet权限,该程序应该可以工作。

答案 1 :(得分:0)

试试这个, 问题是你是以字节形式读取它,而不是将其转换回char

private String readStream(String url) {
        try {
                  HttpClient httpclient = new DefaultHttpClient();
                  HttpPost httppost = new HttpPost(Url);
                  HttpResponse response = httpclient.execute(httppost);
                  HttpEntity entity = response.getEntity();
                  InputStream is = entity.getContent();
                  String result = "";

    // convert response to string
                  new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
                  StringBuilder sb = new StringBuilder();

                  InputStreamReader r = new InputStreamReader(is, "UTF-8");
                  int intch;
                  while ((intch = r.read()) != -1) {
                    char ch = (char) intch;
                    // Log.i("app", Character.toString(ch));
                    String s = new String(Character.toString(ch).getBytes(), "UTF-8");
                    sb.append(s);
                    }
                    is.close();
                    result = sb.toString();
             } catch (Exception e) {
        Log.e(TAG, "Error converting result " + e.toString());

    }

}

答案 2 :(得分:0)

为什么不直接使用JSON或XML发送您想要在Android应用中显示的任何值...尝试这些try these