loadStrings(url) isn't working on Android mode

edited January 2018 in Android Mode

Hello,

when I tried changing the mode from Java, where the program was working, to Android mode, where I get the path separator error.

I'm putting in the following code, on which it crashes

loadStrings("http://mglolenstine.xyz/test.txt");

and I get the following error:

` FATAL EXCEPTION: Animation Thread Process: processing.test.monitor, PID: 19520 java.lang.IllegalArgumentException: File http://mglolenstine.xyz/text.txt contains a path separator at android.app.ContextImpl.makeFilename(ContextImpl.java:1944) at android.app.ContextImpl.getFileStreamPath(ContextImpl.java:549) at android.content.ContextWrapper.getFileStreamPath(ContextWrapper.java:193) at processing.core.PSurfaceNone.getFileStreamPath(Unknown Source) at processing.core.PApplet.sketchPath(Unknown Source) at processing.core.PApplet.createInputRaw(Unknown Source) at processing.core.PApplet.createInput(Unknown Source) at processing.core.PApplet.loadStrings(Unknown Source) at processing.test.trtl_monitor.Monitor.draw(Monitor.java:55) at processing.core.PApplet.handleDraw(Unknown Source) at processing.core.PSurfaceNone.callDraw(Unknown Source) at processing.core.PSurfaceNone$AnimationThread.run(Unknown Source)

`

And I don't understand... I've read this article, but it doesn't seem to help me, as the OP fixed it by adding HTTP:// in front of 'his' url.

Answers

  • edited January 2018 Answer ✓

    After searching for android solutions not related to Processing, I've found this piece of code:

    String getHttp(String url) {
       try {
            URLConnection connection = (new URL(url)).openConnection();
            connection.setConnectTimeout(refreshrate);
            connection.setReadTimeout(refreshrate);
            connection.connect();
            InputStream in = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            StringBuilder html = new StringBuilder();
            for (String s; (s = reader.readLine()) != null; ) {
                html.append(s);
            }
            in.close();
            return html.toString();
        }catch(IOException e) {
            e.printStackTrace();
        }
        return "";
    }
    

    Just wanted to share, so if anyone has the same problem it can be easily resolved!

  • Thxs for sharing.

    Kf

  • edited January 2018

    @MGlolenstine

    I tested with the following code and it worked. I have internet permissions enabled. Notice I was getting this warning:

    (HTTPLog)-Static: isSBSettingEnabled false
    (HTTPLog)-Static: isSBSettingEnabled false

    Although it seems it was addressed already: https://github.com/processing/processing-android/issues/332

    Other searches return this is a warning associated with Samsung devices (my case).

    Kf

    //===========================================================================
    // IMPORTS:
    
    import android.app.Activity;
    import android.content.Context;
    import android.widget.FrameLayout;
    //import android.app.Fragment;
    
    import android.os.Environment;
    import android.graphics.Color;
    import android.widget.Toast;
    import android.os.Looper;
    import android.view.WindowManager;
    import android.os.Bundle;
    import android.view.ViewParent;
    import android.view.ViewGroup;
    import android.view.View;
    import android.widget.RelativeLayout;
    import android.view.LayoutInflater;
    import android.R.string;
    
    
    //===========================================================================
    // FINAL FIELDS:
    
    
    //===========================================================================
    // GLOBAL VARIABLES:
    
    Activity act;
    Context mC;
    String myMsg=null;
    
    //===========================================================================
    // PROCESSING DEFAULT FUNCTIONS:
    
    void setup() {
      //size(400,600);
      fullScreen();
      orientation(PORTRAIT);
      //orientation(LANDSCAPE;)
    
      act = this.getActivity();
      Looper.prepare();
    
      textAlign(CENTER, CENTER);
      rectMode(CENTER);
    
      fill(255);
      strokeWeight(2);
      textSize(32);
    }
    
    void draw() {
      background(0);
      text("Welcome to my test code on "+ day()+"/"+month(), width/2, height/4);
      text("My message="+myMsg, width/2, height/2);
    }
    
    void mouseReleased() {
      String[] rx=loadStrings("http: //mglolenstine.xyz/test.txt");
      myMsg="NOTHING";
      if (rx!=null) {
        myMsg=join(rx, " : ");
      }
    }
    
  • @kfrajer interesting...

    So you just use HTML and it works?

    Could you explain why it works?

    thanks :D

  • @MGlolenstine===

    i don't understand : though inputStream can be used more simple is to write

        String[] txt;
    
        public void setup(){
    
        txt = loadStrings("http://www.mglolenstine.xyz/test.txt");
        println(txt[0]);
    
        ///returns: "this is a test file!"
    

    }

  • Answer ✓

    @MGlelenstine

    Not sure why yours didn't work. If you post and MCVE, then one can either reproduce the problem or understand your approach and probably suggest what needs to be modified or an alternative approach. What I did hear was a simple test demo showing it works. I am not familiar with the calls Processing Android is doing under the hood. However, it is very easy to understand it if you check the Github repo.

    Kf

Sign In or Register to comment.