Skip to content

Commit 1f5824f

Browse files
authored
Version 1.0.2 (#3)
* handling 404 error but valid sitemap response * update version
1 parent ea2f655 commit 1f5824f

File tree

3 files changed

+79
-27
lines changed

3 files changed

+79
-27
lines changed

.idea/jarRepositories.xml

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ android {
88
minSdkVersion 24
99
targetSdkVersion 28
1010
versionCode 101
11-
versionName "1.0.1"
11+
versionName "1.0.2"
1212
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1313
}
1414
buildTypes {

app/src/main/java/io/github/chiver/MainActivity.java

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
import android.view.MenuItem;
88
import android.widget.Toast;
99

10+
import androidx.annotation.NonNull;
11+
import androidx.recyclerview.widget.GridLayoutManager;
12+
import androidx.recyclerview.widget.RecyclerView;
13+
1014
import com.android.volley.RequestQueue;
1115
import com.android.volley.toolbox.StringRequest;
1216
import com.android.volley.toolbox.Volley;
@@ -25,9 +29,6 @@
2529
import javax.xml.parsers.SAXParser;
2630
import javax.xml.parsers.SAXParserFactory;
2731

28-
import androidx.annotation.NonNull;
29-
import androidx.recyclerview.widget.GridLayoutManager;
30-
import androidx.recyclerview.widget.RecyclerView;
3132
import io.github.chiver.adapter.GalleryAdapter;
3233
import io.github.chiver.util.SitemapSAXParser;
3334

@@ -36,10 +37,14 @@ public class MainActivity extends BaseActivity {
3637
private GalleryAdapter adapter;
3738
private RequestQueue requestQueue;
3839
private int daysCounter = 0;
40+
private ProgressDialog progressDialog;
3941

4042

4143
@Override
4244
protected void _onCreate(Bundle savedInstanceState) {
45+
progressDialog = new ProgressDialog(this);
46+
progressDialog.setMessage(getString(R.string.loading));
47+
4348
RecyclerView recyclerView = findViewById(R.id.rv_galleries);
4449
recyclerView.setHasFixedSize(true);
4550
adapter = new GalleryAdapter(this, getChiver().getSimpleDiskCache());
@@ -51,6 +56,9 @@ protected void _onCreate(Bundle savedInstanceState) {
5156
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
5257
@Override
5358
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
59+
if (progressDialog.isShowing()) {
60+
return;
61+
}
5462
if (layoutManager != null) {
5563
boolean canScroll = layoutManager.findLastCompletelyVisibleItemPosition() < adapter.getItemCount() - 1;
5664
if (!canScroll) {
@@ -76,8 +84,6 @@ protected int getContentView() {
7684

7785
private void loadGalleries() {
7886

79-
final ProgressDialog progressDialog = new ProgressDialog(this);
80-
progressDialog.setMessage(getString(R.string.loading));
8187
progressDialog.show();
8288

8389
Calendar utc = GregorianCalendar.getInstance(TimeZone.getTimeZone("UTC"));
@@ -88,29 +94,21 @@ private void loadGalleries() {
8894
int day = utc.get(Calendar.DAY_OF_MONTH);
8995

9096
StringRequest stringRequest = new StringRequest(String.format(Locale.getDefault(), Constants.TC_SITEMAP_PATTERN, year, month, day, String.valueOf(Math.random())), response -> {
91-
92-
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
93-
try {
94-
SAXParser saxParser = saxParserFactory.newSAXParser();
95-
SitemapSAXParser handler = new SitemapSAXParser(adapter::addItem);
96-
saxParser.parse(new InputSource(new StringReader(response)), handler);
97-
} catch (ParserConfigurationException | SAXException | IOException e) {
98-
Log.e("Volley", e.getMessage(), e);
99-
progressDialog.dismiss();
100-
Toast.makeText(MainActivity.this, R.string.loadingError, Toast.LENGTH_SHORT).show();
101-
}
102-
103-
daysCounter--;
104-
105-
adapter.notifyDataSetChanged();
106-
progressDialog.dismiss();
97+
parseAndNotifyProgress(response, progressDialog);
10798
}, error -> {
10899

109100
if (error.networkResponse != null && error.networkResponse.statusCode == 404) {
110-
daysCounter--;
111-
progressDialog.dismiss();
112-
Toast.makeText(MainActivity.this, R.string.backInTime, Toast.LENGTH_SHORT).show();
113-
loadGalleries();
101+
102+
//sometimes we get 404 but even if we get proper response
103+
if (isValidResponse(error.networkResponse.data)) {
104+
String response = new String(error.networkResponse.data);
105+
parseAndNotifyProgress(response, progressDialog);
106+
} else {
107+
daysCounter--;
108+
progressDialog.dismiss();
109+
Toast.makeText(MainActivity.this, R.string.backInTime, Toast.LENGTH_SHORT).show();
110+
loadGalleries();
111+
}
114112
return;
115113
}
116114

@@ -121,6 +119,35 @@ private void loadGalleries() {
121119
getRequestQueue().add(stringRequest);
122120
}
123121

122+
private boolean isValidResponse(byte[] data) {
123+
//does it start with '<?xml' ?
124+
return data != null
125+
&& data.length > 5
126+
&& data[0] == '<'
127+
&& data[1] == '?'
128+
&& data[2] == 'x'
129+
&& data[3] == 'm'
130+
&& data[4] == 'l';
131+
}
132+
133+
private void parseAndNotifyProgress(String response, ProgressDialog progressDialog) {
134+
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
135+
try {
136+
SAXParser saxParser = saxParserFactory.newSAXParser();
137+
SitemapSAXParser handler = new SitemapSAXParser(adapter::addItem);
138+
saxParser.parse(new InputSource(new StringReader(response)), handler);
139+
} catch (ParserConfigurationException | SAXException | IOException e) {
140+
Log.e("Volley", e.getMessage(), e);
141+
progressDialog.dismiss();
142+
Toast.makeText(MainActivity.this, R.string.loadingError, Toast.LENGTH_SHORT).show();
143+
}
144+
145+
daysCounter--;
146+
147+
adapter.notifyDataSetChanged();
148+
progressDialog.dismiss();
149+
}
150+
124151
private synchronized RequestQueue getRequestQueue() {
125152
if (this.requestQueue == null) {
126153
this.requestQueue = Volley.newRequestQueue(this);
@@ -133,8 +160,8 @@ private synchronized RequestQueue getRequestQueue() {
133160
@Override
134161
protected boolean onRefresh(MenuItem item) {
135162
adapter.clearItems();
136-
adapter.notifyDataSetChanged();
137163
resetDaysCounter();
164+
adapter.notifyDataSetChanged();
138165
loadGalleries();
139166
return super.onRefresh(item);
140167
}

0 commit comments

Comments
 (0)