gpt4 book ai didi

Android 应用程序在 AsyncTask 上崩溃以将 http 发布到服务器

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:08:33 26 4
gpt4 key购买 nike

我正在尝试测试 AsyncTask,为此我使用了本教程 http://mobiledevtuts.com/android/android-http-with-asynctask-example/

代码是这样的

package com.example.httpclient;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.opengl.Visibility;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity 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.activity_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);
AsyncTask task = new MyAsyncTask();
task.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){
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("myurl...");

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
}
}

}
}

LogCat 看起来像这样:

10-10 17:22:09.755: D/ProgressBar(32647): setProgress = 0
10-10 17:22:09.755: D/ProgressBar(32647): setProgress = 0, fromUser = false
10-10 17:22:09.755: D/ProgressBar(32647): mProgress = 0mIndeterminate = false, mMin = 0, mMax = 100
10-10 17:22:09.895: D/libEGL(32647): loaded /system/lib/egl/libEGL_mali.so
10-10 17:22:09.905: D/libEGL(32647): loaded /system/lib/egl/libGLESv1_CM_mali.so
10-10 17:22:09.910: D/libEGL(32647): loaded /system/lib/egl/libGLESv2_mali.so
10-10 17:22:09.915: E/(32647): Device driver API match
10-10 17:22:09.915: E/(32647): Device driver API version: 23
10-10 17:22:09.915: E/(32647): User space API version: 23
10-10 17:22:09.915: E/(32647): mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Wed Oct 9 21:05:57 KST 2013
10-10 17:22:10.035: D/OpenGLRenderer(32647): Enabling debug mode 0
10-10 17:22:14.050: D/ProgressBar(32647): updateDrawableBounds: left = 0
10-10 17:22:14.050: D/ProgressBar(32647): updateDrawableBounds: top = 0
10-10 17:22:14.050: D/ProgressBar(32647): updateDrawableBounds: right = 152
10-10 17:22:14.050: D/ProgressBar(32647): updateDrawableBounds: bottom = 152
10-10 17:22:14.120: W/dalvikvm(32647): threadid=11: thread exiting with uncaught exception (group=0x41ba6700)
10-10 17:22:14.190: E/AndroidRuntime(32647): FATAL EXCEPTION: AsyncTask #1
10-10 17:22:14.190: E/AndroidRuntime(32647): java.lang.RuntimeException: An error occured while executing doInBackground()
10-10 17:22:14.190: E/AndroidRuntime(32647): at android.os.AsyncTask$3.done(AsyncTask.java:299)
10-10 17:22:14.190: E/AndroidRuntime(32647): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
10-10 17:22:14.190: E/AndroidRuntime(32647): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
10-10 17:22:14.190: E/AndroidRuntime(32647): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
10-10 17:22:14.190: E/AndroidRuntime(32647): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-10 17:22:14.190: E/AndroidRuntime(32647): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-10 17:22:14.190: E/AndroidRuntime(32647): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-10 17:22:14.190: E/AndroidRuntime(32647): at java.lang.Thread.run(Thread.java:841)
10-10 17:22:14.190: E/AndroidRuntime(32647): Caused by: java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.String[]
10-10 17:22:14.190: E/AndroidRuntime(32647): at com.example.httpclient.MainActivity$MyAsyncTask.doInBackground(MainActivity.java:1)
10-10 17:22:14.190: E/AndroidRuntime(32647): at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-10 17:22:14.190: E/AndroidRuntime(32647): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-10 17:22:14.190: E/AndroidRuntime(32647): ... 4 more
10-10 17:22:24.550: I/Choreographer(32647): Skipped 610 frames! The application may be doing too much work on its main thread.
10-10 17:22:26.085: I/Process(32647): Sending signal. PID: 32647 SIG: 9

我在使用异步任务测试另一个教程时也遇到了同样的问题。

最佳答案

更改任务声明:

MyAsyncTask task = new MyAsyncTask();

如果您将其声明为 AsyncTask ,这意味着您将其声明为 AsyncTask<Object, Object, Object> .因此,doInBackground认为它会收到一个对象数组,它收到了一个字符串数组,这解释了 ClassCastException

关于Android 应用程序在 AsyncTask 上崩溃以将 http 发布到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26302220/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com