モバイル アプリケーション用の 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;
}
}
私のlogcat:
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");
James Holdernessの回答を使用したNullPointerException:
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