0

所以,我的 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);。我错在哪里?

PS - 我在 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" />
4

1 回答 1

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 }
于 2015-06-25T15:07:47.453 回答