我从另一个函数获取一个JSON字符串并将其转换为JSONObject,以便将这些字符串值存储到单独的变量中。然后我将JSONObject中的变量发送到另一个类。你可以在logcat中看到,我成功地将JSON字符串添加到我的函数中,我认为错误是将JSON分成单独的变量。有人可以帮帮我吗?我是android中的菜鸟
04-16 08:02:03.170: D/js string(1865): {"create_time":"2015-04-16T08:01:51Z","id":"PAY-5LH931619M647525PKUXWY3Y","intent":"sale","state":"approved"}
04-16 08:02:03.170: D/DoInBackground(1865): {"create_time":"2015-04-16T08:01:51Z","id":"PAY-5LH931619M647525PKUXWY3Y","intent":"sale","state":"approved"}
04-16 08:02:03.171: W/System.err(1865): org.json.JSONException: No value for
04-16 08:02:03.172: W/System.err(1865): at org.json.JSONObject.get(JSONObject.java:389)
04-16 08:02:03.173: W/System.err(1865): at org.json.JSONObject.getJSONObject(JSONObject.java:609)
04-16 08:02:03.173: W/System.err(1865): at learn2crack.listview.Donate$ProcessPayment.doInBackground(Donate.java:228)
04-16 08:02:03.173: W/System.err(1865): at learn2crack.listview.Donate$ProcessPayment.doInBackground(Donate.java:1)
04-16 08:02:03.173: W/System.err(1865): at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-16 08:02:03.173: W/System.err(1865): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-16 08:02:03.174: W/System.err(1865): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-16 08:02:03.174: W/System.err(1865): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-16 08:02:03.174: W/System.err(1865): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-16 08:02:03.174: W/System.err(1865): at java.lang.Thread.run(Thread.java:818)
04-16 08:02:03.299: E/AndroidRuntime(1865): FATAL EXCEPTION: AsyncTask #1
04-16 08:02:03.299: E/AndroidRuntime(1865): Process: learn2crack.listview, PID: 1865
04-16 08:02:03.299: E/AndroidRuntime(1865): java.lang.RuntimeException: An error occured while executing doInBackground()
04-16 08:02:03.299: E/AndroidRuntime(1865): at android.os.AsyncTask$3.done(AsyncTask.java:300)
04-16 08:02:03.299: E/AndroidRuntime(1865): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
04-16 08:02:03.299: E/AndroidRuntime(1865): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
04-16 08:02:03.299: E/AndroidRuntime(1865): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
04-16 08:02:03.299: E/AndroidRuntime(1865): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-16 08:02:03.299: E/AndroidRuntime(1865): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-16 08:02:03.299: E/AndroidRuntime(1865): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-16 08:02:03.299: E/AndroidRuntime(1865): at java.lang.Thread.run(Thread.java:818)
04-16 08:02:03.299: E/AndroidRuntime(1865): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.getString(java.lang.String)' on a null object reference
04-16 08:02:03.299: E/AndroidRuntime(1865): at learn2crack.listview.Donate$ProcessPayment.doInBackground(Donate.java:240)
04-16 08:02:03.299: E/AndroidRuntime(1865): at learn2crack.listview.Donate$ProcessPayment.doInBackground(Donate.java:1)
04-16 08:02:03.299: E/AndroidRuntime(1865): at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-16 08:02:03.299: E/AndroidRuntime(1865): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-16 08:02:03.299: E/AndroidRuntime(1865): ... 4 more
Java代码:
protected JSONObject doInBackground(String... args) {
UserFunctions userFunction = new UserFunctions();
String js = args[0];
Log.d("js string", js);
JSONObject obj;
JSONObject ii=null;
try {
obj = new JSONObject(js);
Log.d("DoInBackground", obj.toString());
ii = obj.getJSONObject("");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//JSONObject sendjson=null;
String create_time="";
String id="";
String intent="";
String sale="";
try {
create_time = ii.getString("create_time");
id = ii.getString("id");
intent = ii.getString("intent");
sale = ii.getString("sale");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("strings", create_time + "," + id + "," + intent + "," +sale);
JSONObject sendjson = userFunction.CreatePayment(create_time, id, intent,
sale);
Log.d("sendjson", sendjson.toString());
return sendjson;
}
答案 0 :(得分:1)
问题是由此行引起的
ii = obj.getJSONObject("");
您没有JSONObject
映射到空键字符串。您正在寻找的密钥已经可以通过JSONObject obj;
E.g
create_time = obj.getString("create_time");
您也可以使用optString
代替getString
。主要区别在于optString
不会产生异常,但会返回默认值(null
表示字符串)
答案 1 :(得分:0)
你不需要行ii = obj.getJSONObject("")
,obj = new JSONObject(js)
已经是你的全部JSONObject
。只需删除所有ii引用(和JSONObject
)并替换为obj
create_time = obj.getString("create_time");
id = obj.getString("id");
intent = obj.getString("intent");
sale = obj.getString("sale");
答案 2 :(得分:0)
你不需要ii
!当您撰写ii = obj.getJSONObject("");
时,它将返回null
,因为没有空密钥。这没有意义。
此外,您尝试访问ii.getString
,但如果有JSONException
,则ii
为null
!
试试这个:
protected JSONObject doInBackground(String... args) {
UserFunctions userFunction = new UserFunctions();
String js = args[0];
Log.d("js string", js);
JSONObject obj;
String create_time="";
String id="";
String intent="";
String sale="";
JSONObject sendjson;
try {
obj = new JSONObject(js);
Log.d("DoInBackground", obj.toString());
//HERE obj is not null !
try {
create_time = obj.getString("create_time");
id = obj.getString("id");
intent = obj.getString("intent");
sale = obj.getString("sale");
Log.d("strings", create_time + "," + id + "," + intent + "," +sale);
sendjson = userFunction.CreatePayment(create_time, id, intent, sale);
Log.d("sendjson", sendjson.toString());
} catch (JSONException e) {
e.printStackTrace();
}
} catch (JSONException e) {
e.printStackTrace();
}
//check when you use this function that sendjson is not null !
return sendjson;
}