title: Asynctask + JSON (how to get some values)
body: <p><br>
  I was using JSONparser to get some values in a remote mySQL database, and this was working fine until I tested it in Android 3.0+ (Honeycomb), that has <em>the Guardian</em> which won't allow a process perform a networking operation on its main thread.</p>
  
  <p>So I discovered I need an <strong>Asynctask</strong>: <a href="https://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception">How to fix android.os.NetworkOnMainThreadException?</a></p>
  
  <p><br>
  And tried this topics:</p>
  
  <p><a href="https://stackoverflow.com/questions/10575072/get-returned-json-from-asynctask">Get returned JSON from AsyncTask</a></p>
  
  <p><a href="https://stackoverflow.com/questions/15006548/return-json-array-from-asynctask?rq=1">Return JSON Array from AsyncTask</a></p>
  
  <p><a href="https://stackoverflow.com/questions/11001215/how-to-return-data-from-asynctask?rq=1">How to return data from asynctask</a></p>
  
  <p><br>
  Well, now I know that asynctask can't return values, but I need to get this json values because I have <strong>many calls</strong> in <strong>different activities</strong> waiting for that to process and/or show the information in screen (That's why I can't do it in OnPostExecute, I guess).</p>
  
  <p><br>
  Here are the classes, with some adaptations from my old JsonParser.</p>
  
  <p><strong>JSONParser.java</strong></p>
  
  <pre><code>public class JSONParser extends AsyncTask&lt;List&lt;NameValuePair&gt;, Void, JSONObject&gt; {
  
      static InputStream is = null;
      static JSONObject jObj = null;
      static JSONObject result = null;
      static String json = "";
  
      private static String jsonURL = "http://192.168.1.119/Server/db/json/";
  
      private ProgressDialog progressDialog;
  
      // constructor
      public JSONParser() {
  
      }
  
      @Override
      protected void onPreExecute() {
          //progressDialog = new ProgressDialog();
          progressDialog.setMessage("Aguarde...");
          progressDialog.show();
      }
  
      protected JSONObject doInBackground(List&lt;NameValuePair&gt;... params) {
  
          // Making HTTP request
          try {
  
              // defaultHttpClient
              DefaultHttpClient httpClient = new DefaultHttpClient();
              HttpPost httpPost = new HttpPost(jsonURL);
              httpPost.setEntity(new UrlEncodedFormEntity(params[0]));
  
              HttpResponse httpResponse = httpClient.execute(httpPost);
              HttpEntity httpEntity = httpResponse.getEntity();
              is = httpEntity.getContent();
  
          } catch (UnsupportedEncodingException e) {
              e.printStackTrace();
          } catch (ClientProtocolException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          } catch (Exception e) {
              e.printStackTrace();
          }
  
          try {
              BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
              StringBuilder sb = new StringBuilder();
              String line = null;
              while ((line = reader.readLine()) != null) {
                  sb.append(line + "n");
              }
              is.close();
              json = sb.toString();
              Log.e("JSON", json);
          } catch (Exception e) {
              Log.e("Buffer Error", "Error converting result " + e.toString());
          }
  
          // try parse the string to a JSON object
          try {
              jObj = new JSONObject(json);            
          } catch (JSONException e) {
              Log.e("JSON Parser", "Error parsing data " + e.toString());
          }
  
          // return JSON String
          return jObj;
  
      }
  
  
      protected void onPostExecute(JSONObject jObj) {
  
          result = jObj;
          progressDialog.dismiss();
  
      }
  
      public JSONObject getJson(List&lt;NameValuePair&gt; params){
  
          this.execute(params);
  
          return result;
  
      }
  
  }
  </code></pre>
  
  <p><br><strong>UserFunctions.java</strong> (some calls examples)</p>
  
  <pre><code>(...)
  
  public UserFunctions(){
      JSONParser jsonParser = new JSONParser();
  }
  
  public JSONObject loginUser(String email, String password){
      List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
      params.add(new BasicNameValuePair("tag", login_tag));
      params.add(new BasicNameValuePair("email", email));
      params.add(new BasicNameValuePair("password", password));
  
      JSONObject json = jsonParser.getJson(params);
      //JSONObject json = jsonParser.execute(params); //doesn't return
      return json;
  }
  public JSONObject listProjects(String email){
      List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
      params.add(new BasicNameValuePair("tag", list_projects));
      params.add(new BasicNameValuePair("email", email));
  
      JSONObject json = jsonParser.getJson(params);
      return json;
  }
  
  public JSONObject setDisp(String dispID, String disp, String comment){
      List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
      params.add(new BasicNameValuePair("tag", set_disp));
      params.add(new BasicNameValuePair("dispID", dispID));
      params.add(new BasicNameValuePair("disp", disp));
      params.add(new BasicNameValuePair("comment", comment));
  
      JSONObject json = jsonParser.getJson(params);
      return json;
  }
  
  (...)
  </code></pre>
  
  <p><br><br>
  I tried to get passing the values to a var (JSONObject result), but no effect. (NullPointerException, I guess it try to access the var before the async finishes, because, well, it's "async")</p>
  
  <p>What I need to do to get the values? Any ideas?</p>
  
  <p><br>
  Many thanks guys!</p>