我正在编写一个Android应用程序,它通过用PHP编写的API查询数据库中的数据。 它以JSONArray的形式接收结果。
无论出于何种原因,应用程序在尝试返回JSONArray时都会崩溃:
protected JSONArray doInBackground(String... urls) {
try {
Log.e("STORES: ", readWSDet());
JSONArray WSDet = new JSONArray(readWSDet());
Log.e("POST: ", WSDet.toString());
return WSDet;
} catch (Exception e) {
this.exception = e;
e.printStackTrace();
Log.e("ERROR:" , this.exception.getMessage());
return null;
}
}
两个“Log”调用都以
的形式提供正确的输出[{"id":"0","stop":"18:00:00","price":"99.95","start":"08:00:00","description":"some description","name":"Test","day":"2014-05-04","availability":"9"}]
相同的代码在我的项目中的其他活动中正常工作:(
在调试模式下,我可以看到应用程序处理所有步骤,当涉及到返回时,它会跳转到catch子句。不幸的是,我只是没有设法在日志中获得异常原因。
有关我如何发现问题的任何暗示?
提前多多感谢!
P.S。请原谅任何有关格式化的问题或如果缺少信息,这是我的第一个问题;)
编辑1:
从创建到崩溃的活动的Logcat:
04-04 07:33:20.633: D/dalvikvm(2015): GC_FOR_ALLOC freed 378K, 9% free 4827K/5272K, paused 80ms, total 92ms
04-04 07:33:21.273: W/Bundle(2015): Key dbid expected String but value was a java.lang.Long. The default value <null> was returned.
04-04 07:33:21.493: W/Bundle(2015): Attempt to cast generated internal exception:
04-04 07:33:21.493: W/Bundle(2015): java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String
04-04 07:33:21.493: W/Bundle(2015): at android.os.Bundle.getString(Bundle.java:1121)
04-04 07:33:21.493: W/Bundle(2015): at com.application.jmgmobile.WorkshopdetailsActivity.onCreate(WorkshopdetailsActivity.java:149)
04-04 07:33:21.493: W/Bundle(2015): at android.app.Activity.performCreate(Activity.java:5231)
04-04 07:33:21.493: W/Bundle(2015): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-04 07:33:21.493: W/Bundle(2015): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-04 07:33:21.493: W/Bundle(2015): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-04 07:33:21.493: W/Bundle(2015): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-04 07:33:21.493: W/Bundle(2015): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-04 07:33:21.493: W/Bundle(2015): at android.os.Handler.dispatchMessage(Handler.java:102)
04-04 07:33:21.493: W/Bundle(2015): at android.os.Looper.loop(Looper.java:136)
04-04 07:33:21.493: W/Bundle(2015): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-04 07:33:21.493: W/Bundle(2015): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 07:33:21.493: W/Bundle(2015): at java.lang.reflect.Method.invoke(Method.java:515)
04-04 07:33:21.493: W/Bundle(2015): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-04 07:33:21.493: W/Bundle(2015): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-04 07:33:21.493: W/Bundle(2015): at dalvik.system.NativeStart.main(Native Method)
04-04 07:33:22.213: E/STORES:(2015): [{"id":"0","stop":"18:00:00","price":"99.95","start":"08:00:00","description":"some description","name":"Test","day":"2014-05-04","availability":"9"}]
04-04 07:33:32.933: E/POST:(2015): [{"id":"0","stop":"18:00:00","price":"99.95","start":"08:00:00","description":"some description","name":"Test","day":"2014-05-04","availability":"9"}]
04-04 07:33:32.943: I/System.out(2015): loaded dataset
04-04 07:33:32.983: W/ResourceType(2015): No package identifier when getting value for resource number 0x00000000
04-04 07:33:35.813: D/AndroidRuntime(2015): Shutting down VM
04-04 07:33:35.813: W/dalvikvm(2015): threadid=1: thread exiting with uncaught exception (group=0xb1aceba8)
04-04 07:33:36.043: E/AndroidRuntime(2015): FATAL EXCEPTION: main
04-04 07:33:36.043: E/AndroidRuntime(2015): Process: com.application.jmgmobile, PID: 2015
04-04 07:33:36.043: E/AndroidRuntime(2015): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.application.jmgmobile/com.application.jmgmobile.WorkshopdetailsActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x0
04-04 07:33:36.043: E/AndroidRuntime(2015): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-04 07:33:36.043: E/AndroidRuntime(2015): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-04 07:33:36.043: E/AndroidRuntime(2015): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-04 07:33:36.043: E/AndroidRuntime(2015): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-04 07:33:36.043: E/AndroidRuntime(2015): at android.os.Handler.dispatchMessage(Handler.java:102)
04-04 07:33:36.043: E/AndroidRuntime(2015): at android.os.Looper.loop(Looper.java:136)
04-04 07:33:36.043: E/AndroidRuntime(2015): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-04 07:33:36.043: E/AndroidRuntime(2015): at java.lang.reflect.Method.invokeNative(Native Method)
04-04 07:33:36.043: E/AndroidRuntime(2015): at java.lang.reflect.Method.invoke(Method.java:515)
04-04 07:33:36.043: E/AndroidRuntime(2015): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-04 07:33:36.043: E/AndroidRuntime(2015): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-04 07:33:36.043: E/AndroidRuntime(2015): at dalvik.system.NativeStart.main(Native Method)
04-04 07:33:36.043: E/AndroidRuntime(2015): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
04-04 07:33:36.043: E/AndroidRuntime(2015): at android.content.res.Resources.getValue(Resources.java:1123)
04-04 07:33:36.043: E/AndroidRuntime(2015): at android.content.res.Resources.getDrawable(Resources.java:698)
04-04 07:33:36.043: E/AndroidRuntime(2015): at com.application.jmgmobile.WorkshopdetailsActivity.onCreate(WorkshopdetailsActivity.java:178)
04-04 07:33:36.043: E/AndroidRuntime(2015): at android.app.Activity.performCreate(Activity.java:5231)
04-04 07:33:36.043: E/AndroidRuntime(2015): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-04 07:33:36.043: E/AndroidRuntime(2015): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-04 07:33:36.043: E/AndroidRuntime(2015): ... 11 more
编辑2:
java文件的整个代码:
package com.application.jmgmobile;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Base64;
import android.util.Log;
import android.view.Window;
import android.widget.EditText;
import org.apache.http.*;
import com.google.analytics.tracking.android.EasyTracker;
class ParseWorkshopdetails extends AsyncTask<String, Void, JSONArray> implements Serializable{
JSONArray ergeniss1 = null;
private Exception exception;
private String username= "";
private String security_token= "";
private String security_key= "";
private String security= "";
private String dbid= "";
public static final String PREFS_NAME = "JMGMobileSettings";
public ParseWorkshopdetails( Context c )
{
SharedPreferences settings = c.getSharedPreferences(PREFS_NAME, 0);
this.username = settings.getString("username", "none");
this.dbid=settings.getString("dbid", "");
this.security_token = settings.getString("security_token", "");
this.security_key = settings.getString("security_key", "none");
this.security = security_token + ":" + security_key;
}
public String readWSDet(){
HttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost("HERE_GOES_MY_URL");
StringBuilder builder = new StringBuilder();
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("dbid", dbid));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httppost.setHeader("Authorization", "Basic " + Base64.encodeToString(security.getBytes(), Base64.NO_WRAP));
} catch (IOException e) {
// TODO Auto-generated catch block
}
try {
HttpResponse response = client.execute(httppost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
Log.e(ParseWorkshopdetails.class.toString(), "Failed to download data");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//System.out.println(builder.toString());
return builder.toString();
}
private void creatListOfStores() {
//String readAgentString = readAgents();
try {
JSONArray jsonArray = new JSONArray(readWSDet());
Log.i(ParseWorkshopdetails.class.getName(),
"Number of entries " + jsonArray.length());
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Log.i(ParseWorkshopdetails.class.getName(), jsonObject.getString("name"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
protected JSONArray doInBackground(String... urls) {
try {
Log.e("STORES: ", readWSDet());
JSONArray WSDet = new JSONArray(readWSDet());
Log.e("POST: ", WSDet.toString());
return WSDet;
} catch (Exception e) {
this.exception = e;
e.printStackTrace();
Log.e("ERROR:" , this.exception.getMessage());
return null;
}
}
protected void onPostExecute(JSONArray objekt) {
// TODO: check this.exception
this.ergeniss1 = objekt;
}
public JSONArray getErgeniss() {
return ergeniss1;
}
}
答案 0 :(得分:0)
问题似乎是价格字段。在你的JSON中,所有内容都是字符串,但我认为在你的课程中,你有这个字段,所以当你读取这个值时,请Long.parseLong()
。