我看了一些论坛帖子,但没有找到解决我的问题的答案。我正在尝试从一个 php 文件中获取响应。该 php 文件是正常工作的。问题在于 Android 应用程序无法执行我的请求。以下是我的代码示例和文本视图中获得的结果:
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());
}
}
文本视图显示:文本已更改
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());
}
}
这个TextView显示的是:文本已更改连接
在这个清单文件中,我有:
<uses-permission android:name="android.permission.INTERNET" />
在错误日志中我看到如下信息: 网络连接错误 android.os.NetworkOnMainThreadException
需要帮助请告知。