gpt4 book ai didi

java - loadInBackground()的返回值传给谁呢?

转载 作者:行者123 更新时间:2023-12-02 10:01:27 25 4
gpt4 key购买 nike

(抱歉,我不知道我的英语是否正确,我希望是正确的!)loadInBackground()方法,(在BookLoader类中)返回一个字符串值,但是给谁呢?我也找过谁调用了loadInBackground(),但没有人这样做。我也阅读了官方文档,但没有解决方案。谢谢 friend 的建议。

public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<String> {

public EditText mEditText;
public TextView mTextTitle, mTextAuthor;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mEditText = (EditText) findViewById(R.id.bookInput);
mTextTitle = (TextView) findViewById(R.id.titleText);
mTextAuthor = (TextView) findViewById(R.id.authorText);

// to reconnect to the Loader if it already exists
if(getSupportLoaderManager().getLoader(0)!=null){

getSupportLoaderManager().initLoader(0,null,this);

}

}

public void searchBooks(View view) {

String queryString = mEditText.getText().toString();

// nasconde la tastiera
InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

// Check the status of the network connection.
ConnectivityManager connMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

if (networkInfo != null && networkInfo.isConnected() && queryString.length()!=0) {

mTextAuthor.setText("");
mTextTitle.setText(R.string.loading);

Bundle queryBundle = new Bundle();
queryBundle.putString("queryString", queryString);

getSupportLoaderManager().restartLoader(0, queryBundle,this);

}

else {

if (queryString.length() == 0) {

mTextAuthor.setText("");
mTextTitle.setText("Please enter a search term");

} else {

mTextAuthor.setText("");
mTextTitle.setText("Please check your network connection and try again.");

}

}

}

// Called when you instantiate your Loader.
@NonNull
@Override
public Loader<String> onCreateLoader(int i, @Nullable Bundle bundle) {

return new BookLoader(this, bundle.getString("queryString"));

}


@Override
public void onLoadFinished(@NonNull Loader<String> loader, String s) {

try {

JSONObject jsonObject = new JSONObject(s);
JSONArray itemsArray = jsonObject.getJSONArray("items");

int i = 0;
String title = null;
String authors = null;

while (i < itemsArray.length() || (authors == null && title == null)) {
// Get the current item information.
JSONObject book = itemsArray.getJSONObject(i);
JSONObject volumeInfo = book.getJSONObject("volumeInfo");

// Try to get the author and title from the current item,
// catch if either field is empty and move on.
try {

title = volumeInfo.getString("title");
authors = volumeInfo.getString("authors");
Log.d("TITLE", volumeInfo.getString("title"));

} catch (Exception e){

e.printStackTrace();

}

// Move to the next item.
i++;
}

// If both are found, display the result.
if (title != null && authors != null){

mTextTitle.setText(title);
mTextAuthor.setText(authors);
mEditText.setText("");

} else {
// If none are found, update the UI to show failed results.
mTextTitle.setText("no results");
mTextAuthor.setText("");

}

} catch (Exception e){
// If onPostExecute does not receive a proper JSON string, update the UI to show failed results.
mTextTitle.setText("no results");
mTextAuthor.setText("");
e.printStackTrace();
}

}

// Cleans up any remaining resources.
@Override
public void onLoaderReset(@NonNull Loader<String> loader) {

}

}




public class BookLoader extends AsyncTaskLoader<String> {

String mQueryString;

public BookLoader(@NonNull Context context, String queryString) {

super(context);
mQueryString = queryString;

}

@Nullable
@Override
public String loadInBackground() {

return NetworkUtils.getBookInfo(mQueryString);

}

@Override
protected void onStartLoading() {

super.onStartLoading();

forceLoad();

}

}

我编辑了帖子。

最佳答案

正如官方文档所说:

onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.

您的返回值将发送到 onPostExecute()。

我鼓励您更深入地了解 this documentation

关于java - loadInBackground()的返回值传给谁呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55595377/

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