How to use ConnectionRequest

42 views
Skip to first unread message

Rudolph Matongo

unread,
Nov 5, 2015, 10:54:57 AM11/5/15
to CodenameOne Discussions
Hi 
I am just 6 days into using CodenameOne so bear with me.  I want to use the ConnectionRequest class to access a JSON object from a php script.  I have declared a NetworkManager object and started it.  I have declared the ConnectionRequest object and written an override of the readResponse method,now I want to get the input from the user and post it to the php script and get the JSON but I am just not able to figure it out
Below is the class I have written for this.

public class AuthenticateStudent {

    boolean findStudent(String studentname, String studentpassword){
        System.out.println("Getting the student name");

        NetworkManager networkManager = NetworkManager.getInstance();
        networkManager.start();
        networkManager.addErrorListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                NetworkEvent n = (NetworkEvent) evt;
                n.getError().printStackTrace();
                System.out.println(n.getError());
            }
        });
        
        ConnectionRequest request;
        request = new ConnectionRequest() {

            int chr;
            StringBuffer sb = new StringBuffer();
            String response = "";
          
        @Override
        protected void readResponse(InputStream input) throws IOException {

                JSONParser parser = new JSONParser();
                Hashtable hm = parser.parse(new InputStreamReader(input));

                Vector vector = new Vector();
                vector = (Vector) hm.get("homework");
                
                if (vector.size() > 0) {
                    for (int i = 0; i < vector.size(); i++) {

                        Hashtable h = (Hashtable) vector.get(i);
                        Homework homework = new Homework();
                        homework.setId(Integer.parseInt(h.get("id").toString()));
                        homework.setUsername(h.get("username").toString());
                        homework.setActivation(h.get("activation").toString());
                        homework.setInstallation(h.get("installation").toString());
                        homework.setPassword(h.get("password").toString());
                        homework.setSubject(h.get("subject").toString());
                        homework.setStudentclass(h.get("studentclass").toString());
                        homework.setDescription(h.get("description").toString());
                        homework.setTeacher(h.get("teacher").toString());
                        homework.setAssignedDate(h.get("assignedDate").toString());
                        homework.setDueDate(h.get("dueDate").toString());
                        homework.setWfield1(h.get("wfield1").toString());
                        homework.setWfield2(h.get("wfield2").toString());
                        homework.setWfield3(h.get("wfield3").toString());
                        homework.setWfield4(h.get("wfield4").toString());
                        //dailyHomework.add(homework);

                    }
                } else {
                    Homework homework = new Homework();
                    homework.setUsername("Sorry...");
                    homework.setDescription("No messages today");
                    //dailyHomework.add(homework);
                }
            }

            @Override
            protected void handleException(Exception err) {
                System.err.println(err);
                 Homework homework = new Homework();
                    homework.setUsername("Ooops...");
                    homework.setDescription("Please check your internet connection ");
                    //dailyHomework.add(homework);
        }       
        };
    return false;
        
        
    }
   
    

}

Can anyone help me out

Rudolph Matongo

unread,
Nov 5, 2015, 11:14:35 AM11/5/15
to CodenameOne Discussions
update:  I have changed the php script to just output a raw JSON array which I want to use in my Java application.  I have the JSON stored in a Map object but I want to convert that to a vector so that it is easier to look for strings.  Please help 

Shai Almog

unread,
Nov 5, 2015, 11:41:18 PM11/5/15
to CodenameOne Discussions
Hi,
I don't quite understand what's working and what isn't working.
You need to provide sample JSON so we can understand what you are trying to read.

The trick for doing this is quite easy. Place a debugger breakpoint on the parse line then step over line by line.
Use the variable inspector to look at the variables you are about to extract and what they contain then fix your code accordingly. Rinse, repeat until the JSON is properly parsed.

Rudolph Matongo

unread,
Nov 6, 2015, 1:19:03 AM11/6/15
to CodenameOne Discussions
I did that now and it worked
Reply all
Reply to author
Forward
0 new messages