Google+ public API 键在 Android 应用程序中不起作用(它在浏览器中起作用)
Google+ public API keys doesn't work in Android app (it works in browser)
我有一个浏览器和一个 Google+ public API 的 android 密钥。两者都在浏览器中完美运行 (Chrome),但在应用程序内部使用时出现此错误:
01-23 21:31:56.348 2645-2657/com.example.javier.NavigationDrawerAllVersions W/art﹕ Suspending all threads took: 9.048ms
01-23 21:31:56.351 2645-2657/com.example.javier.NavigationDrawerAllVersions I/art﹕ Background partial concurrent mark sweep GC freed 3019(153KB) AllocSpace objects, 0(0B) LOS objects, 29% free, 9MB/13MB, paused 32.687ms total 37.855ms
01-23 21:31:56.368 2645-2663/com.example.javier.NavigationDrawerAllVersions D/OpenGLRenderer﹕ Render dirty regions requested: true
01-23 21:31:56.368 2645-2645/com.example.javier.NavigationDrawerAllVersions D/﹕ HostConnection::get() New Host Connection established 0xa6c473c0, tid 2645
01-23 21:31:56.488 2645-2645/com.example.javier.NavigationDrawerAllVersions D/Atlas﹕ Validating map...
01-23 21:31:56.537 2645-2663/com.example.javier.NavigationDrawerAllVersions D/﹕ HostConnection::get() New Host Connection established 0xa6c47e00, tid 2663
01-23 21:31:56.586 2645-2663/com.example.javier.NavigationDrawerAllVersions I/OpenGLRenderer﹕ Initialized EGL, version 1.4
01-23 21:31:56.594 2645-2661/com.example.javier.NavigationDrawerAllVersions E/JsonParser.java﹕ Error parsing data org.json.JSONException: Value Not of type java.lang.String cannot be converted to JSONObject
01-23 21:31:56.594 2645-2661/com.example.javier.NavigationDrawerAllVersions E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.example.javier.NavigationDrawerAllVersions, PID: 2645
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
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
at com.example.javier.NavigationDrawerAllVersions.MainActivity$AsyncTaskParseJson.doInBackground(MainActivity.java:134)
at com.example.javier.NavigationDrawerAllVersions.MainActivity$AsyncTaskParseJson.doInBackground(MainActivity.java:114)
at android.os.AsyncTask.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
01-23 21:31:56.612 2645-2663/com.example.javier.NavigationDrawerAllVersions D/OpenGLRenderer﹕ Enabling debug mode 0
01-23 21:31:56.620 2645-2663/com.example.javier.NavigationDrawerAllVersions W/EGL_emulation﹕ eglSurfaceAttrib not implemented
01-23 21:31:56.621 2645-2663/com.example.javier.NavigationDrawerAllVersions W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6c9da80, error=EGL_SUCCESS
01-23 21:37:19.786 2702-2702/com.example.javier.NavigationDrawerAllVersions E/libprocessgroup﹕ failed to make and chown /acct/uid_10058: Read-only file system
01-23 21:37:19.786 2702-2702/com.example.javier.NavigationDrawerAllVersions W/Zygote﹕ createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?
01-23 21:37:19.786 2702-2702/com.example.javier.NavigationDrawerAllVersions I/art﹕ Not late-enabling -Xcheck:jni (already on)
01-23 21:37:19.974 2702-2714/com.example.javier.NavigationDrawerAllVersions I/art﹕ Background sticky concurrent mark sweep GC freed 124(5KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 2MB/2MB, paused 11.319ms total 24.560ms
01-23 21:37:20.055 2702-2714/com.example.javier.NavigationDrawerAllVersions I/art﹕ Background partial concurrent mark sweep GC freed 188(29KB) AllocSpace objects, 0(0B) LOS objects, 30% free, 9MB/13MB, paused 11.827ms total 27.957ms
01-23 21:37:20.106 2702-2719/com.example.javier.NavigationDrawerAllVersions D/OpenGLRenderer﹕ Render dirty regions requested: true
01-23 21:37:20.108 2702-2702/com.example.javier.NavigationDrawerAllVersions D/﹕ HostConnection::get() New Host Connection established 0xa68493c0, tid 2702
01-23 21:37:20.144 2702-2702/com.example.javier.NavigationDrawerAllVersions D/Atlas﹕ Validating map...
01-23 21:37:20.175 2702-2719/com.example.javier.NavigationDrawerAllVersions D/﹕ HostConnection::get() New Host Connection established 0xa6849730, tid 2719
01-23 21:37:20.195 2702-2719/com.example.javier.NavigationDrawerAllVersions I/OpenGLRenderer﹕ Initialized EGL, version 1.4
01-23 21:37:20.216 2702-2719/com.example.javier.NavigationDrawerAllVersions D/OpenGLRenderer﹕ Enabling debug mode 0
01-23 21:37:20.224 2702-2719/com.example.javier.NavigationDrawerAllVersions W/EGL_emulation﹕ eglSurfaceAttrib not implemented
01-23 21:37:20.224 2702-2719/com.example.javier.NavigationDrawerAllVersions W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa68a43a0, error=EGL_SUCCESS
01-23 21:37:20.408 2702-2718/com.example.javier.NavigationDrawerAllVersions E/JsonParser.java﹕ Error parsing data org.json.JSONException: Value Not of type java.lang.String cannot be converted to JSONObject
01-23 21:37:20.408 2702-2718/com.example.javier.NavigationDrawerAllVersions E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.example.javier.NavigationDrawerAllVersions, PID: 2702
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'org.json.JSONArray org.json.JSONObject.getJSONArray(java.lang.String)' on a null object reference
at com.example.javier.NavigationDrawerAllVersions.MainActivity$AsyncTaskParseJson.doInBackground(MainActivity.java:135)
at com.example.javier.NavigationDrawerAllVersions.MainActivity$AsyncTaskParseJson.doInBackground(MainActivity.java:114)
at android.os.AsyncTask.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
01-23 21:47:55.916 2760-2760/com.example.javier.NavigationDrawerAllVersions E/libprocessgroup﹕ failed to make and chown /acct/uid_10058: Read-only file system
01-23 21:47:55.916 2760-2760/com.example.javier.NavigationDrawerAllVersions W/Zygote﹕ createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?
01-23 21:47:55.917 2760-2760/com.example.javier.NavigationDrawerAllVersions I/art﹕ Not late-enabling -Xcheck:jni (already on)
01-23 21:47:56.129 2760-2778/com.example.javier.NavigationDrawerAllVersions D/OpenGLRenderer﹕ Render dirty regions requested: true
01-23 21:47:56.131 2760-2760/com.example.javier.NavigationDrawerAllVersions D/﹕ HostConnection::get() New Host Connection established 0xa6c463d0, tid 2760
01-23 21:47:56.176 2760-2760/com.example.javier.NavigationDrawerAllVersions D/Atlas﹕ Validating map...
01-23 21:47:56.230 2760-2778/com.example.javier.NavigationDrawerAllVersions D/﹕ HostConnection::get() New Host Connection established 0xa6c46ce0, tid 2778
01-23 21:47:56.310 2760-2778/com.example.javier.NavigationDrawerAllVersions I/OpenGLRenderer﹕ Initialized EGL, version 1.4
01-23 21:47:56.352 2760-2778/com.example.javier.NavigationDrawerAllVersions D/OpenGLRenderer﹕ Enabling debug mode 0
01-23 21:47:56.369 2760-2778/com.example.javier.NavigationDrawerAllVersions W/EGL_emulation﹕ eglSurfaceAttrib not implemented
01-23 21:47:56.369 2760-2778/com.example.javier.NavigationDrawerAllVersions W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6c9efa0, error=EGL_SUCCESS
01-23 21:47:56.442 2760-2776/com.example.javier.NavigationDrawerAllVersions E/JsonParser.java﹕ Error parsing data org.json.JSONException: Value Not of type java.lang.String cannot be converted to JSONObject
01-23 21:47:56.442 2760-2776/com.example.javier.NavigationDrawerAllVersions E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.example.javier.NavigationDrawerAllVersions, PID: 2760
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
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
at com.example.javier.NavigationDrawerAllVersions.MainActivity$AsyncTaskParseJson.doInBackground(MainActivity.java:134)
at com.example.javier.NavigationDrawerAllVersions.MainActivity$AsyncTaskParseJson.doInBackground(MainActivity.java:114)
at android.os.AsyncTask.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
该代码与其他 json 对象完美配合...
这是我在浏览器上使用机器人密钥获得的 json 对象:
{
"kind": "plus#person",
"etag": "\"RqKWnRU4WW46-6W3rWhLR9iFZQM/bL-xcqP0OC4atHzmOpSf_t3HQrw\"",
"nickname": "Dahnark",
"occupation": "Student",
"skills": "Video Editing",
"birthday": "1989-04-19",
"gender": "male",
"urls": [
{
"value": "http://www.facebook.com/javiersegoviacordoba",
"type": "otherProfile",
"label": "Javier Segovia Córdoba"
},
{
"value": "http://twitter.com/dahnark",
"type": "otherProfile",
"label": "Javier Segovia Córdoba"
},
{
"value": "http://youtube.com/thedahnark",
"type": "otherProfile",
"label": "Dahnark"
},
{
"value": "http://es.linkedin.com/pub/javier-segovia-cordoba/59/27/340/",
"type": "otherProfile",
"label": "Javier Segovia Córdoba"
},
{
"value": "http://desarrollador-android.com",
"type": "contributor",
"label": "Desarrollador Android"
},
{
"value": "http://desarrollador-android.com",
"type": "contributor",
"label": "Desarrollador Android"
}
],
"objectType": "person",
"id": "116491025947756017022",
"displayName": "Javier Segovia Córdoba",
"name": {
"familyName": "Segovia Córdoba",
"givenName": "Javier"
},
"relationshipStatus": "in_a_relationship",
"url": "https://plus.google.com/+JavierSegoviaCordoba",
"image": {
"url": "https://lh4.googleusercontent.com/-LEwBpvgLyOM/AAAAAAAAAAI/AAAAAAAAHJM/CbQbbI7w1Bc/photo.jpg?sz=50",
"isDefault": false
},
"organizations": [
{
"name": "University of Cádiz, España",
"title": "Aerospace Engineering",
"type": "school",
"startDate": "2012",
"primary": true
},
{
"name": "University of Granada, España",
"title": "Physical education",
"type": "school",
"startDate": "2008",
"endDate": "2011",
"primary": false
},
{
"name": "IES Padre Poveda of Guadix, España",
"title": "Technology",
"type": "school",
"startDate": "1999",
"endDate": "2007",
"primary": false
},
{
"name": "Medina Olmos of Guadix, España",
"title": "Elementary School",
"type": "school",
"primary": false
}
],
"placesLived": [
{
"value": "Cádiz",
"primary": true
},
{
"value": "Guadix"
}
],
"isPlusUser": true,
"circledByCount": 89,
"verified": false,
"cover": {
"layout": "banner",
"coverPhoto": {
"url": "https://lh6.googleusercontent.com/-fHOV1IEH9D8/Uo6H1_3Yl3I/AAAAAAAASa8/kfbkrAcRjxY/s630-fcrop64=1,284258f39908ad6a/20130810_202949.jpg",
"height": 705,
"width": 940
},
"coverInfo": {
"topImageOffset": -197,
"leftImageOffset": 0
}
}
}
这是获取字符串的代码:
// get json string from url
JSONObject json = jParser.getJSONFromUrl(url);
// Storing each json item in variable
String firstname = json.getString("kind");
String lastname = json.getString("etag");
我该如何解决这个问题?错误在最后两个字符串中...
通常 json 对象前面有一个数组,但这不是,我写错了 getStrings 方法?
提前谢谢你,抱歉我的英语不好
我发现您教程中的解析器与 Google API(至少您提供的那个)不太兼容。但是,来自 this answer 的简单解析器确实有效。
将您的 JsonParser.java 编辑为:
public class JsonParser {
public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
return new JSONObject(jsonText);
} finally {
is.close();
}
}
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
}
然后像这样从 AsyncTask 中调用它:
@Override
protected String doInBackground(String... arg0) {
JSONObject json = null;
try {
json = JsonParser.readJsonFromUrl(yourJsonStringUrl);
// Storing each json item in variable
String kind = json.getString("kind");
String etag = json.getString("etag");
Log.e("TAG", "Kind: " + kind);
Log.e("TAG", "etag: " + etag);
} catch (IOException | JSONException e) {
e.printStackTrace();
}
return null;
}
我有一个浏览器和一个 Google+ public API 的 android 密钥。两者都在浏览器中完美运行 (Chrome),但在应用程序内部使用时出现此错误:
01-23 21:31:56.348 2645-2657/com.example.javier.NavigationDrawerAllVersions W/art﹕ Suspending all threads took: 9.048ms
01-23 21:31:56.351 2645-2657/com.example.javier.NavigationDrawerAllVersions I/art﹕ Background partial concurrent mark sweep GC freed 3019(153KB) AllocSpace objects, 0(0B) LOS objects, 29% free, 9MB/13MB, paused 32.687ms total 37.855ms
01-23 21:31:56.368 2645-2663/com.example.javier.NavigationDrawerAllVersions D/OpenGLRenderer﹕ Render dirty regions requested: true
01-23 21:31:56.368 2645-2645/com.example.javier.NavigationDrawerAllVersions D/﹕ HostConnection::get() New Host Connection established 0xa6c473c0, tid 2645
01-23 21:31:56.488 2645-2645/com.example.javier.NavigationDrawerAllVersions D/Atlas﹕ Validating map...
01-23 21:31:56.537 2645-2663/com.example.javier.NavigationDrawerAllVersions D/﹕ HostConnection::get() New Host Connection established 0xa6c47e00, tid 2663
01-23 21:31:56.586 2645-2663/com.example.javier.NavigationDrawerAllVersions I/OpenGLRenderer﹕ Initialized EGL, version 1.4
01-23 21:31:56.594 2645-2661/com.example.javier.NavigationDrawerAllVersions E/JsonParser.java﹕ Error parsing data org.json.JSONException: Value Not of type java.lang.String cannot be converted to JSONObject
01-23 21:31:56.594 2645-2661/com.example.javier.NavigationDrawerAllVersions E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.example.javier.NavigationDrawerAllVersions, PID: 2645
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
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
at com.example.javier.NavigationDrawerAllVersions.MainActivity$AsyncTaskParseJson.doInBackground(MainActivity.java:134)
at com.example.javier.NavigationDrawerAllVersions.MainActivity$AsyncTaskParseJson.doInBackground(MainActivity.java:114)
at android.os.AsyncTask.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
01-23 21:31:56.612 2645-2663/com.example.javier.NavigationDrawerAllVersions D/OpenGLRenderer﹕ Enabling debug mode 0
01-23 21:31:56.620 2645-2663/com.example.javier.NavigationDrawerAllVersions W/EGL_emulation﹕ eglSurfaceAttrib not implemented
01-23 21:31:56.621 2645-2663/com.example.javier.NavigationDrawerAllVersions W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6c9da80, error=EGL_SUCCESS
01-23 21:37:19.786 2702-2702/com.example.javier.NavigationDrawerAllVersions E/libprocessgroup﹕ failed to make and chown /acct/uid_10058: Read-only file system
01-23 21:37:19.786 2702-2702/com.example.javier.NavigationDrawerAllVersions W/Zygote﹕ createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?
01-23 21:37:19.786 2702-2702/com.example.javier.NavigationDrawerAllVersions I/art﹕ Not late-enabling -Xcheck:jni (already on)
01-23 21:37:19.974 2702-2714/com.example.javier.NavigationDrawerAllVersions I/art﹕ Background sticky concurrent mark sweep GC freed 124(5KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 2MB/2MB, paused 11.319ms total 24.560ms
01-23 21:37:20.055 2702-2714/com.example.javier.NavigationDrawerAllVersions I/art﹕ Background partial concurrent mark sweep GC freed 188(29KB) AllocSpace objects, 0(0B) LOS objects, 30% free, 9MB/13MB, paused 11.827ms total 27.957ms
01-23 21:37:20.106 2702-2719/com.example.javier.NavigationDrawerAllVersions D/OpenGLRenderer﹕ Render dirty regions requested: true
01-23 21:37:20.108 2702-2702/com.example.javier.NavigationDrawerAllVersions D/﹕ HostConnection::get() New Host Connection established 0xa68493c0, tid 2702
01-23 21:37:20.144 2702-2702/com.example.javier.NavigationDrawerAllVersions D/Atlas﹕ Validating map...
01-23 21:37:20.175 2702-2719/com.example.javier.NavigationDrawerAllVersions D/﹕ HostConnection::get() New Host Connection established 0xa6849730, tid 2719
01-23 21:37:20.195 2702-2719/com.example.javier.NavigationDrawerAllVersions I/OpenGLRenderer﹕ Initialized EGL, version 1.4
01-23 21:37:20.216 2702-2719/com.example.javier.NavigationDrawerAllVersions D/OpenGLRenderer﹕ Enabling debug mode 0
01-23 21:37:20.224 2702-2719/com.example.javier.NavigationDrawerAllVersions W/EGL_emulation﹕ eglSurfaceAttrib not implemented
01-23 21:37:20.224 2702-2719/com.example.javier.NavigationDrawerAllVersions W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa68a43a0, error=EGL_SUCCESS
01-23 21:37:20.408 2702-2718/com.example.javier.NavigationDrawerAllVersions E/JsonParser.java﹕ Error parsing data org.json.JSONException: Value Not of type java.lang.String cannot be converted to JSONObject
01-23 21:37:20.408 2702-2718/com.example.javier.NavigationDrawerAllVersions E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.example.javier.NavigationDrawerAllVersions, PID: 2702
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'org.json.JSONArray org.json.JSONObject.getJSONArray(java.lang.String)' on a null object reference
at com.example.javier.NavigationDrawerAllVersions.MainActivity$AsyncTaskParseJson.doInBackground(MainActivity.java:135)
at com.example.javier.NavigationDrawerAllVersions.MainActivity$AsyncTaskParseJson.doInBackground(MainActivity.java:114)
at android.os.AsyncTask.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
01-23 21:47:55.916 2760-2760/com.example.javier.NavigationDrawerAllVersions E/libprocessgroup﹕ failed to make and chown /acct/uid_10058: Read-only file system
01-23 21:47:55.916 2760-2760/com.example.javier.NavigationDrawerAllVersions W/Zygote﹕ createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?
01-23 21:47:55.917 2760-2760/com.example.javier.NavigationDrawerAllVersions I/art﹕ Not late-enabling -Xcheck:jni (already on)
01-23 21:47:56.129 2760-2778/com.example.javier.NavigationDrawerAllVersions D/OpenGLRenderer﹕ Render dirty regions requested: true
01-23 21:47:56.131 2760-2760/com.example.javier.NavigationDrawerAllVersions D/﹕ HostConnection::get() New Host Connection established 0xa6c463d0, tid 2760
01-23 21:47:56.176 2760-2760/com.example.javier.NavigationDrawerAllVersions D/Atlas﹕ Validating map...
01-23 21:47:56.230 2760-2778/com.example.javier.NavigationDrawerAllVersions D/﹕ HostConnection::get() New Host Connection established 0xa6c46ce0, tid 2778
01-23 21:47:56.310 2760-2778/com.example.javier.NavigationDrawerAllVersions I/OpenGLRenderer﹕ Initialized EGL, version 1.4
01-23 21:47:56.352 2760-2778/com.example.javier.NavigationDrawerAllVersions D/OpenGLRenderer﹕ Enabling debug mode 0
01-23 21:47:56.369 2760-2778/com.example.javier.NavigationDrawerAllVersions W/EGL_emulation﹕ eglSurfaceAttrib not implemented
01-23 21:47:56.369 2760-2778/com.example.javier.NavigationDrawerAllVersions W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6c9efa0, error=EGL_SUCCESS
01-23 21:47:56.442 2760-2776/com.example.javier.NavigationDrawerAllVersions E/JsonParser.java﹕ Error parsing data org.json.JSONException: Value Not of type java.lang.String cannot be converted to JSONObject
01-23 21:47:56.442 2760-2776/com.example.javier.NavigationDrawerAllVersions E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.example.javier.NavigationDrawerAllVersions, PID: 2760
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
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
at com.example.javier.NavigationDrawerAllVersions.MainActivity$AsyncTaskParseJson.doInBackground(MainActivity.java:134)
at com.example.javier.NavigationDrawerAllVersions.MainActivity$AsyncTaskParseJson.doInBackground(MainActivity.java:114)
at android.os.AsyncTask.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
该代码与其他 json 对象完美配合... 这是我在浏览器上使用机器人密钥获得的 json 对象:
{
"kind": "plus#person",
"etag": "\"RqKWnRU4WW46-6W3rWhLR9iFZQM/bL-xcqP0OC4atHzmOpSf_t3HQrw\"",
"nickname": "Dahnark",
"occupation": "Student",
"skills": "Video Editing",
"birthday": "1989-04-19",
"gender": "male",
"urls": [
{
"value": "http://www.facebook.com/javiersegoviacordoba",
"type": "otherProfile",
"label": "Javier Segovia Córdoba"
},
{
"value": "http://twitter.com/dahnark",
"type": "otherProfile",
"label": "Javier Segovia Córdoba"
},
{
"value": "http://youtube.com/thedahnark",
"type": "otherProfile",
"label": "Dahnark"
},
{
"value": "http://es.linkedin.com/pub/javier-segovia-cordoba/59/27/340/",
"type": "otherProfile",
"label": "Javier Segovia Córdoba"
},
{
"value": "http://desarrollador-android.com",
"type": "contributor",
"label": "Desarrollador Android"
},
{
"value": "http://desarrollador-android.com",
"type": "contributor",
"label": "Desarrollador Android"
}
],
"objectType": "person",
"id": "116491025947756017022",
"displayName": "Javier Segovia Córdoba",
"name": {
"familyName": "Segovia Córdoba",
"givenName": "Javier"
},
"relationshipStatus": "in_a_relationship",
"url": "https://plus.google.com/+JavierSegoviaCordoba",
"image": {
"url": "https://lh4.googleusercontent.com/-LEwBpvgLyOM/AAAAAAAAAAI/AAAAAAAAHJM/CbQbbI7w1Bc/photo.jpg?sz=50",
"isDefault": false
},
"organizations": [
{
"name": "University of Cádiz, España",
"title": "Aerospace Engineering",
"type": "school",
"startDate": "2012",
"primary": true
},
{
"name": "University of Granada, España",
"title": "Physical education",
"type": "school",
"startDate": "2008",
"endDate": "2011",
"primary": false
},
{
"name": "IES Padre Poveda of Guadix, España",
"title": "Technology",
"type": "school",
"startDate": "1999",
"endDate": "2007",
"primary": false
},
{
"name": "Medina Olmos of Guadix, España",
"title": "Elementary School",
"type": "school",
"primary": false
}
],
"placesLived": [
{
"value": "Cádiz",
"primary": true
},
{
"value": "Guadix"
}
],
"isPlusUser": true,
"circledByCount": 89,
"verified": false,
"cover": {
"layout": "banner",
"coverPhoto": {
"url": "https://lh6.googleusercontent.com/-fHOV1IEH9D8/Uo6H1_3Yl3I/AAAAAAAASa8/kfbkrAcRjxY/s630-fcrop64=1,284258f39908ad6a/20130810_202949.jpg",
"height": 705,
"width": 940
},
"coverInfo": {
"topImageOffset": -197,
"leftImageOffset": 0
}
}
}
这是获取字符串的代码:
// get json string from url
JSONObject json = jParser.getJSONFromUrl(url);
// Storing each json item in variable
String firstname = json.getString("kind");
String lastname = json.getString("etag");
我该如何解决这个问题?错误在最后两个字符串中...
通常 json 对象前面有一个数组,但这不是,我写错了 getStrings 方法?
提前谢谢你,抱歉我的英语不好
我发现您教程中的解析器与 Google API(至少您提供的那个)不太兼容。但是,来自 this answer 的简单解析器确实有效。
将您的 JsonParser.java 编辑为:
public class JsonParser {
public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
return new JSONObject(jsonText);
} finally {
is.close();
}
}
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
}
然后像这样从 AsyncTask 中调用它:
@Override
protected String doInBackground(String... arg0) {
JSONObject json = null;
try {
json = JsonParser.readJsonFromUrl(yourJsonStringUrl);
// Storing each json item in variable
String kind = json.getString("kind");
String etag = json.getString("etag");
Log.e("TAG", "Kind: " + kind);
Log.e("TAG", "etag: " + etag);
} catch (IOException | JSONException e) {
e.printStackTrace();
}
return null;
}