Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • my http requests class. I can run but dreambot can't.


    Essence

    Recommended Posts

    I made a http requests class. It makes a http request to a url and returns the response as json.  The requests class below is one that I've tested in another project. If we run a call to doPost, I receive a http 200 response and everything is good. However when I call doPost from Main's onLoop call and build my artifact, I can't even run it from the scripts folder in dreambot. When I click start nothing happens. If I remove the call to doPost, then I can start the script and it will cut logs or whatever I program it to do.

    package main;
    
    
    
    import java.io.IOException;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.util.EntityUtils;
    import org.json.JSONException;
    import org.json.JSONObject;
    import org.dreambot.api.script.AbstractScript;
    
    public class Requests {
        public static JSONObject doPost() throws JSONException {
            AbstractScript.log("we out here");
            HttpClient httpclient = HttpClientBuilder.create().build();
            HttpPost request = new HttpPost("https://my-api.com/prod/table");
            JSONObject result = new JSONObject();
            try {
                String bodyContent = "{  'data': {    'score': '5x1',     'time': '15:10'   },   'to' : 'bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...'}";
                StringEntity requestBody = new StringEntity(bodyContent);
                request.setEntity(requestBody);
                request.setHeader("Content-Type", "application/json");
                request.setHeader("Authorization", "key=AIzaSyZ-1u...0GBYzPu7Udno5aA");
                HttpResponse response = httpclient.execute(request);
                HttpEntity entity = response.getEntity();
                String responseString = EntityUtils.toString(entity);
                result.put("status", response.getStatusLine().getStatusCode());
                result.put("bodyContent", new JSONObject(responseString));
            } catch (ClientProtocolException e) {
                result.put("status", "500");
                result.put("bodyContent", "");
                e.printStackTrace();
            } catch (IOException e) {
                result.put("status", "500");
                result.put("bodyContent", "");
                e.printStackTrace();
            }finally {
                request.releaseConnection();
            }
            return result;
        }
    
    }

    And the main class I currently have is:

    package main;
    
    import org.dreambot.api.script.AbstractScript;
    import org.dreambot.api.script.Category;
    import org.dreambot.api.script.ScriptManifest;
    import org.dreambot.api.wrappers.interactive.GameObject;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    @ScriptManifest(
            author = "me",
            description = "stuff",
            category = Category.AGILITY,
            version=0.01,
            name="Wood"
    )
    
    public class Main extends AbstractScript {
        @Override
        public int onLoop(){
    
            log("calling request");
            Requests req = new Requests();
            try{
                JSONObject response = req.doPost();
    
            }catch (JSONException e){
                log("json exception");
            }
    
            return 1000;
        }
    
    
    }
    

    Is there a way to see the errors when dreambot tries to run my script? I can't see it through dreambots debug console because the script never runs.

    Link to comment
    Share on other sites

    17 minutes ago, Infidel said:

    Batch/Quick start. 
     

    
    java -Xms256M -Xmx1024M -jar -Xbootclasspath/p:C:/Users/userName/Dreambot/BotData/client.jar C:/Users/userName/Dreambot/Botdata/client.jar -world 999

     

    Thank you for your reply. I tried to adapt the code to ubuntu 16 which is what im working on:

     

    java -Xms256M -Xmx1024M -jar -Xbootclasspath /home/user/DreamBot/BotData/client.jar /home/user/DreamBot/BotData/client.jar -world 999

    But it gives me the error:

    Unrecognized option: -Xbootclasspath
    Error: Could not create the Java Virtual Machine.
    Error: A fatal exception has occurred. Program will exit.

    Im running java 8:

    user$ java -version
    openjdk version "1.8.0_242"
    OpenJDK Runtime Environment (build 1.8.0_242-8u242-b08-0ubuntu3~16.04-b08)
    OpenJDK 64-Bit Server VM (build 25.242-b08, mixed mode)

     

     

    Link to comment
    Share on other sites

    @Essence maybe try removing try catch block from doPost() and add exceptions to the method signature then the stack trace will show up in debug console or use MethodProvider.log() in doPost() catch clauses.

    Link to comment
    Share on other sites

    4 minutes ago, johnycoool said:

    @Essence maybe try removing try catch block from doPost() and add exceptions to the method signature then the stack trace will show up in debug console or use MethodProvider.log() in doPost() catch clauses.

    Okay I ran the code in debug mode and it's saying

    exception in thread java.lang.noclassdeffounderror: org/json/jsonexception

    Is this saying they can't find the jsonexception class? I added the java-json .jar file aswell as some other .jar filesd to the library to my project for the methods to work. Do I need to add these .jar files elsewhere to make things run.

    Link to comment
    Share on other sites

    9 minutes ago, Essence said:

    Is this saying they can't find the jsonexception class? I added the java-json .jar file aswell as some other .jar filesd to the library to my project for the methods to work. Do I need to add these .jar files elsewhere to make things run.

    Yes, you need to. Here is the answer for how to do it.

    Link to comment
    Share on other sites

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.