我在控制器中有以下方法:
public class FoundController {
private JSONObject accessPoint;
private JSONArray accessPoints;
private List<WlanNetwork> accessPointlist;
private Gson gson;
public FoundController() {
this.accessPoint = new JSONObject();
this.accessPoints = new JSONArray();
this.accessPointlist = new ArrayList<>();
this.gson = new Gson();
}
public List<WlanNetwork> getFoundWlanNetworks() {
accessPoints = CommonHelper.getWifiList(Constants.Global.FILE_WPA_SUPLICANT_CONF);
accessPointlist = new ArrayList<WlanNetwork>();
for (int i = 0; i < accessPoints.length(); i++) {
try {
Logger.d(accessPoints.getJSONObject(i).toString());
WlanNetwork accessPointPOJO = gson.fromJson(accessPoints.getJSONObject(i).toString(), WlanNetwork.class);
Logger.d(accessPointPOJO.getSssid());
accessPointlist.add(accessPointPOJO);
} catch (Exception e) {
Logger.e("Exception");
Logger.e(e.getMessage());
}
}
Logger.d(String.valueOf(accessPointlist.size()));
return accessPointlist;
}
}
方法:
accessPoints.getJSONObject(i).toString()
返回:
{"key_mgmt":"NONE","priority\"":"97","ssid":"test"}
但是如果我尝试使用:
转换为POJO对象WlanNetwork accessPointPOJO = gson.fromJson(accessPoints.getJSONObject(i).toString(), WlanNetwork.class);
我总是得到以下异常:
No message/exception is set
模型类如下:
public class WlanNetwork {
private String sssid;
private String key_mgmt;
private String sim_slot;
private String imsi;
private String priority;
public WlanNetwork(String sssid, String key_mgmt, String sim_slot, String imsi, String priority) {
super();
this.sssid = sssid;
this.key_mgmt = key_mgmt;
this.sim_slot = sim_slot;
this.imsi = imsi;
this.priority = priority;
}
public String getSssid() {
return sssid;
}
public void setSssid(String sssid) {
this.sssid = sssid;
}
public String getKey_mgmt() {
return key_mgmt;
}
public void setKey_mgmt(String key_mgmt) {
this.key_mgmt = key_mgmt;
}
public String getSim_slot() {
return sim_slot;
}
public void setSim_slot(String sim_slot) {
this.sim_slot = sim_slot;
}
public String getImsi() {
return imsi;
}
public void setImsi(String imsi) {
this.imsi = imsi;
}
public String getPriority() {
return priority;
}
public void setPriority(String priority) {
this.priority = priority;
}
}
我几乎尝试了一切,但没有运气。什么可以是错的?
非常感谢您的任何建议。
修改
printStackTrace返回
java.lang.NullPointerException
10-16 12:54:56.421 11985-11985/com.testr W/System.err﹕ at com.orhanobut.logger.LoggerPrinter.log(LoggerPrinter.java:218)
10-16 12:54:56.421 11985-11985/com.testr W/System.err﹕ at com.orhanobut.logger.LoggerPrinter.d(LoggerPrinter.java:109)
10-16 12:54:56.421 11985-11985/com.testr W/System.err﹕ at com.orhanobut.logger.Logger.d(Logger.java:49)
10-16 12:54:56.421 11985-11985/com.testr W/System.err﹕ at com.testr.controller.FoundController.getFoundWlanNetworks(FoundController.java:38)
10-16 12:54:56.421 11985-11985/com.testr W/System.err﹕ at com.testr.view.fragment.Fragment_found.processWhatIsNeed(Fragment_found.java:51)
10-16 12:54:56.422 11985-11985/com.testr W/System.err﹕ at com.testr.view.fragment.Fragment_found.onActivityCreated(Fragment_found.java:76)
10-16 12:54:56.422 11985-11985/com.testr W/System.err﹕ at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1970)
10-16 12:54:56.422 11985-11985/com.testr W/System.err﹕ at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1051)
10-16 12:54:56.422 11985-11985/com.testr W/System.err﹕ at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1207)
10-16 12:54:56.422 11985-11985/com.testr W/System.err﹕ at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
10-16 12:54:56.422 11985-11985/com.testr W/System.err﹕ at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)
10-16 12:54:56.422 11985-11985/com.testr W/System.err﹕ at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:493)
10-16 12:54:56.422 11985-11985/com.testr W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:808)
10-16 12:54:56.423 11985-11985/com.testr W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:103)
10-16 12:54:56.423 11985-11985/com.testr W/System.err﹕ at android.os.Looper.loop(Looper.java:193)
10-16 12:54:56.423 11985-11985/com.testr W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5395)
10-16 12:54:56.423 11985-11985/com.testr W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
10-16 12:54:56.423 11985-11985/com.testr W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
10-16 12:54:56.423 11985-11985/com.testr W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:837)
10-16 12:54:56.424 11985-11985/com.testr W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653)
10-16 12:54:56.424 11985-11985/com.testr W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
检查你的json字符串。 &#34; priority&#34;附近有一个额外的引号和反斜杠。键。
{"key_mgmt":"NONE","priority\"":"97","ssid":"test"}
还有服务响应密钥&#34; ssid&#34;与POJO类属性&#34; sssid&#34;不匹配。更改POJO类属性名称。