2
我想在android设备上读取某些日期,我已经加载到物理服务器上的mysql数据库中。 提取数据的数据库和php脚本都可以。 问题是android编码方面。当从PHP服务器GET'ing数据时,应用程序崩溃加载
public class MainActivity extends Activity {
public HttpResponse response;
public String str;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("/*phpAddressIsCorrect*/");
try {
response = httpclient.execute(httppost);
}
catch(IOException e) {
e.printStackTrace();
}
try {
str = EntityUtils.toString(response.getEntity());
} catch(IOException e){
e.printStackTrace();
}
Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show();
}
}
的main.xml
只是一个RelativeLayout
。
使用互联网的android权限,应用程序在加载时崩溃。我想这是因为IOException,不是吗?
感谢了很多,所以我要创建主○startIntentService它是一个backgroundThread? – 2015-02-01 13:05:57
我发布的线程在后台执行。所以,只需复制我的答案,并做你的解析东西等我写了三个点。请记住,您无法更改另一个线程中的UI(在此背景线程内创建[runOnUiThread](http://stackoverflow.com/a/27992603/4193263))。另外请记住,网络需要一些时间,如果您从onClickListener中读取它们,您的变量可能未初始化。 – ByteHamster 2015-02-01 13:26:12
非常感谢您的帮助,非常有用! – 2015-02-05 18:59:57