title: Android - error on getting JSON from url
body: <p>I wrote an API for my mobile application. It is located on <a href="http://tymonradzik.pl/THUNDER_HUNTER/thapi.php" rel="nofollow">http://tymonradzik.pl/THUNDER_HUNTER/thapi.php</a> and is returning data in JSON format. I wanted to write a login method, but when my app should ask API, I'm getting an error and I don't know what is that error reason. </p>
  
  <p>That is a part of my login function :</p>
  
  <pre><code>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&amp;username=" + nick + "&amp;password=" + password + "&amp;imei=");
          String json = jParser.getJSONFromUrl("http://tymonradzik.pl/THUNDER_HUNTER/thapi.php?q=login&amp;username=" + nick + "&amp;password=" + haslo + "&amp;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");
  </code></pre>
  
  <p>My JSONParser class :</p>
  
  <pre><code>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;
     }
  }
  </code></pre>
  
  <p>My logcat :</p>
  
  <pre><code>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&amp;username=aaa&amp;password=bbb&amp;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.&lt;init&gt;(JSONObject.java:154)
  07-24 13:11:08.485: E/AndroidRuntime(18212):    at org.json.JSONObject.&lt;init&gt;(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
  </code></pre>
  
  <p>I don't know what is the reason of my failure. I added an <code>INTERNET</code> permission in my AndroidManifest.xml. I'm calling <code>logIn</code> function in my main activity like that :</p>
  
  <pre><code>APIFunctions.logIn("aaa", "bbb"); 
  </code></pre>
  
  <p>NullPointerException using James Holderness answer :</p>
  
  <pre><code>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 
  </code></pre>