Android - for loop terminating early - java

I'm trying to manipulate a JSONArray, rawJArr, (taken from the Reddit API), and get the url and a bitmap (taken from the gfycat "API") from each object to create an ArrayList (listing) of Highlight instances which will be converted to a CardView containing a picture, a short description, and a link to the gfycat.
try {
int count = 0;
int highlightMax;
Bitmap bitmap = null;
Highlight curHighlight;
myJSON = new JSONObject(rawJSON);
curJSON = myJSON.getJSONObject("data");
rawJArr = curJSON.getJSONArray("children");
String strHighlightNo = mySPrefs.getString("pref_highlightNo", "notFound");
if(strHighlightNo.equals("notFound")) {
Log.w("FT", "shared pref not found");
return null;
}
highlightMax = Integer.parseInt(strHighlightNo);
Log.w("Arr Length", Integer.toString(rawJArr.length()));
Log.w("Highlight No", Integer.toString(highlightMax));
for(int i=0; i < rawJArr.length(); i++) {
Log.w("Count", Integer.toString(count));
Log.w("I", Integer.toString(i));
if(count == highlightMax) {
Log.w("FT", "Breakpoint reached!");
break;
}
curJSON = rawJArr.getJSONObject(i).getJSONObject("data");
String url = curJSON.getString("url");
String[] parts = url.split("//");
String imageUrl = "http://thumbs." + parts[1] + "-thumb100.jpg";
try {
bitmap = BitmapFactory.decodeStream((InputStream) new URL(imageUrl).getContent());
} catch (MalformedURLException e) {
e.printStackTrace();
}
// if there is no available picture, then don't include one in the Highlight
if(bitmap == null) {
Log.w("FT", "Null bitmap");
curHighlight = new Highlight(curJSON.getString("title"), url, null);
listing.add(curHighlight);
count++;
} else {
Log.w("FT", "Bitmap Available");
curHighlight = new Highlight(curJSON.getString("title"), url, bitmap);
listing.add(curHighlight);
count++;
}
}
} catch(JSONException e) {
e.printStackTrace();
return listing;
}
However, my for loop terminates way too early. The current JSONArray I'm using has a length of 25, and I've specified a pref_highlightNo of 15, but my for loop terminates after 6 iterations.
My Log.w tests in the for loop all record the same count (Count: 1, Integer: 1 - Count: 6, Integer: 6).
I'm struggling to see why my loop is terminating: there is no stack trace printed to my console, and my app doesn't crash.
Any idea what's going on?

Turns out the issue was specific to the last url I was trying to create to get the required gfycat - I didn't have any code to handle cases where the link began with http://www.

Related

Filtering in Android - issue that the code works but it doesn't return to the screen as expected

