我为我的移动应用程序编写了一个 API。它位于http://tymonradzik.pl/THUNDER_HUNTER/thapi.php并以 JSON 格式返回数据。我想写一个登录方法,但是当我的应用程序询问 API 时,我收到了一个错误,我不知道那个错误的原因是什么。
这是我的登录功能的一部分:
public static String logIn(String nick, String password)
{
Log.e("2", "1"); //added by me to debug
JSONParser jParser = new JSONParser();
Log.e("2", "2"); //added by me to debug
Log.wtf("Error !", "http://tymonradzik.pl/THUNDER_HUNTER/thapi.php?q=login&username=" + nick + "&password=" + password + "&imei=");
String json = jParser.getJSONFromUrl("http://tymonradzik.pl/THUNDER_HUNTER/thapi.php?q=login&username=" + nick + "&password=" + haslo + "&imei=");
Log.e("2", "3");
Log.e("aaa",json.toString());
JSONObject jObject;
try {
jObject = new JSONObject(json); //That is crash line !
String error = jObject.getString("error");
Log.e("ADSFDSFSDF", error);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Log.e("2", "3");
我的 JSONParser 类:
package com.radzik.thunter;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.json.JSONObject;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public String getJSONFromUrl(String url) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost(url);
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}
}
我的日志猫:
07-24 13:11:08.440: E/2(18212): 1
07-24 13:11:08.440: E/2(18212): 2
07-24 13:11:08.440: A/Error !(18212): http://tymonradzik.pl/THUNDER_HUNTER/thapi.php?q=login&username=aaa&password=bbb&imei=
07-24 13:11:08.475: D/AndroidRuntime(18212): Shutting down VM
07-24 13:11:08.480: W/dalvikvm(18212): threadid=1: thread exiting with uncaught exception (group=0x414e92a0)
07-24 13:11:08.485: E/AndroidRuntime(18212): FATAL EXCEPTION: main
07-24 13:11:08.485: E/AndroidRuntime(18212): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.radzik.thunter/com.radzik.thunter.ThunderHunter}: java.lang.NullPointerException
07-24 13:11:08.485: E/AndroidRuntime(18212): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
07-24 13:11:08.485: E/AndroidRuntime(18212): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
07-24 13:11:08.485: E/AndroidRuntime(18212): at android.app.ActivityThread.access$600(ActivityThread.java:140)
07-24 13:11:08.485: E/AndroidRuntime(18212): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
07-24 13:11:08.485: E/AndroidRuntime(18212): at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 13:11:08.485: E/AndroidRuntime(18212): at android.os.Looper.loop(Looper.java:137)
07-24 13:11:08.485: E/AndroidRuntime(18212): at android.app.ActivityThread.main(ActivityThread.java:4898)
07-24 13:11:08.485: E/AndroidRuntime(18212): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 13:11:08.485: E/AndroidRuntime(18212): at java.lang.reflect.Method.invoke(Method.java:511)
07-24 13:11:08.485: E/AndroidRuntime(18212): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
07-24 13:11:08.485: E/AndroidRuntime(18212): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
07-24 13:11:08.485: E/AndroidRuntime(18212): at dalvik.system.NativeStart.main(Native Method)
07-24 13:11:08.485: E/AndroidRuntime(18212): Caused by: java.lang.NullPointerException
07-24 13:11:08.485: E/AndroidRuntime(18212): at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
07-24 13:11:08.485: E/AndroidRuntime(18212): at org.json.JSONTokener.nextValue(JSONTokener.java:94)
07-24 13:11:08.485: E/AndroidRuntime(18212): at org.json.JSONObject.<init>(JSONObject.java:154)
07-24 13:11:08.485: E/AndroidRuntime(18212): at org.json.JSONObject.<init>(JSONObject.java:171)
07-24 13:11:08.485: E/AndroidRuntime(18212): at com.radzik.thunter.FunkcjeAPI.zalogujSie(FunkcjeAPI.java:20)
07-24 13:11:08.485: E/AndroidRuntime(18212): at com.radzik.thunter.ThunderHunter.onCreate(ThunderHunter.java:30)
07-24 13:11:08.485: E/AndroidRuntime(18212): at android.app.Activity.performCreate(Activity.java:5206)
07-24 13:11:08.485: E/AndroidRuntime(18212): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
07-24 13:11:08.485: E/AndroidRuntime(18212): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
07-24 13:11:08.485: E/AndroidRuntime(18212): ... 11 more
我不知道我失败的原因是什么。INTERNET
我在我的 AndroidManifest.xml 中添加了一个权限。我logIn
在我的主要活动中调用函数是这样的:
APIFunctions.logIn("aaa", "bbb");
NullPointerException 使用 James Holderness 回答:
07-26 16:42:41.665: E/AndroidRuntime(345): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.radzik.thunter/com.radzik.thunter.ThunderHunter}: java.lang.NullPointerException: println needs a message
07-26 16:42:41.665: E/AndroidRuntime(345): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
07-26 16:42:41.665: E/AndroidRuntime(345): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
07-26 16:42:41.665: E/AndroidRuntime(345): at android.app.ActivityThread.access$600(ActivityThread.java:140)
07-26 16:42:41.665: E/AndroidRuntime(345): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
07-26 16:42:41.665: E/AndroidRuntime(345): at android.os.Handler.dispatchMessage(Handler.java:99)
07-26 16:42:41.665: E/AndroidRuntime(345): at android.os.Looper.loop(Looper.java:137)
07-26 16:42:41.665: E/AndroidRuntime(345): at android.app.ActivityThread.main(ActivityThread.java:4898)
07-26 16:42:41.665: E/AndroidRuntime(345): at java.lang.reflect.Method.invokeNative(Native Method)
07-26 16:42:41.665: E/AndroidRuntime(345): at java.lang.reflect.Method.invoke(Method.java:511)
07-26 16:42:41.665: E/AndroidRuntime(345): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
07-26 16:42:41.665: E/AndroidRuntime(345): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
07-26 16:42:41.665: E/AndroidRuntime(345): at dalvik.system.NativeStart.main(Native Method)
07-26 16:42:41.665: E/AndroidRuntime(345): Caused by: java.lang.NullPointerException: println needs a message
07-26 16:42:41.665: E/AndroidRuntime(345): at android.util.Log.println_native(Native Method)
07-26 16:42:41.665: E/AndroidRuntime(345): at android.util.Log.e(Log.java:297)
07-26 16:42:41.665: E/AndroidRuntime(345): at com.radzik.thunter.FunkcjeAPI.zalogujSie(FunkcjeAPI.java:19)
07-26 16:42:41.665: E/AndroidRuntime(345): at com.radzik.thunter.ThunderHunter.onCreate(ThunderHunter.java:30)
07-26 16:42:41.665: E/AndroidRuntime(345): at android.app.Activity.performCreate(Activity.java:5206)
07-26 16:42:41.665: E/AndroidRuntime(345): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
07-26 16:42:41.665: E/AndroidRuntime(345): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
07-26 16:42:41.665: E/AndroidRuntime(345): ... 11 more