I've look at a few forum post and I can not find an answer to my problem. I am trying to get a response from a php file. The php file is working. The problem is the Android App will not execute my request. Here are two examples of my code and the result I get in the textview:
public void changText(View view) {
TextView textv = (TextView)findViewById(R.id.textview1);
textv.setText("Text Has Been Changed");
BufferedReader in = null;
String data = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet();
URI website = new URI("http://alanhardin.comyr.com/matt24/matt28.php");
request.setURI(website);
HttpResponse response = httpclient.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
textv.append(" Connected ");
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
}
The TextView reads: Text Has Been Changed
public void changText(View view) {
TextView textv = (TextView)findViewById(R.id.textview1);
textv.setText("Text Has Been Changed");
BufferedReader in = null;
String data = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet();
URI website = new URI("http://alanhardin.comyr.com/matt24/matt28.php");
request.setURI(website);
//HttpResponse response = httpclient.execute(request);
//in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
textv.append(" Connected ");
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
}
The TextView reads: Text Has Been Changed Connected
In this manifest I have:
<uses-permission android:name="android.permission.INTERNET" />
In the error log I get the following:
Error in http connection android.os.NetworkOnMainThreadException
Any help would be appreciated.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…