Search code examples
phpandroidandroidhttpclient

Get PHP Echo in Android


I have some problems with Receiving PHP Echo message from an website. I have this code:

HttpClient httpclient=new DefaultHttpClient(); 
   HttpPost httppost=new  HttpPost("http://www.itbstudios.tk/test.php");
   HttpResponse response = httpclient.execute(httppost);
   String str =  EntityUtils.toString(response.getEntity());

I found this code and a lot more here on stackoverflow and is is not working. The app is crahsing on httpclient.execute();

Internet permission is set in android manifest.

Php code is just an <?php echo "test"; ?>

I'm building for API 15 and testing on HTC Device.

Can you guys help me? I even created an other project and paste the code in on create and i have the same problem.

Thanks!


Solution

  • HttpClient got some bugs in API higher than 10. Use HttpUrlConnection instead.

    GET method example:

    URL url = new URL("http://www.itbstudios.tk/test.php");
    HttpUrlConnection mUrlConnection = (HttpURLConnection) url.openConnection();
    mUrlConnection.setDoInput(true);
    
    InputStream is = new BufferedInputStream(mUrlConnection.getInputStream());
    String s = readStream(is);