diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/java/dev/ninjdai/balscanner/ui/scanner/ScannerFragment.java b/app/src/main/java/dev/ninjdai/balscanner/ui/scanner/ScannerFragment.java index 0ad537b..b376c85 100644 --- a/app/src/main/java/dev/ninjdai/balscanner/ui/scanner/ScannerFragment.java +++ b/app/src/main/java/dev/ninjdai/balscanner/ui/scanner/ScannerFragment.java @@ -25,6 +25,7 @@ import androidx.recyclerview.widget.RecyclerView; import com.journeyapps.barcodescanner.ScanContract; import com.journeyapps.barcodescanner.ScanOptions; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -89,68 +90,43 @@ public class ScannerFragment extends Fragment { String ean = result.getContents(); Toast.makeText(getActivity(), "Scanned: " + ean, Toast.LENGTH_LONG).show(); Thread thread = new Thread(() -> { + String bookURL="https://openlibrary.org/isbn/"+ean+".json"; + JSONObject res = getJSONFromUrl(bookURL); try { - String bookURL="https://openlibrary.org/isbn/"+ean+".json"; - URLConnection urlConn = null; - BufferedReader bufferedReader = null; - try - { - URL url = new URL(bookURL); - urlConn = url.openConnection(); - bufferedReader = new BufferedReader(new InputStreamReader(urlConn.getInputStream())); - - StringBuffer stringBuffer = new StringBuffer(); - String line; - while ((line = bufferedReader.readLine()) != null) - { - stringBuffer.append(line); - } - - JSONObject res = new JSONObject(stringBuffer.toString()); + String title = res.getString("title"); + JSONArray authors = res.getJSONArray("authors"); + String author = "N/A"; + if (authors.length() > 0) { + String authorId = authors.getJSONObject(0).getString("key"); try { - String title = res.getString("title"); - Book book = new Book(title, "", ean, MainActivity.avril, 0); - getActivity().runOnUiThread(() -> { - AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); - builder.setTitle("Prix"); - - final EditText input = new EditText(getActivity()); - input.setInputType(InputType.TYPE_CLASS_NUMBER); - builder.setView(input); - - builder.setPositiveButton("Valider", (dialog, which) -> { - book.setPrice(Integer.parseInt(input.getText().toString())); - MainActivity.books.computeIfPresent(MainActivity.avril, ((owner, ownerBooks) -> { - ownerBooks.add(book); - return ownerBooks; - })); - updateItems(); - }); - builder.setNegativeButton("Annuler", (dialog, which) -> dialog.cancel()); - builder.show(); - }); + JSONObject authorRes = getJSONFromUrl("https://openlibrary.org" + authorId + ".json"); + if (authorRes != null) author = authorRes.getString("name"); } catch (JSONException ex) { Log.e("App", "Failure", ex); } } - catch(Exception ex) - { - Log.e("App", "OpenLibraryFetchTask", ex); - } - finally - { - if(bufferedReader != null) - { - try { - bufferedReader.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - //Your code goes here - } catch (Exception e) { - Log.e("App", e.getMessage()); + Book book = new Book(title, author, ean, MainActivity.avril, 0); + getActivity().runOnUiThread(() -> { + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + builder.setTitle("Prix"); + + final EditText input = new EditText(getActivity()); + input.setInputType(InputType.TYPE_CLASS_NUMBER); + builder.setView(input); + + builder.setPositiveButton("Valider", (dialog, which) -> { + book.setPrice(Integer.parseInt(input.getText().toString())); + MainActivity.books.computeIfPresent(MainActivity.avril, ((owner, ownerBooks) -> { + ownerBooks.add(book); + return ownerBooks; + })); + updateItems(); + }); + builder.setNegativeButton("Annuler", (dialog, which) -> dialog.cancel()); + builder.show(); + }); + } catch (JSONException ex) { + Log.e("App", "Failure", ex); } }); thread.start(); @@ -169,7 +145,6 @@ public class ScannerFragment extends Fragment { JSONObject getJSONFromUrl(String fetchURL) { URLConnection urlConn = null; BufferedReader bufferedReader = null; - JSONObject object = null; try { URL url = new URL(fetchURL); urlConn = url.openConnection(); @@ -181,10 +156,12 @@ public class ScannerFragment extends Fragment { stringBuffer.append(line); } - object = new JSONObject(stringBuffer.toString()); + bufferedReader.close(); + return new JSONObject(stringBuffer.toString()); } catch (Exception e) { - Log.e("App", e.getMessage()); + Log.e("BALScanner", fetchURL); + Log.e("BALScanner", "getJsonFromUrl", e); } finally { @@ -197,7 +174,7 @@ public class ScannerFragment extends Fragment { } } } - return object; + return null; } @Override diff --git a/app/src/main/java/dev/ninjdai/balscanner/ui/scanner/ScannerItemsAdapter.java b/app/src/main/java/dev/ninjdai/balscanner/ui/scanner/ScannerItemsAdapter.java index 6abec39..18e72a0 100644 --- a/app/src/main/java/dev/ninjdai/balscanner/ui/scanner/ScannerItemsAdapter.java +++ b/app/src/main/java/dev/ninjdai/balscanner/ui/scanner/ScannerItemsAdapter.java @@ -39,7 +39,8 @@ public class ScannerItemsAdapter extends RecyclerView.Adapter