简体   繁体   English

Android JSON Web服务

[英]Android json web services

I am working on web services that I want from my Android application connect to Oracle database, so my server is in java which connects to database... and the connection is done! 我正在研究要从我的Android应用程序连接到Oracle数据库的Web服务,因此我的服务器位于连接数据库的Java中……连接完成! What I am stuck to is that I got data from database and converted to json in the server side when I retrieve it in my Android app there I get null pointer exception. 我坚持的是,当我在Android应用程序中检索数据时,我从数据库中获取了数据并将其转换为服务器端的json,但出现了空指针异常。

Function to create my json in server side: 在服务器端创建我的json的函数:

public static String constructJSN(String tag, boolean status,ArrayList<Teacher> f) {

JSONObject obj = new JSONObject();
try {
    for(int i=0;i<f.size();i++){
    obj.put("tag", tag);
    obj.put("status", new Boolean(status));
    String json = new Gson().toJson(f);
    obj.put("data",json);
    }


} catch (JSONException e) {
    // TODO Auto-generated catch block
}
return obj.toString(); 
}

and when i print it i get : 当我打印时,我得到:

{"tag":"login","status":true,"data":"[{\"name\":\"rasha\",\"classes\":\"b\",\"freetime\":\"3-4\"},{\"name\":\"heba\",\"classes\":\"a\",\"freetime\":\"3-4\"},{\"name\":\"omnia\",\"classes\":\"c\",\"freetime\":\"2-6\"}]"}

in android client side : 在android客户端:

                   String j= obj.get("data").toString();
                  JSONObject myJsonObj = new JSONObject(j);
                  String Name = myJsonObj.getString("name");
                    String Classes = myJsonObj.getString("classes");
                    Toast.makeText(getApplicationContext(), Name+ " " + Classes, Toast.LENGTH_LONG).show();

and here I get an error while debugging: 在调试时出现错误:

Method threw java.lang.NullPointerException exception. 方法抛出java.lang.NullPointerException异常。 Cannot evaluate org.json.JSONObject.toString() 无法评估org.json.JSONObject.toString()

JSONObject myJsonObj = new JSONObject(j);
JsonArray data = myJsonObj.getJsonArray("data");
for (JSONObject jObjc: JsonArray) { 
 String Classes = jObjc.getString("classes");
}

Try the below: 请尝试以下方法:

Declare a static JsonObject and assign it to null static JSONObject jObj = null; 声明一个静态JsonObject并将其分配给null静态JSONObject jObj = null;

Then in ur web service call, assign this to the JSON Response like this jObj = new JSONObject(result); 然后在您的Web服务调用中,将其分配给JSON响应,如下所示:jObj = new JSONObject(result);

jObj.getString("data"); jObj.getString(“ data”);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM