0

I am getting some info from an api with json and i add it on some variables.The problem is that i don't know how to use them in other classes or so.Here is my AsyncTask class

class TestJson extends AsyncTask<String,String,String> {
     public static String aJsonString;

     @Override
     protected String doInBackground(String... strings) {


         ArrayList<Geocode> geocodes = null;

         DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
         HttpPost httppost = new HttpPost("https://developers.zomato.com/api/v2.1/geocode?lat=40.712776&lon=-74.005974");

         httppost.setHeader("user-key", "mykey");


         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();
             JSONObject jObject = new JSONObject(result);


             JSONObject jsonObject= (JSONObject) jObject.get("location");


             //for nested json objects
             String title=(String)jsonObject.get("city_name");


             aJsonString = jObject.getString("link");

         } catch (Exception e) {

         } finally {
             try {
                 if (inputStream != null) inputStream.close();
             } catch (Exception squish) {
             }
         }

         try {

         } catch (Exception e) {
             e.printStackTrace();


         }
         return null;
     }
 }

For example i made aJsonString static but if i call it anywhere else it gives me null.Any ideas on that?

Alex
  • 1,816
  • 5
  • 23
  • 39

1 Answers1

0

Try to store json data into static variables(Class level/global) having public access and then share wherever you want.(you may need some if - else conditions to check for valid data and for not null data).

Viraj S
  • 390
  • 2
  • 12