我正在尝试使用webservice从mysql数据库中检索数据。 php文件将执行查询,然后它将在ContactsService.java中解析,但我不断收到此错误:
java.lang.NullPointerException
[EDT] 0:0:5,954 - 在行中解析JSON令牌时,键值预期为true:2列:58 buffer:e1elxde-exe-e-edl [EDT] 0:0:5,954 - 在行解析JSON标记时,键值的预期为null:2列:79缓冲区:e1elxde-exe-e-edlde1ell at com.socialpro.services.ContactsService.getAllFriends(ContactsService.java:33) [EDT] 0:0:5,954 - 在行解析JSON标记时,键值的预期为null:2列:92缓冲区:e1elxde-exe-e-edlde1ell0elldd [EDT] 0:0:5,954 - 在行解析JSON标记时,键值预期为true:3列:2缓冲区:e1elxde-exe-e-edlde1ell0elldd1 at com.socialpro.presentation.ContactsForm $ 5.actionPerformed(ContactsForm.java:103) [EDT] 0:0:5,954 - 在行解析JSON标记时,键值预期为true:3列:3缓冲区:e1elxde-exe-e-edlde1ell0elldd1 at com.socialpro.presentation.ContactsForm $ 5.actionPerformed(ContactsForm.java:98) [EDT] 0:0:5,954 - 解析行中的JSON令牌时键值的预期值为null:3列:7缓冲区:e1elxde-exe-e-edlde1ell0elldd1l [EDT] 0:0:5,954 - 在行中解析JSON令牌时,键值预期为真:3列:10缓冲区:e1elxde-exe-e-edlde1ell0elldd1lef [EDT] 0:0:5,954 - 解析行中的JSON令牌时键值的预期值为null:3列:34缓冲区:e1elxde-exe-e-edlde1ell0elldd1leflf57900l
这是ContactsForm.java:
public class ContactsForm {
Form fcont;
String cr;
Container Cc;
private Image img1;
ContactsForm(Resources theme) {
cr = CurrentUser.currentRole;
System.out.println(cr);
UIBuilder ui = new UIBuilder();
fcont = new Form("Mes contacts", BoxLayout.y());
fcont.setUIID("background");
Cc = new Container(new BoxLayout(BoxLayout.Y_AXIS));
Cc.setUIID("ContainerLogin");
img1 = theme.getImage("logo.png");
fcont.getToolbar().addCommandToOverflowMenu("Logout", null, new
ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
LoginForm login = new LoginForm(theme);
login.getF().showBack();
}
});
fcont.getToolbar().addCommandToSideMenu("Changer mes infos", null, new
ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
System.out.println("you are a person!!");
UpdateProfileForm update = new UpdateProfileForm(theme);
update.getFu().show();
}
});
fcont.getToolbar().addCommandToSideMenu("Mes contacts", null, new
ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
ContactsForm cont = new ContactsForm(theme);
cont.getFcont().show();
}
});
fcont.getToolbar().addCommandToSideMenu("Membres de SocialPro", null,
new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
//ContactsForm cont = new ContactsForm(theme);
//cont.getFcont().show();
}
});
ConnectionRequest con = new ConnectionRequest();
con.setUrl("http://127.0.0.1/SocialProMobile/contacts.php");
con.addResponseListener(new ActionListener<NetworkEvent>() {
@Override
public void actionPerformed(NetworkEvent evt) {
ContactsService conServ=new ContactsService();
ArrayList<Friends> arrFr=conServ.getAllFriends(new String(con.getResponseData()));
Iterator i=arrFr.iterator();
while(i.hasNext())
{
Friends f=(Friends) i.next();
Cc.add(addItem(f));
}
fcont.add(Cc);
fcont.refreshTheme();
}
});
NetworkManager.getInstance().addToQueue(con);
}
public Container addItem(Friends fri) {
Container C1 = new Container(new BoxLayout(BoxLayout.X_AXIS));
Container C2 = new Container(new BoxLayout(BoxLayout.Y_AXIS));
Label nom =new Label(fri.getNomfriend());
// SpanLabel publi = new SpanLabel(fri.get());
C2.add(nom);
//C1.add(new Label("img"));
C1.add(C2);
C1.setLeadComponent(nom);
return C1;
//f.refreshTheme();
}
public Form getFcont() {
return fcont;
}
}
ContactsService.java :
public class ContactsService {
public ArrayList<Friends> getAllFriends(String json)
{
ArrayList<Friends> frArr=new ArrayList<Friends>();
try {
JSONParser j = new JSONParser();
Map<String, Object> contacts = j.parseJSON(new
CharArrayReader(json.toCharArray()));
System.out.println();
List<Map<String, Object>> list = (List<Map<String, Object>>)
contacts.get("contact");
for (Map<String, Object> obj : list) {
Friends f = new Friends();
f.setNomfriend((obj.get("id").toString()));
frArr.add(f);
}
} catch (IOException ex) {
}
return frArr;
}
}
contacts.php:
<?php
require_once('connect.php');
$sql = " SELECT nomfriend FROM friends WHERE id='173'"
$result = $conn->query($sql);
$json = new SimpleXMLElement('<xml/>');
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$mydata = $json->addChild('contact');
$mydata->addChild('nomfriend',$row['nomfriend']);
}
$return_arr=$json->addChild('contact');
echo json_encode($json);
} else {
echo "false";
}
$conn->close();
?>