Android 有没有办法更新AsyncTask中的变量?
在Android 有没有办法更新AsyncTask中的变量?,android,json,android-recyclerview,android-asynctask,Android,Json,Android Recyclerview,Android Asynctask,在MainActivity.java第66-74行中,我获得了在recyclerView中单击的项目的位置,并使用它从JSON获取当前url,并将url传递给新闻详细信息。java使用putExtra,在news\u detail.java中AsyncTask中的doInBackground方法中,url不会随着每次单击而更新,而是使用recyclerView中的第一项。 有没有办法解决这个问题,如果还有其他办法解决这个问题,请我真诚地想知道 MainActivity.java packag
MainActivity.java
第66-74行中,我获得了在recyclerView
中单击的项目的位置,并使用它从JSON
获取当前url
,并将url
传递给新闻详细信息。java
使用putExtra
,在news\u detail.java
中AsyncTask
中的doInBackground
方法中,url
不会随着每次单击而更新,而是使用recyclerView中的第一项。
有没有办法解决这个问题,如果还有其他办法解决这个问题,请我真诚地想知道
MainActivity.java
package wami.ikechukwu.kanu;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import dmax.dialog.SpotsDialog;
public class MainActivity extends AppCompatActivity implements newsAdapter.onclicklistener {
private final String KEY_AUTHOR = "author";
private final String KEY_TITLE = "title";
private final String KEY_DESCRIPTION = "description";
private final String KEY_URL = "url";
private final String KEY_URL_TO_IMAGE = "urlToImage";
private final String KEY_PUBLISHED_AT = "publishedAt";
//this string is appended to the url
String urlLink = "buhari";
String url;
int mPosition;
ArrayList<dataModel> list;
private RecyclerView recyclerView;
private newsAdapter mAdapter;
private RecyclerView.LayoutManager mLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
list = new ArrayList<>();
recyclerView = findViewById(R.id.recyclerView);
mAdapter = new newsAdapter(this, list, this);
mLayout = new LinearLayoutManager(this);
recyclerView.setLayoutManager(mLayout);
recyclerView.setAdapter(mAdapter);
jsonParser();
}
@Override
public void onItemClick(int position) {
mPosition = position;
Intent intent = new Intent(this, news_detail.class);
intent.putExtra("POSITION", position);
intent.putExtra("URL", url);
startActivity(intent);
}
private void jsonParser() {
final AlertDialog progressDialog = new SpotsDialog(this, R.style.customProgressDialog);
progressDialog.show();
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "https://newsapi.org/v2/everything?q=" + urlLink + "&language=en&sortBy=publishedAt&pageSize=100&apiKey=a5f976b34089493abc8f97f088e5df64", null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("articles");
//Using a for loop to get the object (data) in the JSON
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
JSONObject JO = jsonArray.getJSONObject(mPosition);
dataModel dataModel = new dataModel();
dataModel.setTitle(jsonObject.getString(KEY_TITLE));
dataModel.setImage(jsonObject.getString(KEY_URL_TO_IMAGE));
dataModel.setDescrip(jsonObject.getString(KEY_DESCRIPTION));
url = JO.getString(KEY_URL);
list.add(dataModel);
}
} catch (JSONException e) {
e.printStackTrace();
}
mAdapter.notifyDataSetChanged();
progressDialog.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", error.toString());
progressDialog.dismiss();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonObjectRequest);
}
}
MainActivity.java
包wami.ikechukwu.kanu;
导入android.app.AlertDialog;
导入android.content.Intent;
导入android.os.Bundle;
导入android.support.v7.app.AppActivity;
导入android.support.v7.widget.LinearLayoutManager;
导入android.support.v7.widget.RecyclerView;
导入android.util.Log;
导入com.android.volley.Request;
导入com.android.volley.RequestQueue;
导入com.android.volley.Response;
导入com.android.volley.VolleyError;
导入com.android.volley.toolbox.JsonObjectRequest;
导入com.android.volley.toolbox.volley;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.util.ArrayList;
导入dmax.dialog.SpotsDialog;
公共类MainActivity扩展AppCompatActivity实现newsAdapter.onclicklistener{
私有最终字符串密钥\u AUTHOR=“AUTHOR”;
私有最终字符串键\u TITLE=“TITLE”;
私有最终字符串键\u DESCRIPTION=“DESCRIPTION”;
私有最终字符串键\u URL=“URL”;
私有最终字符串键\u URL\u到\u IMAGE=“urlToImage”;
私有最终字符串密钥\u PUBLISHED\u AT=“publishedAt”;
//此字符串将附加到url
字符串urlink=“buhari”;
字符串url;
整合;
数组列表;
私人回收站;
私人新闻适配器;
private RecyclerView.LayoutManager mLayout;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_main);
列表=新的ArrayList();
recyclerView=findViewById(R.id.recyclerView);
mAdapter=newsAdapter(this,list,this);
mLayout=新的LinearLayoutManager(本);
recyclerView.setLayoutManager(mLayout);
recyclerView.setAdapter(mAdapter);
jsonParser();
}
@凌驾
公共空间单击(内部位置){
位置=位置;
意向意向=新意向(此,news\u detail.class);
意图。额外(“位置”,位置);
intent.putExtra(“URL”,URL);
星触觉(意向);
}
私有void jsonParser(){
final AlertDialog progressDialog=new SpotsDialog(此为R.style.customProgressDialog);
progressDialog.show();
JsonObjectRequest JsonObjectRequest=新的JsonObjectRequest(Request.Method.GET)https://newsapi.org/v2/everything?q=“+urlink+”&language=en&sortBy=publishedAt&pageSize=100&apiKey=a5f976b34089493abc8f97f08e5df64”,空,新响应。侦听器(){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
JSONArray JSONArray=response.getJSONArray(“文章”);
//使用for循环获取JSON中的对象(数据)
for(int i=0;i
news\u detail.java
包wami.ikechukwu.kanu;
导入android.os.AsyncTask;
导入android.os.Build;
导入android.os.Bundle;
导入android.support.v7.app.AppActivity;
导入android.widget.ImageView;
导入android.widget.TextView;
导入com.android.volley.Request;
导入com.android.volley.RequestQueue;
导入com.android.volley.Response;
导入com.android.volley.VolleyError;
导入com.android.volley.toolbox.JsonObjectRequest;
导入com.android.volley.toolbox.volley;
导入com.bumptech.glide.glide;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入org.jsoup.jsoup;
导入org.jsoup.nodes.Document;
导入java.io.IOException;
公共类新闻详细信息扩展应用程序活动{
//TODO:如果应用程序中不需要,请删除未使用的行
//这些变量用于从API获取JSON的匹配响应
//私有最终字符串密钥\u AUTHOR=“AUTHOR”;
私有最终字符串键\u TITLE=“TITLE”;
//私有最终字符串键\u DESCRIPTION=“DESCRIPTION”;
私有最终字符串键\u URL=“URL”;
私有最终字符串键\u URL\u到\u IMAGE=“urlToImage”;
私有最终字符串密钥\u PUBLISHED\u AT=“publishedAt”;
//这个变量保持P
news_detail.java
package wami.ikechukwu.kanu;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;
public class news_detail extends AppCompatActivity {
//TODO: REMOVE THE UNUSED LINE IF THERE IS NO NEED FOR THEM IN THE APP
//THESE VARIABLE ARE USED TO GET THE MATCHING RESPONSE FROM THE JSON FROM THE API
// private final String KEY_AUTHOR = "author";
private final String KEY_TITLE = "title";
//private final String KEY_DESCRIPTION = "description";
private final String KEY_URL = "url";
private final String KEY_URL_TO_IMAGE = "urlToImage";
private final String KEY_PUBLISHED_AT = "publishedAt";
//THIS VARIABLE HOLD THE POSITION (NUMBER/INTEGER) OF THE ITEM CLICKED IN THE RECYCLERVIEW
int itemPosition;
//THIS STRING IS APPENDED TO THE URL OF THE API AND IS THE MAIN KEYWORD BEING SEARCHED FOR
String urlLink = "buhari";
//THIS STRING IS INTENDED TO HOLD THE URL FROM THE JSON -WHICH IS USED OPEN EACH INDIVIDUAL
// NEWS PAGE
String news_url;
//INSTANCE OF THE XML VIEWS
TextView newsDetail_Title, newDetail_Time_Posted, newsDetail_News;
ImageView newsDetail_Image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_detail);
//GET THE POSITION (NUMBER) OF THE ITEM IN THE RECYCLERVIEW THAT WAS CLICKED IN THE MAIN
// ACTIVITY
itemPosition = getIntent().getIntExtra("POSITION", 0);
news_url = getIntent().getStringExtra("URL");
//GET THE INSTANCE OF THE VIEW
newsDetail_Title = findViewById(R.id.newsDetail_Title);
newDetail_Time_Posted = findViewById(R.id.newDetail_Time_Posted);
newsDetail_News = findViewById(R.id.newsDetail_News);
newsDetail_Image = findViewById(R.id.newsDetail_Image);
//CALL THE METHOD THAT DOES ALL THE WORK IN THIS ACTIVITY
newsRequest();
new experiment().execute();
}
public void newsRequest() {
//USING VOLLEY TO CREATE AN INTERNET CONNECTION AND PARSE THE JSON
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "https://newsapi.org/v2/everything?q=" + urlLink + "&language=en&sortBy=publishedAt&pageSize=100&apiKey=a5f976b34089493abc8f97f088e5df64", null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//I HAD TO SURROUND THIS IN A TRY AND CATCH STATEMENT TO AVOID THE APP CRASHING
try {
//GETTING THR ARRAY IN THE JSON THAT HOLD OTHER OBJECT/ARRAY
JSONArray jsonArray = response.getJSONArray("articles");
//USING A FOR-LOOP TI GET THE OBJECT (DATA) IN THE JSON
JSONObject jsonObject = jsonArray.getJSONObject(itemPosition);
//SET THE TEXT IN THE XML TO THAT OF THE TITLE FROM THE JSON RESPONSE
newsDetail_Title.setText(jsonObject.getString(KEY_TITLE));
//news_url = jsonObject.getString(KEY_URL);
//newsDetail_News.setText(news_url);
Glide.with(getApplicationContext()).load(jsonObject.getString(KEY_URL_TO_IMAGE)).into(newsDetail_Image);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonObjectRequest);
///////////////////////////////////////////////////////////////////////////////////////////////////
}
public class experiment extends AsyncTask<Void, Void, Void> {
String title;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... voids) {
try {
String newUrl;
if (!news_url.contains("http")) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
newUrl = "https://" + news_url;
} else {
newUrl = "http" + news_url;
}
} else {
newUrl = news_url;
}
Document document =
Jsoup.connect(newUrl).followRedirects(true).timeout(600000).get();
/* Elements element = document.select("p");
for (Element paragraph : element) {
builder.append(paragraph.text());
}
*/
title = document.title();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
newsDetail_News.setText(title);
}
}
}
list.add(dataModel);
//set adapter after this above line
mAdapter = new newsAdapter(this, list, this);
onItemClick(int position, String url)