2015-11-24 2 views
0

J'essaie de lire un lien API qui contient des données au format JSON. ce que je suis en train de lire et d'afficher sur le téléphone:Analyse du format JSON à partir d'une connexion HttpConnection

[[],{"index":"2","name":"jim","email":"[email protected]","contact":"0122511336","username":"jim","password":"jim","photo":"student.jpg","status":"Active"}] 

Le programme censé authentifier les entrées de nom d'utilisateur et mot de passe et ce dipsplay le contenu de ce qui précède depuis l'URL.

Voici les codes et jusqu'ici ce que j'ai fait. S'il vous plaît, quelqu'un m'aidera à le faire.

 package org.json.me; 

    import java.io.*; 
    import javax.microedition.io.*; 
    import javax.microedition.lcdui.*; 
    import javax.microedition.midlet.*; 
public class Login extends MIDlet implements CommandListener { 

private String name; 
private String email; 
private int contact; 
private String username; 
private String password; 
private String status; 
private final String JSONformat = null; 

public String getName() { 
    return name; 
} 
public void setName(String name) { 
    this.name=name; 
} 
public String getEmail() { 
    return email; 
} 
public void setEmail(String email) { 
    this.email=email; 
} 
public int getContact() { 
    return contact; 
} 
public void setContact (int contact) { 
    this.contact=contact; 
} 
public String getUsername() { 
    return username; 
} 
public void setUsername (String username) { 
    this.username = username; 
} 
public String getPassword() { 
    return password; 
} 
public void setPassword (String Password) { 
    this.password = password; 
} 
public String getStatus() { 
    return status; 
} 
public void setStatus(String status) { 
    this.status=status; 
} 
public String toString() { 
    return getName()+""+getEmail()+""+getContact()+""+getUsername()+""+getPassword()+""+getStatus(); 
} 
public String fromJSON(String jsonString) { 

    try { 
     JSONObject json = new JSONObject(jsonString); 
     setName(json.getString("name")); 
     setEmail(json.getString("email")); 
     setContact(json.getInt("contact")); 
     setUsername(json.getString("username")); 
     setPassword(json.getString("password")); 
     setStatus(json.getString("status")); 
    } 
    catch (JSONException e) { 
     e.printStackTrace(); 
    } 

    return JSONformat; 
}  
TextField UserName = null; 
TextField Password = null; 
Form authForm, mainscreen; 
TextBox t = null; 
StringBuffer b = new StringBuffer(); 

private Display myDisplay = null; 
private Command okCommand = new Command("OK", Command.OK, 1); 
private Command exitCommand = new Command("Exit", Command.EXIT, 2); 
private Command backCommand = new Command("Back", Command.BACK, 2); 
private Alert alert = null; 

    public Login() { 

myDisplay = Display.getDisplay(this); 

UserName = new TextField("Username", "", 10, TextField.ANY); 
Password = new TextField("Password", "", 10, TextField.ANY); 
authForm = new Form("Identification"); 
mainscreen = new Form("Logging IN"); 
mainscreen.append("Logging in...."); 
mainscreen.addCommand(backCommand); 
authForm.append(UserName); 
authForm.append(Password); 
authForm.addCommand(okCommand); 
authForm.addCommand(exitCommand); 
authForm.setCommandListener(this); 
myDisplay.setCurrent(authForm); 

} 

public void startApp() throws MIDletStateChangeException { 
} 

public void pauseApp() { 
} 

protected void destroyApp(boolean unconditional) 
    throws MIDletStateChangeException { 
} 

public void commandAction(Command c, Displayable d) { 

    if ((c == okCommand) && (d == authForm)) { 
    if (UserName.getString().equals("") || Password.getString().equals("")) { 
    alert = new Alert("Error", "You should enter Username and Password", null,AlertType.ERROR); 
    alert.setTimeout(Alert.FOREVER); 
    myDisplay.setCurrent(alert); 
    } 

    else { 
     //myDisplay.setCurrent(mainscreen); 
     login(UserName.getString(), Password.getString()); 
     } 
    } 
    if ((c == backCommand) && (d == mainscreen)) { 
    myDisplay.setCurrent(authForm); 
    } 
    if ((c == exitCommand) && (d == authForm)) { 
    notifyDestroyed(); 
    } 
    } 

    public void login(String UserName, String PassWord) { 
     HttpConnection connection = null; 
     DataInputStream in = null; 
     String base = "http://sunday-tech.com/chunghua/api/login.php"; 
     String url = base + "?username=" + UserName + "&password=" + PassWord; 

     OutputStream out = null; 
try 
{ 
    connection = (HttpConnection) Connector.open(url); 
    connection.setRequestMethod(HttpConnection.POST); 
    connection.setRequestProperty("IF-Modified-Since", "2 Oct 2002          15:10:15 GMT"); 
    connection.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0"); 
    connection.setRequestProperty("Content-Language", "en-CA"); 
    connection.setRequestProperty("Content-Length", "" + (UserName.length() + PassWord.length())); 
    connection.setRequestProperty("UserName", UserName); 
    connection.setRequestProperty("PassWord", PassWord); 
    out = connection.openDataOutputStream(); 
    out.flush(); 
    in = connection.openDataInputStream(); 
    int ch; 
    while ((ch = in.read()) != -1) { 
     b.append((char) ch); 
     //System.out.println((char)ch); 
    } 
    mainscreen.append(fromJSON(b.toString())); 
    if (in != null) { 
     in.close(); 
    } 
    if (out != null) { 
     out.close(); 
    } 
    if (connection != null) { 
     connection.close(); 
    } 
} 
catch (Exception x) { 
} 
myDisplay.setCurrent(mainscreen); 

    } 

} 

Répondre

0

Modifiez vos méthodes setter pour également modifier les valeurs TextField. Par exemple

public void setUsername (String username) { 
    this.username = username; 
    this.UserName.setString(username); 
} 
+0

il ne marche pas toujours afficher les objets pour moi, il affiche des caractères entiers et je ne sais pas quoi faire .... que la production de l'API est tellement confus, devrais-je faire JSONObject au sein JSONArray ou comment ? si quelqu'un juste plz montrez-moi les codes de la façon d'obtenir ces objets et les ajouter je serais vraiment sacrément heureux :(cela fait des jours et je recherche sans le faire fonctionner –

+0

Votre méthode fromJSON devrait extraire l'information, à droite Déboguer ces lignes pour confirmer –