feat: price input and author fetching
This commit is contained in:
parent
ce65b1b7e4
commit
e2600b2cbb
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
@ -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,27 +90,22 @@ public class ScannerFragment extends Fragment {
|
||||
String ean = result.getContents();
|
||||
Toast.makeText(getActivity(), "Scanned: " + ean, Toast.LENGTH_LONG).show();
|
||||
Thread thread = new Thread(() -> {
|
||||
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());
|
||||
JSONObject res = getJSONFromUrl(bookURL);
|
||||
try {
|
||||
String title = res.getString("title");
|
||||
Book book = new Book(title, "", ean, MainActivity.avril, 0);
|
||||
JSONArray authors = res.getJSONArray("authors");
|
||||
String author = "N/A";
|
||||
if (authors.length() > 0) {
|
||||
String authorId = authors.getJSONObject(0).getString("key");
|
||||
try {
|
||||
JSONObject authorRes = getJSONFromUrl("https://openlibrary.org" + authorId + ".json");
|
||||
if (authorRes != null) author = authorRes.getString("name");
|
||||
} catch (JSONException ex) {
|
||||
Log.e("App", "Failure", ex);
|
||||
}
|
||||
}
|
||||
Book book = new Book(title, author, ean, MainActivity.avril, 0);
|
||||
getActivity().runOnUiThread(() -> {
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
||||
builder.setTitle("Prix");
|
||||
@ -132,26 +128,6 @@ public class ScannerFragment extends Fragment {
|
||||
} 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());
|
||||
}
|
||||
});
|
||||
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
|
||||
|
@ -39,7 +39,8 @@ public class ScannerItemsAdapter extends RecyclerView.Adapter<ScannerItemsAdapte
|
||||
@Override
|
||||
public void onBindViewHolder(ViewHolder holder, int position) {
|
||||
Book book = mData.get(position);
|
||||
holder.myTextView.setText(String.format("%s (%s) - %s", book.getTitle(), book.getPrice()!=0 ? book.getPrice()+"€" : "PL", book.getEAN()));
|
||||
holder.myTextView.setText(String.format("%s (%s) - %s - %s",
|
||||
book.getTitle(), book.getPrice()!=0 ? book.getPrice()+"€" : "PL", book.getAuthor(), book.getEAN()));
|
||||
}
|
||||
|
||||
// total number of rows
|
||||
|
Loading…
Reference in New Issue
Block a user