I found a couple of ways to approach this. First, get it from a file through an internet stream. The other is locally.
INTERNET WAY
My first method involved storing the file in my public Dropbox folder. I got a generic link (make sure it ends with .json ) and pasted it into the example line "https://dl.dropboxusercontent.com/..EXAMPLE-CREDENTIALS"
@ApiMethod(name = "sayHi") public MyBean sayHi(@Named("name") String name) { MyBean mModelClassObject = null; String text = ""; try { String line = ""; StringBuilder builder = new StringBuilder(); URL url = new URL("https://dl.dropboxusercontent.com/..EXAMPLE-CREDENTIALS"); BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream())); while ((line = reader.readLine()) != null) {
LOCAL WAY
Another way is to take the version above and skip something like dropbox
@ApiMethod(name = "sayHi") public MyBean sayHi(@Named("name") String name) { MyBean mModelClassObject = null; String text = "JUST PASTE YOUR JSON CONTENTS HERE"; InputStream stream = new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8)); FirebaseOptions options = null; options = new FirebaseOptions.Builder() .setServiceAccount(stream) .setDatabaseUrl("https://[PROJECT-ID].firebaseio.com/") .build(); FirebaseApp.initializeApp(options); DatabaseReference ref = FirebaseDatabase.getInstance().getReference(); final TaskCompletionSource<MyBean> tcs = new TaskCompletionSource<>(); Task<MyBean> tcsTask = tcs.getTask(); ref.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { MyBean result = dataSnapshot.getValue(MyBean.class); if(result != null){ tcs.setResult(result); } } @Override public void onCancelled(DatabaseError databaseError){
I do not know if this is in line with best practice, but my project is working now. I also included firebase code for information. look at this answer to a question I recently asked about reading and writing in firebase.
EDIT
cleaned version that does not throw errors
public class MyEndpoint { private FirebaseOptions options; private DatabaseReference ref; private String serviceAccountJSON = "i took mine out for security reasons"; // create firebase instance if need be private void connectToFirebase(){ if (options == null) { options = null; options = new FirebaseOptions.Builder() .setServiceAccount(new ByteArrayInputStream(serviceAccountJSON.getBytes(StandardCharsets.UTF_8))) .setDatabaseUrl("https://[PROJECT-ID].firebaseio.com/") .build(); FirebaseApp.initializeApp(options); } if(ref == null) { ref = FirebaseDatabase.getInstance().getReference(); } } /** A simple endpoint method that takes a name and says Hi back */ @ApiMethod(name = "sayHi") public MyBean sayHi(@Named("name") String name) { // always do this first connectToFirebase(); MyBean mModelClassObject = null; final TaskCompletionSource<MyBean> tcs = new TaskCompletionSource<>(); Task<MyBean> tcsTask = tcs.getTask(); // get the info ref.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { MyBean result = dataSnapshot.getValue(MyBean.class); if(result != null){ tcs.setResult(result); } } @Override public void onCancelled(DatabaseError databaseError){ //handle error } }); // wait for it try { mModelClassObject = Tasks.await(tcsTask); }catch(ExecutionException e){ //handle exception }catch (InterruptedException e){ //handle exception } mModelClassObject.setData(mModelClassObject.getData() + name); return mModelClassObject; } }
source share