我'我试图通过HTTP请求从谷歌获取数据:
主要
public class MainActivity extends ActionBarActivity {
TextView httpStuff;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.httpexample);
httpStuff = (TextView) findViewById(R.id.httpText);
HTTP test = new HTTP();
String returned = null;
try {
returned = test.getInternetData();
httpStuff.setText(returned);
} catch (Exception e) {com
e.printStackTrace();
}
}
}
HTTP clas如下:
public class HTTP {
public String getInternetData() throws Exception{
BufferedReader in = null;
String data = null;
try{
HttpClient client = new DefaultHttpClient();
URI website = new URI("http://www.google.net");
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.separator");
while((l = in.readLine()) !=null){
sb.append(l + nl );
}
in.close();
data = sb.toString();
return data;
}finally {
if (in != null){
try{
in.close();
return data;
}catch (Exception e){
e.printStackTrace();
}
}
}
}
}
这是控制台logcat:
04-27 19:31:58.063 2167-2191 / com.example.briesanji.test D / OpenGLRenderer:渲染所请求的脏区:true 04-27 19:31:58.065 2167-2167 / com.example.briesanji.test D /:HostConnection :: get()建立新主机连接0xa687c600,tid 2167 04-27 19:31:58.103 2167-2167 / com.example.briesanji.test D / Atlas:验证地图...... 04-27 19:31:58.196 2167-2191 / com.example.briesanji.test D /:HostConnection :: get()建立新的主机连接0xa687c7c0,tid 2191 04-27 19:31:58.207 2167-2191 / com.example.briesanji.test I / OpenGLRenderer:初始化的EGL,版本1.4 04-27 19:31:58.283 2167-2191 / com.example.briesanji.test D / OpenGLRenderer:启用调试模式0 04-27 19:31:58.334 2167-2191 / com.example.briesanji.test W / EGL_emulation:eglSurfaceAttrib未实现 04-27 19:31:58.334 2167-2191 / com.example.briesanji.test W / OpenGLRenderer:无法在表面0xa6868780上设置EGL_SWAP_BEHAVIOR,错误= EGL_SUCCESS
TextView不显示数据但仍保留其默认文本("加载数据")
我曾尝试用httpStuff.setText(returned);
更改httpStuff.setText("hello");
,但没有任何内容,所以我认为这是布局问题,然后我从try方法中取出httpStuff.setText("hello");
并显示Hello ..所以问题是当然,尝试方法,但我不知道我错在哪里
答案 0 :(得分:0)
您的代码无效,因为尝试在其主线程上执行网络操作。因此,您会收到NetworkOnMainThreadException
解决此问题的最简单方法是使用AsyncTask
:
public class SampleActivity extends Activity {
TextView httpStuff;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
httpStuff = (TextView) findViewById(R.id.add_button);
new DownloadInternetData().execute();
}
public class DownloadInternetData extends AsyncTask<Void, Void, String>{
@Override
protected String doInBackground(Void... voids) {
BufferedReader in = null;
String data = null;
try {
HttpClient client = new DefaultHttpClient();
URI website = new URI("http://www.google.net");
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.separator");
while ((l = in.readLine()) != null) {
sb.append(l + nl);
}
in.close();
data = sb.toString();
return data;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
return data;
} catch (Exception e) {
e.printStackTrace();
}
}
}
return "";
}
@Override
protected void onPostExecute(String result) {
httpStuff.setText(result);
}
}
}