title: JSON: getting Null String From User define Json Function Android
body: <p>I'm getting <code>null</code> string 1st time from a <code>Json</code> function
  I have 2 class files:</p>
  
  <ol>
  <li>func.java</li>
  <li>main.java</li>
  </ol>
  
  <p>In <code>func.java</code> code is</p>
  
  <pre><code>package com.example.json;
  
  import java.io.BufferedReader;
  import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.FileReader;
  import java.io.FileWriter;
  import java.io.IOException;
  import java.util.Random;
  import android.content.Context;
  import org.apache.http.Header;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import com.loopj.android.http.AsyncHttpClient;
  import com.loopj.android.http.AsyncHttpResponseHandler;
  
  import android.widget.Toast;
  
  public class func {
      public static String res = null;
  
      public static String jsonv(String url, Context c) {
          AsyncHttpClient client = new AsyncHttpClient();
  
          client.get(url, new AsyncHttpResponseHandler() {
  
              @Override
              public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
  
                  try {
                      res = new String(arg2);
                  } catch (JSONException e) {
                      // TODO Auto-generated catch block
                      res = "error 1 ";
                  }
  
                  // Toast.makeText(getApplicationContext(), res,
                  // Toast.LENGTH_LONG).show();
              }
  
              @Override
              public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
                  res = "error 2";
              }
          });
          return res;
      }
  }
  </code></pre>
  
  <p>in <strong>func.java</strong> I code to get JSON string and I want it in function only
  and then in <strong>main.java</strong> code is</p>
  
  <pre><code>public class main extends Activity {
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          String getj = func.jsonv("192.168.71.1/json?app=1", getApplicationContext());
          Toast.makeText(getApplicationContext(), "json is" + getj, 1000).show();
      }
  }
  </code></pre>
  
  <p>It give <code>null</code> because on startup of app <code>json</code> is still loading so how to perform a action when <code>json</code> is loaded successfully and I want it in function form only so I can use it any where any time.</p>
  
  <p>here is log </p>
  
  <pre><code>06-21 14:10:01.193: I/BufferQueue(19206): [unnamed-19206-0](this:0x60e29008,id:0,api:0,p:-1,c:-1) BufferQueue
  06-21 14:10:01.193: I/GLConsumer(19206): [void android::RingBuffer&lt;TYPE&gt;::resize(uint32_t, bool) [with TYPE = android::sp&lt;android::BackupBuffer&gt;; uint32_t = unsigned int]] 0x60e25e3c resize to 10 (force=true)
  06-21 14:10:01.193: I/GLConsumer(19206): [void android::RingBuffer&lt;TYPE&gt;::resize(uint32_t, bool) [with TYPE = android::sp&lt;android::BackupBuffer&gt;; uint32_t = unsigned int]] 0x60e25e3c resize to 0 (force=false)
  06-21 14:10:01.194: I/BufferQueue(19206): [unnamed-19206-0](this:0x60e29008,id:0,api:0,p:-1,c:19206) consumerConnect consumer=(19206:com.example.json) controlledByApp=true
  06-21 14:10:01.194: I/BufferQueue(19206): [unnamed-19206-0](this:0x60e29008,id:0,api:0,p:-1,c:19206) setConsumerName: unnamed-19206-0
  06-21 14:10:01.194: I/GLConsumer(19206): [unnamed-19206-0](this:0x60e29670,api:0) GLConsumer
  06-21 14:10:01.194: I/GLConsumer(19206): [unnamed-19206-0](this:0x60e29670,api:0) detachFromContext
  06-21 14:10:01.204: D/VelocityTracker(19206): Couldn't open '/dev/touch' (Permission denied)
  06-21 14:10:01.204: D/VelocityTracker(19206): tpd read x fail: Bad file number
  06-21 14:10:01.205: D/VelocityTracker(19206): tpd read y fail: Bad file number
  06-21 14:10:01.205: D/WebViewCallback(19206): getVisitedHistory
  06-21 14:10:01.219: D/dalvikvm(19206): threadid=15: interp stack at 0x6163b000
  06-21 14:10:01.222: D/MediatekClassFactory(19206): createInstance(): Begin = 5611917
  06-21 14:10:01.222: D/MediatekClassFactory(19206): create Instance with :  interface com.mediatek.common.telephony.IOnlyOwnerSimSupport
  06-21 14:10:01.222: W/MediatekClassFactory(19206): Tablet not exist!, Get obj from default class
  06-21 14:10:01.223: D/MediatekClassFactory(19206): create Instance from tablet library :  com.mediatek.tb.telephony.OnlyOwnerSimSupport
  06-21 14:10:01.223: D/MediatekClassFactory(19206): createInstance(): End = 5611918
  06-21 14:10:01.303: D/dalvikvm(19206): create interp thread : stack size=128KB
  06-21 14:10:01.303: D/dalvikvm(19206): create new thread
  06-21 14:10:01.304: D/dalvikvm(19206): new thread created
  06-21 14:10:01.304: D/dalvikvm(19206): update thread list
  06-21 14:10:01.304: D/dalvikvm(19206): threadid=16: interp stack at 0x61694000
  06-21 14:10:01.304: D/dalvikvm(19206): threadid=16: created from interp
  06-21 14:10:01.304: D/dalvikvm(19206): start new thread
  06-21 14:10:01.304: D/dalvikvm(19206): threadid=16: notify debugger
  06-21 14:10:01.304: D/dalvikvm(19206): threadid=16 (RefQueueWorker@org.apache.http.impl.conn.tsccm.ConnPoolByRoute@427f7c80): calling run()
  06-21 14:10:01.309: I/System.out(19206): httpget:http://192.168.71.1/json/?app=0b52765ac1d697a37891605df3c27e0f
  06-21 14:10:01.310: I/System.out(19206): http://192.168.71.1/json/?app=0b52765ac1d697a37891605df3c27e0f
  06-21 14:10:01.310: D/dalvikvm(19206): create interp thread : stack size=128KB
  06-21 14:10:01.310: D/dalvikvm(19206): create new thread
  06-21 14:10:01.310: D/dalvikvm(19206): new thread created
  06-21 14:10:01.310: D/dalvikvm(19206): update thread list
  06-21 14:10:01.311: D/dalvikvm(19206): threadid=17: interp stack at 0x616b9000
  06-21 14:10:01.311: D/dalvikvm(19206): threadid=17: created from interp
  06-21 14:10:01.311: D/dalvikvm(19206): start new thread
  06-21 14:10:01.311: D/dalvikvm(19206): threadid=17: notify debugger
  06-21 14:10:01.311: D/dalvikvm(19206): threadid=17 (pool-1-thread-1): calling run()
  06-21 14:10:01.317: I/System.out(19206): [socket][0] connection /192.168.71.1:80;LocalPort=56751(10000)
  06-21 14:10:01.317: I/System.out(19206): [CDS]connect[/192.168.71.1:80] tm:10
  06-21 14:10:01.317: D/Posix(19206): [Posix_connect Debug]Process com.example.json :80 
  06-21 14:10:01.318: I/System.out(19206): [socket][/192.168.71.101:56751] connected
  06-21 14:10:01.318: I/System.out(19206): [CDS]rx timeout:10000
  06-21 14:10:01.319: I/System.out(19206): [CDS]SO_SND_TIMEOUT:0
  06-21 14:10:01.320: D/ActivityThread(19206): ACT-AM_ON_RESUME_CALLED ActivityRecord{41f53428 token=android.os.BinderProxy@41f52b48 {com.example.json/com.example.json.MainActivity}}
  06-21 14:10:01.321: I/System.out(19206): &gt;doSendRequest
  06-21 14:10:01.323: I/System.out(19206): &lt;doSendRequest
  06-21 14:10:01.323: V/PhoneWindow(19206): DecorView setVisiblity: visibility = 4 ,Parent =null, this =com.android.internal.policy.impl.PhoneWindow$DecorView{41f7b548 I.E..... R.....ID 0,0-0,0}
  06-21 14:10:01.333: V/PhoneWindow(19206): DecorView setVisiblity: visibility = 0 ,Parent =ViewRoot{42821dd0 com.example.json/com.example.json.MainActivity,ident = 0}, this =com.android.internal.policy.impl.PhoneWindow$DecorView{41f7b548 V.E..... R.....ID 0,0-0,0}
  06-21 14:10:01.333: D/ActivityThread(19206): ACT-LAUNCH_ACTIVITY handled : 0 / ActivityRecord{41f53428 token=android.os.BinderProxy@41f52b48 {com.example.json/com.example.json.MainActivity}}
  06-21 14:10:01.371: E/(19206): appName=com.example.json, acAppName=/system/bin/surfaceflinger
  06-21 14:10:01.371: E/(19206): 0
  06-21 14:10:01.374: D/GraphicBuffer(19206): create handle(0x617e0348) (w:544, h:960, f:1)
  06-21 14:10:01.378: I/MaliEGL(19206): [Mali]window_type=1, is_framebuffer=0, errnum = 0
  06-21 14:10:01.378: I/MaliEGL(19206): [Mali]surface-&gt;num_buffers=4, surface-&gt;num_frames=3, win_min_undequeued=1
  06-21 14:10:01.378: I/MaliEGL(19206): [Mali]max_allowed_dequeued_buffers=3
  06-21 14:10:01.378: D/GraphicBuffer(19206): close handle(0x617e0348) (w:544 h:960 f:1)
  06-21 14:10:01.380: I/(19206): [custom_prop]custom_get_string-&gt;module[browser],key[UAProfileURL]
  06-21 14:10:01.380: I/(19206): [custom_prop]reset_parser-&gt;keep[0]
  06-21 14:10:01.380: I/(19206): [custom_prop]enter STATE_VALUE
  06-21 14:10:01.380: I/(19206): [custom_prop]reset_parser-&gt;keep[1]
  06-21 14:10:01.380: D/GraphicBuffer(19206): create handle(0x617e2de8) (w:544, h:960, f:1)
  06-21 14:10:01.382: D/OpenGLRenderer(19206): Enabling debug mode 0
  06-21 14:10:01.382: I/(19206): [custom_prop]enter STATE_VALUE
  06-21 14:10:01.382: I/(19206): [custom_prop]reset_parser-&gt;keep[1]
  06-21 14:10:01.382: I/(19206): [custom_prop]enter STATE_VALUE
  06-21 14:10:01.382: I/(19206): [custom_prop]reset_parser-&gt;keep[1]
  06-21 14:10:01.382: I/(19206): [custom_prop]enter STATE_VALUE
  06-21 14:10:01.382: I/(19206): [custom_prop]enter STATE_PROP
  06-21 14:10:01.382: I/(19206): [custom_prop]prop name:ro.build.version.release
  06-21 14:10:01.382: I/(19206): [custom_prop]enter STATE_VALUE
  06-21 14:10:01.382: I/(19206): [custom_prop]reset_parser-&gt;keep[1]
  06-21 14:10:01.382: I/(19206): [custom_prop]enter STATE_VALUE
  06-21 14:10:01.382: I/(19206): [custom_prop]enter STATE_PROP
  06-21 14:10:01.382: I/(19206): [custom_prop]prop name:ro.build.version.release
  06-21 14:10:01.382: I/(19206): [custom_prop]enter STATE_VALUE
  06-21 14:10:01.382: I/(19206): [custom_prop]reset_parser-&gt;keep[1]
  06-21 14:10:01.382: I/(19206): [custom_prop]enter STATE_VALUE
  06-21 14:10:01.382: I/(19206): [custom_prop]reset_parser-&gt;keep[1]
  06-21 14:10:01.382: I/(19206): [custom_prop]enter STATE_VALUE
  06-21 14:10:01.382: I/(19206): [custom_prop]reset_parser-&gt;keep[1]
  06-21 14:10:01.382: I/(19206): [custom_prop]enter STATE_VALUE
  06-21 14:10:01.382: I/(19206): [custom_prop]reset_parser-&gt;keep[1]
  06-21 14:10:01.382: I/(19206): [custom_prop]enter STATE_VALUE
  06-21 14:10:01.382: I/(19206): [custom_prop]reset_parser-&gt;keep[1]
  06-21 14:10:01.382: I/(19206): [custom_prop]enter STATE_VALUE
  06-21 14:10:01.382: I/(19206): [custom_prop]reset_parser-&gt;keep[1]
  06-21 14:10:01.382: I/(19206): [custom_prop]reset_parser-&gt;keep[0]
  06-21 14:10:01.383: D/GraphicBuffer(19206): create handle(0x617e1968) (w:768, h:768, f:1)
  06-21 14:10:01.385: I/[MALI][Gralloc](19206): dlopen libsec_mem.so fail
  06-21 14:10:01.386: D/OpenGLRenderer(19206): setViewport 540x960 &lt;0x60e3c360&gt;
  06-21 14:10:01.387: I/BufferQueue(19206): [unnamed-19206-0](this:0x60e29008,id:0,api:0,p:-1,c:19206) setDefaultBufferSize: w=150, h=150
  06-21 14:10:01.387: I/BufferQueue(19206): [unnamed-19206-0](this:0x60e29008,id:0,api:0,p:-1,c:19206) setDefaultBufferSize: w=150, h=150
  06-21 14:10:01.403: V/InputMethodManager(19206): onWindowFocus: android.webkit.WebView{41f76e28 VFED..C. .F....ID 195,488-345,638 #7f0a0003 app:id/webView1} softInputMode=288 first=true flags=#1810100
  06-21 14:10:01.404: V/InputMethodManager(19206): START INPUT: android.webkit.WebView{41f76e28 VFED..C. .F....ID 195,488-345,638 #7f0a0003 app:id/webView1} ic=com.android.org.chromium.content.browser.input.AdapterInputConnection@42835280 tba=android.view.inputmethod.EditorInfo@42835190 controlFlags=#105
  06-21 14:10:01.415: W/AwContents(19206): nativeOnDraw failed; clearing to background color.
  06-21 14:10:01.416: D/OpenGLRenderer(19206): prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 &lt;0x60e3c360&gt;
  06-21 14:10:01.463: D/OpenGLRenderer(19206): finish &lt;0x60e3c360&gt;
  06-21 14:10:01.468: D/WebViewCallback(19206): onProgressChanged=0
  06-21 14:10:01.471: V/AsyncHttpResponseHandler(19206): Progress 90 from 90 
  </code></pre>