我是 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)
==================================================== ==================