我试图解析一个jsonObject,但我不能将doInBackground的结果 输入onPostExecute
这是我的AsyncTask代码:
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MyActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = …Run Code Online (Sandbox Code Playgroud) Android Studio坚持认为我错过了返回值doInBackground,即使它似乎被声明为Void.我忽略了什么吗?
AsyncTask<Boolean, Void, Void> initiateConnection = new AsyncTask<WebSocketClient, Void, Void>() {
@Override
protected Void doInBackground(WebSocketClient ... clients) {
clients[0].connect();
}
@Override
protected void onPostExecute(Void result) {
Log.i(st, "Socket connected.");
}
};
initiateConnection.execute();
}
Run Code Online (Sandbox Code Playgroud) 我有多次调用AsyncTask我想转换为RxJava.AsyncTask代码有效,但我想在RxJava中探索如何做到这一点?
(顺便说一句,我知道标题很糟糕)
它能做什么:
我如何将其转换为RxJava:
for (MyImageButton myImageButton: myImageButtons) {
final ImageButton imageButton = (ImageButton) findViewById(myImageButton.getImageButtonResId()); //getImageButtonResId holds a reference to some ImageButton
final Bitmap bitmap = ((BitmapDrawable) imageButton.getDrawable()).getBitmap();
new BindImageTask(imageButton, bitmap, myImageButton.getIconUrl()).execute();
}
Run Code Online (Sandbox Code Playgroud)
这是BindImageTask:
private class BindImageTask extends AsyncTask<Void, Void, Bitmap> {
private WeakReference<Bitmap> srcBitmapWeakReference;
private WeakReference<ImageButton> imageButtonWeakReference;
private String dstIconUrl;
BindImageTask(ImageButton imageButton, Bitmap srcBitmap, String dstIconUrl) {
srcBitmapWeakReference = new WeakReference<>(srcBitmap);
imageButtonWeakReference = new WeakReference<>(imageButton);
this.dstIconUrl = dstIconUrl;
}
@Override
protected Bitmap doInBackground(Void... params) …Run Code Online (Sandbox Code Playgroud) 我是android开发的新手.我正在使用asynctask方法来调用我的函数.一切正常,但我不知道为什么函数确实返回数据和android studio为什么会出现此错误
> Error:(182, 9) error: missing return statement
Run Code Online (Sandbox Code Playgroud)
当我删除 return null;
这里是代码我希望你能理解我的问题是什么
public class YourAsyncTask extends AsyncTask<String, String, String> {
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... strings) {
//Call getData function from here
getData();
return null;
}
@Override
protected void onPostExecute(String lenghtOfFile) {
// do stuff after posting data
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用ApplicationTestCase测试Android应用.我想模拟我的一个AsyncTasks(示例简化以显示问题):
public class Foo extends AsyncTask<Void,Void,Void> {
@Override
protected Void doInBackground(Void... unused) {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
所以为了设置我的测试,我做了以下事情:
private Foo mockFoo;
@Override
protected void setUp() throws Exception {
super.setUp()
mockFoo = mock(Foo.class);
createApplication();
}
Run Code Online (Sandbox Code Playgroud)
然后实际测试如下:
public void testAsyncTaskMock() {
mockFoo.execute();
verify(mockFoo).execute();
}
Run Code Online (Sandbox Code Playgroud)
但是我在mockFoo.execute();运行时遇到异常:
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.AsyncTask$Status.ordinal()' on a null object reference
Run Code Online (Sandbox Code Playgroud)
为什么模拟AsyncTask的技术不起作用?
请注意,createApplication();在这种简单的情况下,删除会导致问题消失,但对于我的实际测试,我确实需要创建应用程序.
我在装载机上遇到了一些麻烦,但是我不明白我做错了什么。这是初始化并启动Loader的片段
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.pixelark.bulletinplus.R;
import com.pixelark.bulletinplus.async.ChurchListLoader;
import com.pixelark.bulletinplus.debug.Debug;
import com.pixelark.bulletinplus.model.ChurchListItem;
import java.util.ArrayList;
public class ChurchListFragment extends android.support.v4.app.Fragment
implements LoaderManager.LoaderCallbacks<ArrayList<ChurchListItem>> {
private static final int CHURCH_LIST_LOADER_ID = 777;
public ChurchListFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_church_list, container, false);
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
toolbar.setTitle("ChurchListFragment");
getLoaderManager().initLoader(CHURCH_LIST_LOADER_ID, null, this);
return rootView;
}
@Override
public Loader<ArrayList<ChurchListItem>> …Run Code Online (Sandbox Code Playgroud) 在这里,他用过
View myFragmentView = inflater.inflate(R.layout.fragment_a, container, false);
Run Code Online (Sandbox Code Playgroud)
来自父类.我inflater在父母身上也没有,这是一张地图.
而从的AsyncTask doitbackground,我回到5周的ArrayList,使textview在postexecute.
但我做不到因为我不能使用findviewbyid,因为我无法获得活动.我有上下文但它没有做任何事情.
这是我的 postexecute
protected void onPostExecute(Wrapper wrap){
TextView name = new TextView (mContext);
TextView type = new TextView (mContext);
TextView location = new TextView (mContext);
TextView distance = new TextView (mContext);
List<Double> dist = new ArrayList();
List<String> loc = new ArrayList();
List<String> nme = new ArrayList();
List<String> typ = new ArrayList();
List<Calendar> start = new ArrayList();
List<Calendar> endd = new ArrayList();
dist …Run Code Online (Sandbox Code Playgroud) 我有一个小时的使用RxJava的经验,我试图在我的项目中实现它,而不是使用接口和监听器.
我有一个异步任务,它在一个单独的模块中调用谷歌云端点方法,并List<Profile>在完成时收到.
在onPostExecute()异步任务的方法中,我调用onNext以便任何订阅者都能接收到这些数据.
这是AsyncTask看起来像:
private BirthpayApi mApi;
private String mUserId;
private ReplaySubject<List<Profile>> notifier = ReplaySubject.create();
public GetFriends(String userId) {
mUserId = userId;
}
public Observable<List<Profile>> asObservable() {
return notifier;
}
@Override
protected List<Profile> doInBackground(Void... params) {
if (mApi == null) {
BirthpayApi.Builder builder = new BirthpayApi.Builder(AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), null)
// options for running against local devappserver
// - 10.0.2.2 is localhost's IP address in Android emulator
// - turn off compression when running against …Run Code Online (Sandbox Code Playgroud) 从Google的Android文档中可以看出,我们看到了这一点
Params,执行时发送给任务的参数类型.
进度,后台计算期间发布的进度单元的类型.
结果,后台计算结果的类型.
在这个优秀的答案中也提到过.
但是,为什么有Void结果类型会导致Android Studio在线上发出以下警告doInBackground:
doInBackground(String...)' in '(..)MainActivity.myTask' clashes with 'doInBackground(Params...)' in 'android.os.AsyncTask'; attempting to use incompatible return type
Run Code Online (Sandbox Code Playgroud)
这AsyncTask就是声明的方式:
private class myTask extends AsyncTask<String,Void,Void> {
@Override
protected void doInBackground(String... url) {
//do stuff with url[0]
}
@Override
protected void onPostExecute() {
//more stuff
}
}
Run Code Online (Sandbox Code Playgroud)
相比之下,这似乎工作正常:
private class myTask extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... url) {
//do stuff with url[0] …Run Code Online (Sandbox Code Playgroud) 我已经解析了一些XML数据Asynctask并将其打印在日志中,但每当我尝试将ArrayList数据复制到我的Activity中时,它总是保持为空.
这是代码,
public class MainActivity extends AppCompatActivity {
static ArrayList<NewsItems>myData=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ReadRss readRss = new ReadRss(this);
readRss.execute();
Log.d("TAG", String.valueOf(myData.size()));//This stays empty
}
public static void getData(ArrayList<NewsItems>items){
for (int i=0; i<items.size(); i++){
myData.add(items.get(i));
}
}
class ReadRss extends AsyncTask<Void, Void, Void>{
ArrayList<NewsItems>feedItems = new ArrayList<>();
Context context;
String address = "http://www.thedailystar.net/frontpage/rss.xml";
ProgressDialog progressDialog;
URL url;
public ReadRss(Context context) {
this.context = context;
progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Loading..."); …Run Code Online (Sandbox Code Playgroud)