title: JSON retrieval with AsyncTask from Python server is returning "End of input at character 0"
body: <p>I am having an issue with my AsyncTasks. Basically, i am doing two AsyncTasks execute in sequence and i am always having errors when running the app. I am using a Flask Python server to retrieve data with an API. I have 2 sets of objects that i need to fetch from the server in JSON format. In my JSON on the server, i am naming the root element the name of the set that i need (<code>roles</code> and <code>device_types</code>) which both self contain an array of objects. When i get the data in my client JAVA (Android app), i check if the root element is the required element and place them in an Arraylist of its corresponding Objects. The issue is that the second request is returning an error. (Note: I placed my AsyncTask.execute requests in the onCreate because on loading the activity, i will add the json retrieved as adapter to a Listview). </p>
  
  <p>when i log.d my json response, i noticed it is incomplete. When printed from server, it is complete. </p>
  
  <pre><code>D/roles json: [
      {
          "roles": [
              {
                  "name": "Anti-Spam",
                  "slug": "anti-spam",
                  "color": "f44336",
                  "vm_role": false,
                  "id": 23
              },
              {
                  "name": "Backup",
                  "slug": "backup",
                  "color": "ffc107",
                  "vm_role": true,
                  "id": 13
              },
              {
                  "name": "Blade Center",
                  "slug": "blade-center",
                  "color": "2f6a31",
                  "vm_role": false,
                  "id": 26
              },
              {
                  "name": "Console Server - Mangement Server",
                  "slug": "console-server-mangement-server",
                  "color": "009688",
                  "vm_role": false,
                  "id": 1
              },
              {
                  "name": "Deflecteur",
                  "slug": "deflecteur",
                  "color": "00bcd4",
                  "vm_role": false,
                  "id": 42
              },
              {
                  "name": "Disk",
                  "slug": "disk",
                  "color": "aa1409",
                  "vm_role": false,
                  "id": 34
              },
              {
                  "name": "DS3524 Battery",
                  "slug": "ds3524-battery",
                  "color": "aa1409",
                  "vm_role": true,
                  "id": 37
              },
              {
                  "name": "DS3524 Controller",
                  "slug": "ds3524-controller",
                  "color": "ffeb3b",
                  "vm_role": true,
                  "id": 35
              },
              {
                  "name": "DS3524 PowerFAN",
                  "slug": "ds3524-powerfan",
                  "color": "aa1409",
                  "vm_role": true,
                  "id": 36
              },
              {
                  "name": "Equipements Tiers",
                  "slug": "equipements-tiers",
                  "color": "111111",
                  "vm_role": true,
                  "id": 28
              },
              {
                  "name": "Firewall Client",
                  "slug": "firewall-client",
                  "color": "f44336",
                  "vm_role": true,
                  "id": 33
              },
              {
                  "name": "Firewall OMT",
                  "slug": "firewall-omt",
                  "color": "f44336",
                  "vm_role": true,
                  "id": 6
              },
              {
                  "name": "Media Gateway",
                  "slug": "media-gateway",
                  "color": "cddc39",
                  "vm_role": true,
                  "id": 11
              },
              {
                  "name": "Mobile NSS - Data Gateway",
                  "slug": "mobile-nss-data-gateway",
                  "color": "3f51b5",
                  "vm_role": true,
                  "id": 47
              },
              {
                  "name": "Mobile NSS - DSR",
                  "slug": "mobile-nss-dsr",
                  "color": "3f51b5",
                  "vm_role": true,
                  "id": 49
              },
              {
                  "name": "Mobile NSS - Messaging",
                  "slug": "mobile-nss-messaging",
                  "color": "3f51b5",
                  "vm_role": true,
                  "id": 44
              },
              {
                  "name": "Mobile NSS - Mobility Management",
                  "slug": "mobile-nss-mobility-management",
                  "color": "3f51b5",
                  "vm_role": true,
                  "id": 45
              },
              {
                  "name": "Mobile NSS - OAM",
                  "slug": "mobile-nss-oam",
                  "color": "3f51b5",
                  "vm_role": true,
                  "id": 51
              },
              {
                  "name": "Mobile NSS - OTA",
                  "slug": "mobile-nss-ota",
                  "color": "3f51b5",
                  "vm_role": true,
                  "id": 43
              },
              {
                  "name": "Mobile NSS
  </code></pre>
  
  <p>Error in Run Android Studio for when i run </p>
  
  <pre><code>AsyncTask roles= new form.HTTPAsyncTask().execute("http://10.0.2.2:5000/get_role_id");
  </code></pre>
  
  <blockquote>
    <p>W/System.err: org.json.JSONException: End of input at character 0 of 
            at org.json.JSONTokener.syntaxError(JSONTokener.java:449)
            at org.json.JSONTokener.nextValue(JSONTokener.java:97)
            at org.json.JSONArray.(JSONArray.java:92)</p>
  </blockquote>
  
  <p>The json printed from Flask python server</p>
  
  <blockquote>
    <p>[{"roles": [{"name": "Anti-Spam", "slug": "anti-spam", "color":
    "f44336", "vm_role": false, "id": 23}, {"name": "Backup", "slug":
    "backup", "color": "ffc107", "vm_role": true, "id": 13}, {"name":
    "Blade Center", "slug": "blade-center", "color": "2f6a31", "vm_role":
    false, "id": 26}, {"name": "Console Server - Mangement Server",
    "slug": "console-server-mangement-server", "color": "009688",
    "vm_role": false, "id": 1}, {"name": "Deflecteur", "slug":
    "deflecteur",...</p>
  </blockquote>
  
  <p>Code:</p>
  
  <pre><code>private String HttpPost(String myUrl) throws IOException, JSONException {
      String result = "";
  
      URL url = new URL(myUrl);
  
      // 1. create HttpURLConnection
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setRequestMethod("POST");
      conn.setRequestProperty("Content-Type", "application/json; charset=utf-8");
  
      // 2. build JSON object
      JSONObject jsonObject = buidJsonObject();
  
      // 3. add JSON content to POST request body
      setPostRequestContent(conn, jsonObject);
  
      // 4. make POST request to the given URL
      conn.connect();
      BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"),8);
      StringBuilder sb = new StringBuilder();
      String output;
      while ((output = br.readLine()) != null) {
          sb.append(output);
      }
  
  
  
      return sb.toString();
  
  }
  
  
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_form);
      all=new ArrayList&lt;DeviceType&gt;();
      listTypes=new ArrayList&lt;DeviceType&gt;();
      listRoles=new ArrayList&lt;Roles&gt;();
      if(checkNetworkConnection()){
  
         AsyncTask dtypes= new form.HTTPAsyncTask().execute("http://10.0.2.2:5000/get_dtypes");
         AsyncTask roles= new form.HTTPAsyncTask().execute("http://10.0.2.2:5000/get_role_id");
  
         }
      }
      else{
          Toast.makeText(this, "Not Connected!", Toast.LENGTH_SHORT).show();}
  
  
  }
  
  
  
  
    private class HTTPAsyncTask extends AsyncTask&lt;String, Void, String&gt; {
      @Override
      protected String doInBackground(String... urls) {
          // params comes from the execute() call: params[0] is the url.
          try {
              try {
                  return HttpPost(urls[0]);
              } catch (JSONException e) {
                  e.printStackTrace();
                  return "Error!";
              }
          } catch (IOException e) {
              return "Unabdgdsle to retrieve web page. URL may be invalid.";
          }
      }
  
      protected void onPostExecute(String result) {
  
          String N1=null;
          JSONObject obj=null;
          JSONArray temparr=null;
          JSONArray temparr2=null;
          JSONArray arr=null;
  
  
          try {
  
              arr = new JSONArray(result);
  
  
  
  
              if(arr.getJSONObject(0).optString("device_types")!=null){
                  //System.out.println("types: "+arr.getJSONObject(0).optString("device_types"));
                  temparr=new JSONArray(arr.getJSONObject(0).optString("device_types"));
                  System.out.println("got device types");
  
                  for(int i=0; i &lt; temparr.length(); i++) {
                      //obj = arr.getJSONObject(i);
  
                      String m=temparr.getJSONObject(i).getString("manufacturer");
                      int mid=temparr.getJSONObject(i).getInt("manufacturerid");
                      String mod=temparr.getJSONObject(i).getString("model");
                      String pn=temparr.getJSONObject(i).getString("part_number");
                      int id=temparr.getJSONObject(i).getInt("id");
                      listTypes.add(new DeviceType(m,mid,mod,pn,id));
  
                      //arrTypes[i]= arr.getJSONObject(i);
  
                  }
                  System.out.println(listTypes.toString());
                 System.out.println(listTypes.size());
  
              }
  
              if(arr.getJSONObject(0).optString("roles")!=null){
                  //System.out.println("roles: "+arr.getJSONObject(0).optString("roles"));
                  temparr2=new JSONArray(arr.getJSONObject(0).optString("roles"));
                  System.out.println("got roles");
  
                  for(int i=0; i &lt; temparr2.length(); i++) {
                      //obj = arr.getJSONObject(i);
  
                      String c=temparr2.getJSONObject(i).getString("color");
                      int id=temparr2.getJSONObject(i).getInt("id");
                      String n=temparr2.getJSONObject(i).getString("name");
                      String s=temparr2.getJSONObject(i).getString("slug");
  
                      listRoles.add(new Roles(c,id,n,s));
  
  
                      //arrTypes[i]= arr.getJSONObject(i);
  
                  }
                  System.out.println(listRoles.toString());
                  System.out.println(listRoles.size());
              }
  
  
  
          } catch (JSONException e) {
              e.printStackTrace();
          }
  
  
  private JSONObject buidJsonObject() throws JSONException {
  
      JSONObject jsonObject = new JSONObject();
      jsonObject.accumulate("token", "1");
  
      return jsonObject;
  }
  
  private void setPostRequestContent(HttpURLConnection conn,
                                     JSONObject jsonObject) throws IOException {
  
      OutputStream os = conn.getOutputStream();
      BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
      writer.write(jsonObject.toString());
      Log.i(login.class.toString(), jsonObject.toString());
      writer.flush();
      writer.close();
      os.close();
  }
  </code></pre>