How can I modify a HTTP header for reading a JSONObject?

edited January 9 in Android Mode

I need to modify the HTTP header like this: Please modify your client to use a HTTP header named "X-Auth-Token" with the underneath personal token as value.

Your API token: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

this is my code:

String image = "";
String name = "";
String position = "";

void setup() {
  size(800, 860);
  textSize(20);
}

void draw() {
    getInfo();
      background(0);
      text(name,10,820);
      text(position,10,850);
    }
  }
}

void getInfo() {
try {
    JSONObject info = loadJSONObject("http:" + "//api.football-data.org/v1/teams/66/players");
    name = info.getString("name");
    position = info.getString("position");
  }
  catch (Exception e) {
    println("rats... " + e);
  }
}

Can someone please help me? (I'm new to processing)

Answers

  • there is an http request library that allows you to specify headers (iirc).

    it has a jsonget example... use addHeader() to add the, er, header.

  • edited January 8

    I have changed the code. It is in android mode.

    long Team;
    int shortName;
    int Value;
    String name;
    
    String url1 = "http://" + "api.football-data.org/v1/teams/";
    String teamNumber = "66";
    String url = url1 + teamNumber;
    
    int refreshDelay = 5000;
    long lastTime;
    
    void setup(){
    size(1440, 2560);
    lastTime =  millis()-refreshDelay;
    fill(175,100,220);
    rect(0,0,1440, 200);
    fill(0);
    textSize(178);
    
    fill(255);
    rectMode(CENTER);
    rect(720,1280,800,300);
    fill(0);
    textSize(150);
    text("SEE TEAM INFO", 330, 675);
    }
    
    void draw(){
    
    if ((millis() - lastTime) > refreshDelay) {
    lastTime = millis();
    if(mousePressed){
              if(mouseX>((720)-(400)) && mouseX <((720)+(400)) && mouseY>((640)-(150)) && mouseY<((640)+(150))){
                getInfo();
                fill(0);
                textSize(140);
                text("teamNumber: "+teamNumber, 10, 900);
                text("Team: "+Team, 10, 1050);
                text("Level: "+shortName, 10, 1200);
                text("Points: "+Value, 10, 1350);
              }
        } 
      }
    }
    
    void getInfo(){
      try{
        addHeader("X-Auth-Token","XXXXXXXXXXXXXXXXXXXXX");
          JSONObject info = loadJSONObject(url); 
          Team = info.getLong("team"); 
          shortName = info.getInt("shortName"); 
          Value = info.getInt("Value"); 
    
       } catch (Exception e) {
           println("rats...");
           }
    }
    

    But the addHeader() is not working. Can someone tell me how I can let it work?

  • you have to replace the loadJSONObject with the http library.

    however, i'm not sure if this works with android mode...

  • edited January 9

    @Naecekki===create your connection with httpUrlConnection() then setRequestProperty() it runs with android; more details here: https://docs.oracle.com/javase/7/docs/api/java/net/HttpURLConnection.html

Sign In or Register to comment.