Skip to content

Instantly share code, notes, and snippets.

@devrath
Created November 24, 2014 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devrath/d96051592d017095245a to your computer and use it in GitHub Desktop.
Save devrath/d96051592d017095245a to your computer and use it in GitHub Desktop.
Passing data to http GET request as header
private void getRequest(String urlString) {
URL url;
InputStream response = null;
try {
url = new URL(urlString);
HttpURLConnection connection;
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("phonenumber", "1234");
connection.setRequestProperty("authtoken", "1234");
response = connection.getInputStream();
String stringRep=IOUtils.toString(response);
JSONArray responseArray = new JSONArray(stringRep);
//jsonobject = new JSONObject(stringRep);
listData=new ArrayList<HashMap<String,String>>();
for(int i=0; i<responseArray.length();i++){
mapData=new HashMap<String, String>();
JSONObject temp_obj = responseArray.getJSONObject(i);
mapData.put("id", temp_obj.getInt("id")+"");
mapData.put("name", temp_obj.getString("name"));
mapData.put("description", temp_obj.getString("description"));
mapData.put("isactive", temp_obj.getBoolean("isactive")+"");
listData.add(mapData);
}
System.out.println(listData);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment