title: Android HTTP-POST
body: <p>Good, not much English, but try to be as clear as possible. </p>
  
  <p>I am very newbie in java. I started a few days ago. I'm trying to develop a reading of a website a arhico php. But it does not work. Always returns me the catch </p>
  
  <p>I think we should do something with Thread, but i do not know how. </p>
  
  <p>I'm using Android studio. </p>
  
  <p>I expose the data that I have.</p>
  
  <p><strong>AndroidManifest.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.tuto1.app" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="8"
          android:maxSdkVersion="15"/&gt;
      &lt;uses-permission android:name="android.permission.INTERNETr"/&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name="com.example.tuto1.app.MainActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p><strong>Activity_main.xml</strong></p>
  
  <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  android:paddingBottom="@dimen/activity_vertical_margin"
  tools:context="com.example.tuto1.app.MainActivity"&gt;
  
  &lt;TextView
      android:text="@string/hello_world"
      android:layout_width="fill_parent"
      android:layout_height="200dp"
      android:background="#044234"
      android:textColor="#ffff"
      android:textSize="30sp"
      android:id="@+id/text1" /&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>package com.example.tuto1.app;
  
  import android.support.v7.app.ActionBarActivity;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.widget.TextView;
  
  public class MainActivity extends ActionBarActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
  
          httpHandler handler = new httpHandler();
          String txt = handler.post("http://demo.com/demo.php");
  
          TextView t = (TextView)findViewById(R.id.text1);
          t.setText(txt);
  
  
  
      }
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
  
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
          if (id == R.id.action_settings) {
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
  }
  </code></pre>
  
  <p><strong>httpHander.java (Class)</strong></p>
  
  <pre><code>package com.example.tuto1.app;
  
      import org.apache.http.HttpEntity;
      import org.apache.http.HttpResponse;
      import org.apache.http.client.HttpClient;
      import org.apache.http.client.methods.HttpPost;
      import org.apache.http.impl.client.DefaultHttpClient;
      import org.apache.http.util.EntityUtils;
  
  public class httpHandler {
  
      public String post(String posturl){
  
          try {
  
              HttpClient httpclient = new DefaultHttpClient();
              HttpPost httppost = new HttpPost(posturl);
  
              HttpResponse resp = httpclient.execute(httppost);
              HttpEntity ent = resp.getEntity();
  
              String text = EntityUtils.toString(ent);
  
              return text;
  
          }
          catch (Exception e) { return "mi error"; }
  
      }
  
  }
  </code></pre>
  
  <p><strong>updated The solution is this.</strong></p>
  
  <pre><code>    import android.os.AsyncTask;
  import android.support.v7.app.ActionBarActivity;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.widget.TextView;
  import org.apache.http.HttpEntity;
  import org.apache.http.HttpResponse;
  import org.apache.http.client.HttpClient;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.apache.http.util.EntityUtils;
  
  public class MainActivity extends ActionBarActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          class TheTask extends AsyncTask&lt;String,Void,String&gt;
          {
  
              @Override
              protected String doInBackground(String... arg0) {
                  String text =null;
                  try {
                      HttpClient httpclient = new DefaultHttpClient();
                      HttpPost httppost = new HttpPost(arg0[0]);
                      HttpResponse resp = httpclient.execute(httppost);
                      HttpEntity ent = resp.getEntity();
                      text = EntityUtils.toString(ent);
                  }
                  catch (Exception e)
                  {
                      e.printStackTrace();
                  }
  
                  return text;
              }
  
              @Override
              protected void onPostExecute(String result) {
                  super.onPostExecute(result);
  
                  TextView t = (TextView)findViewById(R.id.text1);
                  t.setText(result);
              }
  
          }
  
          new TheTask().execute("http://demo.com/php.php");
  
  
      }
  
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
  
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
          int id = item.getItemId();
          if (id == R.id.action_settings) {
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
  }
  </code></pre>