Php Post FATAL EXCEPTION - 包含异步和权限

时间:2014-11-29 12:02:26

标签: android android-asynctask

以下代码不断出错。我从here获取了此代码,似乎没问题。问题是我已经检查了stackoverflow中关于错误的每个主题。完成添加互联网权限和互联网状态权限。也正确使用AsyncTask类。我会在这里添加整个错误。当我按下按钮时会弹出这些错误:

public class MainActivity extends Activity implements OnClickListener {

private EditText value;
private Button btn;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    value = (EditText) findViewById(R.id.edit1);
    btn = (Button) findViewById(R.id.btn);
    btn.setOnClickListener(this);
}


public void onClick(View v) {
    // TODO Auto-generated method stub
    if (value.getText().toString().length() < 1) {

        // out of range
        Toast.makeText(this, "please enter something", Toast.LENGTH_LONG).show();
    } else {
        new MyAsyncTask().execute(value.getText().toString());
    }


}

private class MyAsyncTask extends AsyncTask<String, Integer, Double> {

    @Override
    protected Double doInBackground(String... params) {
        // TODO Auto-generated method stub
        postData(params[0]);
        return null;
    }

    protected void onPostExecute(Double result) {
        Toast.makeText(getApplicationContext(), "command sent", Toast.LENGTH_LONG).show();
    }

    protected void onProgressUpdate(Integer... progress) {
    }

    public void postData(String valueIWantToSend) {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://somewebsite.com/receiver.php");

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("myHttpData", valueIWantToSend));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    }

}

}

和错误

11-29 07:21:11.958    1856-1869/com.example.nova.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.example.nova.myapplication, PID: 1856
java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:300)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
        at java.util.concurrent.FutureTask.run(FutureTask.java:242)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:841)
 Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=erdemsimsek.info/deneme.php
        at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)
        at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
        at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
        at com.example.nova.myapplication.MainActivity$MyAsyncTask.postData(MainActivity.java:81)
        at com.example.nova.myapplication.MainActivity$MyAsyncTask.doInBackground(MainActivity.java:58)
        at com.example.nova.myapplication.MainActivity$MyAsyncTask.doInBackground(MainActivity.java:53)
        at android.os.AsyncTask$2.call(AsyncTask.java:288)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)

清单文件:     

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name="com.example.nova.myapplication.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>s

2 个答案:

答案 0 :(得分:1)

这样做,

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example"
    android:versionCode="1"
    android:versionName="1.0" >

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name="com.example.nova.myapplication.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

现在做,

String valueIWantToSend;

 public void onClick(View v) {
    // TODO Auto-generated method stub
    if (value.getText().toString().length() < 1) {

        // out of range
        Toast.makeText(this, "please enter something", Toast.LENGTH_LONG).show();
    } else {
      valueIWantToSend = value.getText().toString();
      new MyAsyncTask().execute();
    }



@Override
    protected Double doInBackground(String... params) {
        // TODO Auto-generated method stub
        postData();
        return null;
    }




public void postData() {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://somewebsite.com/receiver.php");

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("myHttpData", valueIWantToSend));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    }

答案 1 :(得分:0)

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name="com.example.nova.myapplication.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

尝试移动外部的权限&#39;应用程序标签