gpt4 book ai didi

android - 缓存 JSON 响应以在设备离线时使用

转载 作者:行者123 更新时间:2023-11-30 00:55:03 25 4
gpt4 key购买 nike

我正在构建一个新闻应用程序,我想缓存一个 JSON 响应以在没有网络时使用。我尝试了很多方法,但都不适合我。

我的异步任务和我的适配器是另一个附带问题,当列表加载新项目时,它会转到屏幕顶部,然后添加项目。我该如何解决这个问题?

private class jsontask extends AsyncTask<String, String, List<newsmodel>> {
@Override
protected List<newsmodel> doInBackground(String... params) {
BufferedReader reader = null;
HttpURLConnection connection = null;
try {
URL url = new URL(params[0]);

connection = (HttpURLConnection) url.openConnection();

connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}

String finaljson = buffer.toString();
JSONObject parentobject = new JSONObject(finaljson);

JSONArray parentarray = parentobject.getJSONArray("articles");

for (int i = 0; i < parentarray.length(); i++) {
JSONObject finalobject = parentarray.getJSONObject(i);
newsmodel newsmodel = new newsmodel();
newsmodel.setAuthor(finalobject.getString("author"));
if (finalobject.isNull("author")) {
}
newsmodel.setDescription(finalobject.getString("description"));
newsmodel.setTitle(finalobject.getString("title"));
newsmodel.setImage(finalobject.getString("urlToImage"));
newsmodel.setUrl(finalobject.getString("url"));
newsmodel.setPublishedAt(finalobject.getString("publishedAt"));

moviemodelList.add(newsmodel);
}

return moviemodelList;

} catch (MalformedURLException e) {
e.printStackTrace();
return moviemodelList;
} catch (IOException e) {
e.printStackTrace();
return moviemodelList;
} catch (JSONException e) {

} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

return null;
}

@Override
protected void onPostExecute(List<newsmodel> result) {

super.onPostExecute(result);
newsadapter adapter = new newsadapter(getApplicationContext(), R.layout.row, result);
lvnews.setAdapter(adapter);
}
}

public class newsadapter extends ArrayAdapter {
private List<newsmodel> moviemodelList;
private int resource;
private LayoutInflater inflater;

public newsadapter(Context context, int resource, List<newsmodel> objects) {
super(context, resource, objects);
moviemodelList = objects;
this.resource = resource;
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
viewholder holder = null;
if (convertView == null) {
holder = new viewholder();
convertView = inflater.inflate(resource, null);
holder.newsimage = (ImageView) convertView.findViewById(R.id.imageView2);
holder.title = (TextView) convertView.findViewById(R.id.textView2);
holder.description = (TextView) convertView.findViewById(R.id.textView3);
holder.author = (TextView) convertView.findViewById(R.id.textView4);
holder.publishdate = (TextView) convertView.findViewById(R.id.textView5);
holder.dotsmenu = (ImageButton) convertView.findViewById(R.id.dots);
convertView.setTag(holder);
} else {
holder = (viewholder) convertView.getTag();
}

final ProgressBar progressBar;
progressBar = (ProgressBar) convertView.findViewById(R.id.progressBar);
ImageLoader.getInstance().displayImage(moviemodelList.get(position).getImage(), holder.newsimage, new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
progressBar.setVisibility(view.VISIBLE);
}

@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
progressBar.setVisibility(view.GONE);
}

@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
progressBar.setVisibility(view.GONE);
}

@Override
public void onLoadingCancelled(String imageUri, View view) {
progressBar.setVisibility(view.GONE);
}
});
holder.title.setText(moviemodelList.get(position).getTitle());
holder.description.setText(moviemodelList.get(position).getDescription());
holder.author.setText(moviemodelList.get(position).getAuthor());
holder.publishdate.setText(moviemodelList.get(position).getPublishedAt());
final viewholder finalHolder = holder;
holder.dotsmenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(page.this, finalHolder.dotsmenu);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.dots_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {

case R.id.share:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, moviemodelList.get(position).getUrl());
sendIntent.setType("text/plain");
startActivity(sendIntent);
return true;
}
return false;
}

});
popup.show();
}
});
lvnews.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

Intent intent = new Intent(page.this, webview.class);
intent.putExtra("Link", moviemodelList.get(position).getUrl());
startActivity(intent);
}
});

return convertView;
}

最佳答案

制作一个测试网络连接是否可用的类。

连接检测器.java

public class ConnectionDetector {
public Context context;
public ConnectionDetector(Context context)
{
this.context=context;
}
public boolean isConnecting() {
ConnectivityManager check=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (check!=null){
NetworkInfo[] infos=check.getAllNetworkInfo();
for (int i=0;i<infos.length;i++)
{
if (infos[i].getState()== NetworkInfo.State.CONNECTED)
return true;
}}

return false;

}
}

创建一个上述类的对象ob。

ConnectionDetector ob=new  ConnectionDetector(getApplicationContext());

将这些变量声明为全局变量:

  File httpCacheDir;
long httpCacheSize = 5 * 1024 * 1024;// In place of 5 you can use size in mb
HttpResponseCache cache;

制作一个函数cacher():

 public void cacher()
{
httpCacheDir = getExternalCacheDir();

// Cache Size of 5MB


try {
// Install the custom Cache Implementation
HttpResponseCache.install(httpCacheDir, httpCacheSize);
} catch (Exception e) {
e.printStackTrace();
}
}

在 onCreate 中的 setContentView 下方,添加以下代码:

   cacher();
cache = HttpResponseCache.getInstalled();
if(cache != null) {
// If cache is present, flush it to the filesystem.
// Will be used when activity starts again.
cache.flush();
}

这是最后一件事,在 url.openConnection 行下面放置以下 fragment :

 if (ob.isConnecting()){
connection.addRequestProperty("Cache-Control", "max-age=0");}
else{
connection.addRequestProperty("Cache-Control", "max-stale=" + 60*60*36);//In place of 36 , you can put hours for which cache is available
connection.setUseCaches(true);
}

关于android - 缓存 JSON 响应以在设备离线时使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40324162/

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