title: Android JSON post request with Ruby Web Application connection
body: <p>I'm trying to connect Android json output with Ruby web application.
  I'm having difficulty to connect Android json post request with receiving from Ruby app. When Android jsonobject has another jsonobject inside, it is not recognised in the ruby app. Here is my code.</p>
  
  <p>Android Code</p>
  
  <pre><code>JSONObject events_array = new JSONObject();
  JSONObject parent=new JSONObject();
  
  for (int i = 0; i &lt; classtimeList.size(); i++) {
      events_array.put(classtimeList.get(i).toString(),priorityList.get(i).toString());
  }
  
  parent.put("token","token_information");
  parent.put("class_type", "something");
  parent.put("class_frequency", "5");
  parent.put("course_id", "20");
  parent.put("events_array", events_array);
  
  String urlParameters = "info="+parent.toString();
  Log.i("parameters", urlParameters);
  </code></pre>
  
  <p>This is a log info for parameters.</p>
  
  <pre><code>info={"token":"token_information","class_type":"something","class_frequency":"5","course_id":"20","events_array":{"3074":"3","3134":"1","3140":"1","3146":"3","3152":"1","3075":"3","3076":"3","3077":"3","3078":"3","3079":"3","3216":"3","3217":"3","3218":"1","3219":"3"}}
  </code></pre>
  
  <p>I pass this information to Ruby app and I am having difficulty to extract "events_array" information. Below is my Ruby Code.</p>
  
  <p>My full Android code looks like</p>
  
  <pre><code>class apply extends AsyncTask&lt;String, String, String&gt; {
  
          ProgressDialog pd;
          private Exception exception;
          protected void onPreExecute() {
  
              pd = new ProgressDialog(ClassApply2.this);
              pd.setMessage("loading");
              pd.show();
          }
  
          protected String doInBackground(String... args) {
              String token = args[0];
  
  
  
              try {
                  URL url = new URL("https://www.ringleplus.com/api/v1/apply/test");
                  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  
                  JSONObject events_array = new JSONObject();
                  JSONObject parent=new JSONObject();
  
                  for (int i = 0; i &lt; classtimeList.size(); i++) {
                      events_array.put(classtimeList.get(i).toString(),priorityList.get(i).toString());
                  }
  
                  parent.put("token","token_information");
                  parent.put("class_type", "something");
                  parent.put("class_frequency", "5");
                  parent.put("course_id", "20");
                  parent.put("events_array", events_array);
  
                  connection.setRequestMethod("POST");
                  connection.setDoOutput(true);
  
  
                  String urlParameters = "info=" + parent.toString();
  
                  DataOutputStream dStream = new DataOutputStream(connection.getOutputStream());
                  dStream.writeBytes(urlParameters);
  
  
                  //dStream.write(data);  // &lt;!-- 여기에 url parameter가 들어감
                  dStream.flush();
                  dStream.close();
  
                  Log.i("parameters", parent.toString());
  
  
  
                  InputStream in = connection.getInputStream();
  
                  BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                  StringBuffer buffer = new StringBuffer();
  
                  String line = "";
                  while ((line = reader.readLine()) != null) {
                      buffer.append(line).append("\n");
                  }
                  reader.close();
                  connection.disconnect();
                  JSONObject jsonObj = null;
  
                  jsonObj = new JSONObject(buffer.toString().trim());
  
                  String output = jsonObj.getString("item");
  
                  return output;
  
              }
              catch(Exception e) {
                  Log.e("ERROR", e.getMessage(), e);
                  return null;
              }
          }
  
          protected void onPostExecute(String response) {
              if(response == null) {
                  response = "THERE WAS AN ERROR";
              }
              //progressBar.setVisibility(View.GONE);
              Log.i("INFO", response);
  
              if (pd != null) {
                  pd.dismiss();
              }
  
              adapter.notifyDataSetChanged();
  
          }
  </code></pre>
  
  <p>Ruby Code</p>
  
  <p>post :test do</p>
  
  <pre><code>    parsed_json = ActiveSupport::JSON.decode(params[:info].to_json)
      token = parsed_json["token"]
      events_array = parsed_json["events_array"]
  
      output = Array.new      
  
      if events_array != nil 
          events_array.each do |start_time, priority|
              if priority.to_i == 1 || priority.to_i == 2
                userapply = Userapply.new
                userapply.classtime_id = start_time
                userapply.user_id = user.id
                userapply.priority = priority
                userapply.save
                output &lt;&lt; {:userapply_id =&gt; userapply.id, :classtime_id =&gt; userapply.classtime_id, :user_id =&gt; userapply.user_id, :priority =&gt; userapply.priority}
              end
          end
      end
  
  
      return {status: 'ok', item: events_array}
  
  end
  </code></pre>
  
  <p>What it supposed to be returned is the information about events_array (i.e. {"3074":"3","3134":"1","3140":"1","3146":"3","3152":"1","3075":"3","3076":"3","3077":"3","3078":"3","3079":"3","3216":"3","3217":"3","3218":"1","3219":"3"} ) </p>
  
  <p>but, the Android output log is
      I/INFO: events_array</p>
  
  <p>but the token extraction seems working. </p>
  
  <pre><code>token = parsed_json["token"]
  </code></pre>
  
  <p>If this works, then my gut feeling is parsed_json["events_array"] also should work in some sense. But I'm not stuck here.</p>
  
  <p>If this is resolved, then I want to receive parsed_json["events_array"] as a hash information and want to process the full code and get the "output" instead of events_array to check why this doesn't work.</p>
  
  <p>Please let me know if there is anything I'm missing. I really need some help.</p>
  
  <p>Looking forward to seeing anyone's reply.</p>