I've created an app in Android to retrieve restaurants in the local area and return them in list view. I've created individual arrays for each of the details in question (name / address / postcode & hygiene rating). Some of the venues are exempt and the array will return the information as -1, so I want to alter this using a filter, which I think I have done.
This is my Async Task which returns the JSON array.
// lets things run in the background, JSON Array to retrieve the list
class RetrieveNearestRestaurantsTask extends AsyncTask<Void, Void, JSONArray> {
private Exception exception;
// performs the search in the background / populates the arraylist
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
protected JSONArray doInBackground(Void... voids) {
// if the searchUrl conversion is empty
if (!searchUrl.toString().isEmpty()) {
// do this instead
try {
URL url = new URL(searchUrl.toString());
URLConnection tc = url.openConnection();
InputStreamReader isr = new InputStreamReader(tc.getInputStream());
BufferedReader in = new BufferedReader(isr);
String line; // variable
// while line in (read in) is not equal to null
// create a new object and output as line in JSON
while ((line = in.readLine()) != null) {
ja = new JSONArray(line);
// run through the length of the array
for (int i = 0; i < ja.length(); i++) {
JSONObject JO = (JSONObject) ja.get(i);
// output to meet Basic Functionality requirement
businessNames.add(JO.getString("BusinessName"));
postCodes.add(JO.getString("PostCode"));
addressList1.add(JO.getString("AddressLine1"));
addressList2.add(JO.getString("AddressLine2"));
addressList3.add(JO.getString("AddressLine3"));
// if the rating of the restaurant is -1, exempt
// should be displayed
ratingValue.add(JO.getString("RatingValue"));
ratingValue.stream()
.filter(x -> x.equals("-1"))
.findFirst()
.ifPresent(v -> System.out.println("Exempt"));
calcDistance.add(JO.getString("DistanceKM"));
// output everything together
ConcatenateSearchResults();
}
}
isr.close();
in.close();
//return ja;
} catch (Exception e) {
this.exception = e;
return ja;
} finally {
//is.close();
}
}
return ja;
}
Loads the search results.
protected void onPostExecute(JSONArray jsonArray) {
LoadSearchResults();
}
}
Concatenates the search results
private ArrayList<String> ConcatenateSearchResults()
{
int length = businessNames.size();
ArrayList<String> concatenatedResults = new ArrayList<>();
if(!businessNames.isEmpty() && !calcDistance.isEmpty() && !ratingValue.isEmpty()
&& !addressList1.isEmpty() && !addressList2.isEmpty() && !addressList3.isEmpty()
&& !postCodes.isEmpty())
{
for(int i=0; i < length; i++)
{
concatenatedResults.add("\n"+businessNames.get(i) +
"\nDistance (in Km) :"+ calcDistance.get(i) +
"\n\nRating: "+ ratingValue.get(i) +
"\nAddress Line 1: "+ addressList1.get(i) +
"\nAddress Line 2: "+ addressList2.get(i)+
"\nAddress Line 3: "+ addressList3.get(i) +
"\nPostcode: "+ postCodes.get(i));
}
}
return concatenatedResults;
}
}
However, I think somewhere I haven't enabled the proper ratingValue variable (?) to return the correct information (exempt instead of -1, where applicable, but I haven't worked out what I might have done wrong. Thank you for any help, I am slooowly getting better at explaining what I don't know.

Why turns the string to empty?

