我有这个脚本。

我经历了堆栈溢出问题的许多变化,并使用该解决方案尝试建立知识来做到这一点,但似乎每次都失败了,有人可以帮忙吗?

public class Main extends Activity implements OnClickListener {

    private EditText value;
    private Button btn;
    private ProgressBar pb;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        value = (EditText) findViewById(R.id.editText1);
        btn = (Button) findViewById(R.id.button1);
        pb = (ProgressBar) findViewById(R.id.progressBar1);
        pb.setVisibility(View.GONE);
        btn.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    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 {
            pb.setVisibility(View.VISIBLE);
            new MyAsyncTask().execute("hey");
        }

    }

    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) {
            pb.setVisibility(View.GONE);
            Toast.makeText(getApplicationContext(), "command sent",
                    Toast.LENGTH_LONG).show();
        }

        protected void onProgressUpdate(Integer... progress) {
            pb.setProgress(progress[0]);
        }

        public void postData(String valueIWantToSend) {
            // Create a new HttpClient and Post Header
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(
                    "http://users.aber.ac.uk/bym1/group/androidto.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
            }
        }
    }
}

我在网上找到了这段代码,我只是想让它工作,所以我有一个工作原型(prototype)可以工作并构建我自己的,但我一直在我的日志文件中得到这个:
    01-28 22:30:07.030: W/dalvikvm(27142): threadid=11: thread exiting with uncaught exception (group=0x40bc2498)
01-28 22:30:07.030: E/test(27142): Exception
01-28 22:30:07.060: E/AndroidRuntime(27142): FATAL EXCEPTION: AsyncTask #1
01-28 22:30:07.060: E/AndroidRuntime(27142): java.lang.RuntimeException: An error occured while executing doInBackground()
01-28 22:30:07.060: E/AndroidRuntime(27142):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-28 22:30:07.060: E/AndroidRuntime(27142):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-28 22:30:07.060: E/AndroidRuntime(27142):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-28 22:30:07.060: E/AndroidRuntime(27142):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-28 22:30:07.060: E/AndroidRuntime(27142):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-28 22:30:07.060: E/AndroidRuntime(27142):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-28 22:30:07.060: E/AndroidRuntime(27142):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-28 22:30:07.060: E/AndroidRuntime(27142):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-28 22:30:07.060: E/AndroidRuntime(27142):    at java.lang.Thread.run(Thread.java:856)
01-28 22:30:07.060: E/AndroidRuntime(27142): Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)
01-28 22:30:07.060: E/AndroidRuntime(27142):    at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
01-28 22:30:07.060: E/AndroidRuntime(27142):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
01-28 22:30:07.060: E/AndroidRuntime(27142):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
01-28 22:30:07.060: E/AndroidRuntime(27142):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
01-28 22:30:07.060: E/AndroidRuntime(27142):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-28 22:30:07.060: E/AndroidRuntime(27142):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-28 22:30:07.060: E/AndroidRuntime(27142):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-28 22:30:07.060: E/AndroidRuntime(27142):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-28 22:30:07.060: E/AndroidRuntime(27142):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-28 22:30:07.060: E/AndroidRuntime(27142):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-28 22:30:07.060: E/AndroidRuntime(27142):    at com.example.httpasync.Main$MyAsyncTask.postData(Main.java:98)
01-28 22:30:07.060: E/AndroidRuntime(27142):    at com.example.httpasync.Main$MyAsyncTask.doInBackground(Main.java:70)
01-28 22:30:07.060: E/AndroidRuntime(27142):    at com.example.httpasync.Main$MyAsyncTask.doInBackground(Main.java:1)
01-28 22:30:07.060: E/AndroidRuntime(27142):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-28 22:30:07.060: E/AndroidRuntime(27142):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-28 22:30:07.060: E/AndroidRuntime(27142):    ... 5 more
01-28 22:30:07.060: E/AndroidRuntime(27142): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
01-28 22:30:07.060: E/AndroidRuntime(27142):    at libcore.io.Posix.getaddrinfo(Native Method)
01-28 22:30:07.060: E/AndroidRuntime(27142):    at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:55)
01-28 22:30:07.060: E/AndroidRuntime(27142):    at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
01-28 22:30:07.060: E/AndroidRuntime(27142):    ... 19 more
01-28 22:30:07.060: E/AndroidRuntime(27142): Caused by: libcore.io.ErrnoException: getaddrinfo failed: EACCES (Permission denied)
01-28 22:30:07.060: E/AndroidRuntime(27142):    ... 22 more

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.httpasync.Main"
            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>

设置了 Internet 权限,我查找了异常(exception)情况,但答案似乎与我的代码无关,有什么想法吗?

最佳答案

线索在logcat中:

Caused by: java.lang.SecurityException: Permission denied (missing INTERNET permission?)

您的 AndroidManifest 中缺少 android.permission.INTERNET 权限。添加这一行:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

略低于:
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

关于Android AsyncTask 通过 http Post 发送数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21418349/

10-10 10:12