How to use JSON Webservices in Android?

  • code_finder

    I am developing an app where i need to use webservices in it. As i was given with php files and json data, asked to do web services. As i am new to developing i do not have any idea how to do webservices coding in android native. After a long search i came to know that by using SOAP & REST we can use web services in android.

    Can anyone please suggest me any material & how to use JSON webservices in android native. Should we need to use SOAP & REST services to implement Json Webservices in android, otherwise or there any other possibilities to get the code with out using SOAP & REST services.

    As i am new to developing any small help will be appreciated.

  • Answers
  • Michal

    In my opinion the simplest way is to make request using HttpClient.

    And then parse result with some library for instance GSON

    If you have to send parameters in JSON as well you can use POST with HttpClient to send JSON String.

    EDIT: Example can look like this:

    public class DataObject {
        private Integer intField;
        private String strField;
        // This is a class which object will be sent through http
    public class Client {
        public DataObject getData() {
            URL url = new URL("");
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            try {
                InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                //Here you have to read data from InputStream to String
                String jsonData = readStream(in);
                //Here you parse String using GSON
                return (new Gson()).fromJson(jsonData,DataObject.class);
            finally {
  • Rajendra

    for Json

        public class HttpRequests {
        private  InputStream is = null;
        public HttpRequests() { }
        public  String[] makeHttpRequest(String url, String method,List<NameValuePair> params) {
        String[] result = new String[2];
        try {
             if(method == "POST"){
                 DefaultHttpClient httpClient = new DefaultHttpClient();
                 HttpPost httpPost = new HttpPost(url);
                 httpPost.setEntity(new UrlEncodedFormEntity(params));
                 HttpResponse httpResponse = httpClient.execute(httpPost);
                 HttpEntity httpEntity = httpResponse.getEntity();
                 is = httpEntity.getContent();
             }else if(method == "GET"){
                 DefaultHttpClient httpClient = new DefaultHttpClient();
                 String paramString = URLEncodedUtils.format(params, "utf-8");
                 url += "?" + paramString;
                 HttpGet httpGet = new HttpGet(url);
                 HttpResponse httpResponse = httpClient.execute(httpGet);
                 HttpEntity httpEntity = httpResponse.getEntity();
                 is = httpEntity.getContent();
        }catch (UnsupportedEncodingException e) {
        }catch (ClientProtocolException e) {
        }catch (IOException e) {
            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");
                 result[1]= sb.toString();
                 return result;
            }catch (Exception e) {
                result[0]= e.toString();
                return result;
             return result;

    where List is array which contain the item you want to send like this

          List<NameValuePair> params = new ArrayList<NameValuePair>();
             params.add(new BasicNameValuePair("Name",name1));
             params.add(new BasicNameValuePair("Email",email1));
                  HttpRequests myHttpRequests=new HttpRequests();
             String[] responce= myHttpRequests.makeHttpRequest(SignUpURL,
                    "POST", params); 
  • Ganga Naidu

    You can use normal httpclient or httpurlconnection

    but its better to use asynckhttp which is best option for parsing json data

    and u can also use some apis like jakson( which is more faster

    than normal parsing

  • Related Question

    jquery - How to format a JSON date?
  • Questioner

    I'm taking my first crack at Ajax with jQuery. I'm getting my data onto my page, but I'm having some trouble with the JSON data that is returned for Date data types. Basically, I'm getting a string back that looks like this:


    From someone totally new to JSON - How do I format this to a short date format? Should this be handled somewhere in the jQuery code? I've tried the jQuery.UI.datepicker plugin using $.datepicker.formatDate() without any success.

    FYI: Here's the solution I came up with using a combination of the answers here:

    function getMismatch(id) {
        { MismatchId: id },
        function (result) {
      return false;
    function formatJSONDate(jsonDate) {
      var newDate = dateFormat(jsonDate, "mm/dd/yyyy");
      return newDate;

    This solution got my object from the callback method and displayed the dates on the page properly using the date format library.

  • Related Answers
  • Seasoned Advice (cooking)

    Eval is not necessary. This will work fine:

    var date = new Date(parseInt(jsonDate.substr(6)));

    The substr function takes out the "/Date(" part, and the parseInt function gets the integer and ignores the ")/" at the end. The resulting number is passed into the Date constructor.

    EDIT: I have intentionally left out the radix (the 2nd argument to parseInt); see my comment below. Also, I completely agree with Rory's comment: ISO-8601 dates are preferred over this old format -- so this format generally shouldn't be used for new development. See the excellent Json.NET library for a great alternative that serializes dates using the ISO-8601 format.

    For ISO-8601 formatted JSON dates, just pass the string into the Date constructor:

    var date = new Date(jsonDate); //no ugly parsing needed; full timezone support