所以,我在我的Activity中有这个代码(布局支持按钮的用户界面,触发方法onSaveRegistrationClick
):
public void onSaveRegistrationClick (View view){
postData();
}
public void postData(){
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.mywebsite.com");
try
{
httpclient.execute(httppost);
} catch (Exception e){
Toast.makeText(this, "Not working" , Toast.LENGTH_SHORT).show();
}
}
在我的网站上,我有index.php,它在电话中保存了一行,这有助于我确定Android是否连接到网站。但是,我在第httpclient.execute(httppost);
行获得了例外。我在哪里误会?
P.S。 - 我在AndroidManifest.xml中添加了权限(我添加了额外的,即:。
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
答案 0 :(得分:0)
错误是您必须使用线程而不是函数, 试试这个:
new MyTask().execute();
将其调用到流程
private class MyTask extends AsyncTask<Void, Void, Void> {
String textResult;
private Exception exception;
@Override
protected Void doInBackground(Void... voids)
{ ...your code }