How do I print PHP echo on my android app?

被刻印的时光 ゝ 提交于 2019-12-08 08:08:05

问题


I'm fairly new to android and PHP programming and I am currently running into a problem printing my echo statement from my php page, which is as simple as:

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

What I am currently using in my MainActivity is:

        setContentView(R.layout.activity_main);

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

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

Where AsyncTask is defined by:

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

The message in the txtView becomes error2, and I have no idea why.

EDIT I was originally using a non-AsyncTask method of reading in input stream but I have switch to AsyncTask due to Networking error. The problem still persists though since I am not getting the corrent echo in my application.


回答1:


This is probably too late for yanki but for anybody else that comes across this page.

I tried it with yanki's code and got the same error as him "error 2". I noticed then that I hadn't updated the manifest file to allow internet permissions.

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

I ran it again and it worked perfectly. So using yanki's code and entering the internet permissions in the manifest file the program should work.




回答2:


Try this, The problem was you are reading it as byte, you weren't converting it back to 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());

    }

}



回答3:


Why dont you jst use JSON or XML in sending whatever value you want to be displayed in your android app... try these try these



来源:https://stackoverflow.com/questions/22177821/how-do-i-print-php-echo-on-my-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!