1

我是 Android 上的新手,我正在开发一个将一些信息发布到 php 脚本的应用程序,但是我遇到了太多异常。

这是我的代码:

 package com.example.testapp;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.ProgressBar;
import android.widget.Toast;

public class HttpTraitement extends AsyncTask<HttpClient, Integer, HttpResponse> {

    private Context context;

    public HttpTraitement (Context context) 
    {
        this.context = context;     
    }

    @Override
    protected HttpResponse doInBackground(HttpClient... params) {
        // TODO Auto-generated method stub
        HttpResponse response = null;
        params[0] = new DefaultHttpClient();


        HttpPost httppost = new HttpPost("http://namedomain/test.php");


        try {
            response = params[0].execute(httppost); 

        } catch (ClientProtocolException e) {
            Toast.makeText(this.context, "Caught ClientProtocolException", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            Toast.makeText(this.context, "Caught IOException", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(this.context, "Exception", Toast.LENGTH_SHORT).show();                  
        }

        return response;

    }






}

LogCat 堆栈:

06-12 14:54:01.455: E/Trace(1244): error opening trace file: No such file or directory (2)
06-12 14:54:04.166: E/AndroidRuntime(1244): FATAL EXCEPTION: AsyncTask #1
06-12 14:54:04.166: E/AndroidRuntime(1244): java.lang.RuntimeException: An error occured while executing doInBackground()
06-12 14:54:04.166: E/AndroidRuntime(1244):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
06-12 14:54:04.166: E/AndroidRuntime(1244):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
06-12 14:54:04.166: E/AndroidRuntime(1244):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
06-12 14:54:04.166: E/AndroidRuntime(1244):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
06-12 14:54:04.166: E/AndroidRuntime(1244):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-12 14:54:04.166: E/AndroidRuntime(1244):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-12 14:54:04.166: E/AndroidRuntime(1244):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-12 14:54:04.166: E/AndroidRuntime(1244):     at java.lang.Thread.run(Thread.java:856)
06-12 14:54:04.166: E/AndroidRuntime(1244): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
06-12 14:54:04.166: E/AndroidRuntime(1244):     at com.example.testapp.HttpTraitement.doInBackground(HttpTraitement.java:29)
06-12 14:54:04.166: E/AndroidRuntime(1244):     at com.example.testapp.HttpTraitement.doInBackground(HttpTraitement.java:1)
06-12 14:54:04.166: E/AndroidRuntime(1244):     at 
android.os.AsyncTask$2.call(AsyncTask.java:287)
06-12 14:54:04.166: E/AndroidRuntime(1244):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)

==================================================== ==================

4

3 回答 3

2

调用execute()时,AsyncTask您没有传递任何参数(因此长度params0)。
然后在内部doInBackground尝试将 aDefaultHttpClient放入params位置 0 的数组中,但该位置不存在。

params 参数是为了让您可以将参数传递AsyncTask给执行。您不应该在AsyncTask- 您应该将其视为只读的。

于 2013-06-12T15:13:28.817 回答
1

I suspect this is how you want to call your async task:

HttpTraitement task = new HttpTraitement(this);
task.execute("http://namedomain/test.php");

In order to do that, you must change your async task the following way:

public class HttpTraitement extends AsyncTask<String, Integer, HttpResponse> {

    private Context context;

    public HttpTraitement (Context context) 
    {
        this.context = context;     
    }

    @Override
    protected HttpResponse doInBackground(String... params) {
        HttpResponse response = null;
        HttpPost httppost = new HttpPost(params[0]);

        try {
            HttpClient client = new DefaultHttpClient();
            response = client.execute(httppost); 

        } catch (ClientProtocolException e) {
            Toast.makeText(this.context, "Caught ClientProtocolException", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            Toast.makeText(this.context, "Caught IOException", Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(this.context, "Exception", Toast.LENGTH_SHORT).show();                  
        }

        return response;

    }

}

Please note that the method execute() must be called only once for each async task object. Thus if you want to call it for more than one URL, you must do this:

HttpTraitement task1 = new HttpTraitement(this);
task1.execute("http://namedomain/test1.php");
HttpTraitement task2 = new HttpTraitement(this);
task2.execute("http://namedomain/test2.php");
于 2013-06-12T15:15:47.790 回答
0

为了调用异步任务,您必须在 MainClass 中创建类 Httptraitement 的新对象

HttpTraitement http=new HttpTraitement();

然后你必须执行一个 asyncTask 参数的路径,在你的情况下是 HttpClient 参数;

HttpClient client= new DefaultHttpClient();
htt.execute(client);

在您的日志中:

 06-12 14:54:04.166: E/AndroidRuntime(1244): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
06-12 14:54:04.166: E/AndroidRuntime(1244):     at com.example.testapp.HttpTraitement.doInBackground(HttpTraitement.java:29)

这意味着你没有传递一个参数......你去

于 2013-06-12T15:12:33.793 回答