If I run this code the first 28 lines of the append.txt are full of numbers but then it is empty, why? It should also contains some numbers!!
Is the String get too big?
try {
OutputStream os = new FileOutputStream(new File("append.txt"), true);
String seq = "11", output = "";
int u = 1;
while(u<=37)
{
String temp = "";
int iter = 1;
for(int i=0; i<seq.length()-1; i++) {
if(seq.charAt(i)==seq.charAt(i+1)) {
iter++;
if(i==seq.length()-2) {
temp += iter + "" + seq.charAt(i);
iter=1;
}
}else {
temp += iter + "" + seq.charAt(i);
if(i==seq.length()-2) {
temp += "1" + seq.charAt(i+1);
}
iter=1;
}
}
seq=temp;
output += seq + "\n";
System.out.println(u + ": " + seq.length());
u++;
}
os.write(output.getBytes(), 0, output.length());
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Ok, I have the solution:
I use Eclipse to show the text file but it magically do not show the rest of the file. If I use any other text editor it will show the rest. Maybe the eclipse txt editor can only show a limited amount of text...
If the String would be too big to create you would get an OutOfMemoryException. This is not the case, your program works and prints 37 lines of numbers with last line being 48410 characters long.
Most likely the editor you are using to view the output fails to render these very long lines. It works in IntelliJ IDEA for me.

Jsoup Hanging Without Errors

I'm having an issue with Jsoup and hanging. I was testing Jsoup code, that was previously working, when out of nowhere it stopped working. I haven't changed any of the code since about a week ago, and until now, it has been working.
I've been trying to hit Wikipedia's Main Page to scrape links off of it for a homework assignment.
It hangs without throwing me any errors, nor does the program move past the URL connection .get() method. I waited about 10 minutes and still nothing happened.
Below is my code:
private WikiPage pullData(String url, WikiPage parent) {
WikiPage wp;
try {
String decodedURL = URLDecoder.decode(url, "UTF-8");
Document doc = Jsoup.connect(decodedURL).get();
Elements links = doc.select("a");
Elements paragraphs = doc.select("p");
Element t = doc.select("title").first();
StringBuilder words = new StringBuilder();
String title = t.text().replace(" - Wikipedia", "");
paragraphs.forEach(e -> {
words.append(e.text().toLowerCase());
});
wp = new WikiPage(url, title, parent);
for (int i = 0; i < AMOUNT_LINKS; i++) {
boolean properLink = false;
while (!properLink) {
//int rnd = R_G.nextInt(links.size());
String a = links.get(i).attr("href");
if (a.length() >= 5 && a.substring(0, 5).equals("/wiki") && containsChecker(a)) {
String BASE_URL = "https://en.wikipedia.org";
String decode = URLDecoder.decode(BASE_URL + a, "UTF-8");
wp.addChildren(decode);
properLink = true;
}
}
}
String[] splitWords = words.toString().replaceAll("[_$&+,:;=?##|'<>.^*()%!\\[\\]\\-\"/{}]", " ").split(" ");
for (String s : splitWords) {
if (s.length() >= 1) {
wp.addToWords(new WordCount(s, 1, 0));
}
}
System.out.printf("%1$-10s %2$-45s\n", counter, title);
counter++;
} catch (Exception e) {
e.printStackTrace();
return null;
}
return wp;
}
Here is a screenshot after running the program for 10 minutes with a break point at Elements links = doc.select("a"); :
Before hitting the .get() method
Hanging for about 10 minutes
I can't seem to see where the issue lies, and I've even tried different websites, but it just does not work at all.
Thanks for the help!

NullPointerException with listview on android

I am want to show this json data in to listview. Iam having this problem:
java.lang.NullPointerException: Attempt to invoke virtual method 'org.json.JSONObject org.json.JSONArray.getJSONObject(int)' on a null object reference
private String[] arrow() throws JSONException {
json = new JSONObject();
JSONArray com= null;
String[] list = new String[10];
try {
com = json.getJSONArray("parameters"); // this have 10 different values
} catch (JSONException e) {
e.printStackTrace();
}
for (int i = 0; i < 10; i++)
try {
json = com.getJSONObject(i);
String forward= json.getString("forward");
String back= json.getString("back");
list[i]="Forward: " + forward + "\n" + "Backward: " + back;
} catch (JSONException e) {
e.printStackTrace();
}
return list;
}
I did it with textview but cant do it with listview, i get this nullpointer..
Please help. Thanks.
There are multiple problems. The first problem is that you are using two try/catch. If you get exception in first one, you still go to 2nd try and try to run code.
json = com.getJSONObject(i);
this line is in 2nd try catch. In your case it seems com is null because there was exception in 1st try/catch.
try {
com = json.getJSONArray("parameters"); // this have 10 different values
for (int i = 0; i < com.length(); i++) {
json = com.getJSONObject(i);
String forward= json.getString("forward");
String back= json.getString("back");
list[i]="Forward: " + forward + "\n" + "Backward: " + back;
}
} catch (JSONException e) {
e.printStackTrace();
}

Issue with title text in Java

I have used Jtidy parser in java to fetch the title text.
String titleText=null;
try {
titleText = doc.getElementsByTagName("title").item(0)
.getFirstChild().getNodeValue();
} catch (Exception e1) {
try {
titleText = doc.getElementsByTagName("title").item(1)
.getFirstChild().getNodeValue();
} catch (Exception e2) {
try {
titleText = doc.getElementsByTagName("title").item(2)
.getFirstChild().getNodeValue();
} cathc (...)
}
}
above code is working fine,It is reading title at 0'th index,if not found then at 1'st index,and then at 2'nd index.But here I am getting issue:-for some page,title text is present at mid of page or below that,so this code is not working for such pages.In this way,for such condition, length of program is getting increased.Is there any other solution,which will read the title from entire page in one go?.Please help me.
I suggest you do it like this:
String titleText=null;
NodeList titles = doc.getElementsByTagName("title");
for (int i = 0; titleText == null && i < titles.getLength(); i++) {
try {
titleText = doc.item(i).getFirstChild().getNodeValue();
} catch (SomeException e) {
}
}

Categories