title: Android JSON Call through HTTPPOST with Headers
body: <p>I found an example on how to call JSON from HTTPPOST in Android. Now I can call A service on my localhost using the link: (http://10.0.2.2/testingjson/testing.json)</p>
  
  <p>It gives the output in the listView and that is Great!</p>
  
  <p>Now what I need or require is that I want to insert some extra information on the link like,</p>
  
  <pre><code>http://10.0.2.2/testingjson/testing.json?regid=1234&amp;pass="abcd"
  </code></pre>
  
  <p>There are two java Files, Main and JSONfunction.</p>
  
  <p>Here is all the code:</p>
  
  <p>Main.java:</p>
  
  <pre><code>    package com.android.jsontut;
  
      import java.util.ArrayList;
      import java.util.HashMap;
  
      import org.json.JSONArray;
      import org.json.JSONException;
      import org.json.JSONObject;
      import org.w3c.dom.Document;
      import org.w3c.dom.Element;
      import org.w3c.dom.NodeList;
  
  
  
      import android.app.ListActivity;
      import android.content.Intent;
      import android.os.Bundle;
      import android.util.Log;
      import android.view.View;
      import android.widget.AdapterView;
      import android.widget.AdapterView.OnItemClickListener;
      import android.widget.ListAdapter;
      import android.widget.ListView;
      import android.widget.SimpleAdapter;
      import android.widget.Toast;
  
  public class Main extends ListActivity {
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.listplaceholder);
  
          ArrayList&lt;HashMap&lt;String, String&gt;&gt; mylist = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
  
  
          JSONObject json = JSONfunctions.getJSONfromURL("http://10.0.2.2/testingjson/testing.json");
  
          try{
  
              JSONArray  earthquakes = json.getJSONArray("earthquakes");
  
              for(int i=0;i&lt;earthquakes.length();i++){                        
                  HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();    
                  JSONObject e = earthquakes.getJSONObject(i);
  
                  map.put("id",  String.valueOf(i));
                  map.put("name", "Earthquake name:" + e.getString("eqid"));
                  map.put("magnitude", "Magnitude: " +  e.getString("magnitude"));
                  mylist.add(map);            
              }       
          }catch(JSONException e)        {
               Log.e("log_tag", "Error parsing data "+e.toString());
          }
  
          ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main, 
                          new String[] { "name", "magnitude" }, 
                          new int[] { R.id.item_title, R.id.item_subtitle });
  
          setListAdapter(adapter);
  
          final ListView lv = getListView();
          lv.setTextFilterEnabled(true);  
          lv.setOnItemClickListener(new OnItemClickListener() {
              public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {              
                  @SuppressWarnings("unchecked")
                  HashMap&lt;String, String&gt; o = (HashMap&lt;String, String&gt;) lv.getItemAtPosition(position);                   
                  Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show(); 
  
              }
          });
      }
  }
  </code></pre>
  
  <p>Now the Second File:</p>
  
  <p>JSONFUNCTIONS.java</p>
  
  <pre><code>package com.android.jsontut;
  
  import java.io.BufferedReader;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.util.HashMap;
  import org.apache.http.HttpEntity;
  import org.apache.http.HttpResponse;
  import org.apache.http.client.HttpClient;
  import org.apache.http.client.entity.UrlEncodedFormEntity;
  import org.apache.http.client.methods.HttpPost;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import android.util.Log;
  
  public class JSONfunctions {
  
      public static JSONObject getJSONfromURL(String url){
          InputStream is = null;
          String result = "";
          JSONObject jArray = null;
  
          //http post
          try{
                  HttpClient httpclient = new DefaultHttpClient();
                  HttpPost httppost = new HttpPost(url);
                  HttpResponse response = httpclient.execute(httppost);
                  HttpEntity entity = response.getEntity();
                  is = entity.getContent();
  
          }catch(Exception e){
                  Log.e("log_tag", "Error in http connection "+e.toString());
          }
  
        //convert response to string
          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();
                  result=sb.toString();
          }catch(Exception e){
                  Log.e("log_tag", "Error converting result "+e.toString());
          }
  
          try{
  
              jArray = new JSONObject(result);            
          }catch(JSONException e){
                  Log.e("log_tag", "Error parsing data "+e.toString());
          }
  
          return jArray;
      }
  }
  </code></pre>
  
  <p>Is there any complete Example from where I can learn how to add Header in this example? or can any genius help me? :)</p>
  
  <p>I am willing to solve this problem so I will be happy to talk in detail to solve this issue! :)</p>
  
  <p>Please let me know if need more information..</p>
  
  <p>Thanks in Advance!</p>