I've been searching for a long time to answer to my simple question but haven't found it yet.
I've just started Android Development and I can't manage to layout this simple XML to the Android App I have just created.
There is my code :
public class MainActivity extends Activity {
private static final String TAG = null;
/** Called when the activity is first created. */
private String getPage() {
String str = null ;
Log.v(TAG, "testentreemethode");
try
{
HttpClient hc = new DefaultHttpClient();
Log.v(TAG, "testnew");
HttpPost post = new HttpPost("http://www.3pi.tf/test.xml");
Log.v(TAG, "testurl");
HttpResponse rp = hc.execute(post);
Log.v(TAG, "testpost");
if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
str = EntityUtils.toString(rp.getEntity());
}
}catch(IOException e){
e.printStackTrace();
}
return str;
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView txt = (TextView) findViewById(R.id.textview1);
Log.v(TAG, "test1");
txt.setText(getPage());
Log.v(TAG, "test2");
}
}
As you can see I put some Logcat to see where the "cursor" goes and it can't pass this line:
HttpResponse rp = hc.execute(post);
Can someone help me please?
Network operation cannot be performed on the main thread. Use an AsyncTask to execute it on a seperate thread like this:
You can execute the task like this: