Skip to content Skip to sidebar Skip to footer

In Android How To Post Data To Webservice Which Is Created In Wcf?

I am new to C# webservice developed using WCF framework. And I have to post data in URL. My URL is something like as http://www.example.com/abc/DGLC.svc/login and I have to pass da

Solution 1:

create a class representing the fields of your json. and in your webservice pass this in method parameter. and run this method in your backthread (asyncTask)

publicstatic String postAPIResponse(String url, String data) {

        HttpURLConnectioncon=null;
        InputStream inputStream;
        StringBufferresponses=null;
        try {
            URLurlObject=newURL(url);
            con = (HttpURLConnection) (urlObject.openConnection());

            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            con.setRequestMethod("POST");
            con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            con.setRequestProperty("Content-Length", Integer.toString(data.getBytes().length));
            con.setRequestProperty("Content-Language", "en-US");
            if (Cookie.getCookie() != null)
                con.addRequestProperty("Cookie", Cookie.getCookie());

            con.setUseCaches(false);
            con.setDoInput(true);
            con.setDoOutput(true);

            //Send requestDataOutputStreamwr=newDataOutputStream(con.getOutputStream());
            wr.writeBytes(data);
            wr.flush();
            wr.close();

            //Get Responseif (con.getResponseCode() == 200) {


                InputStreamis= con.getInputStream();
                BufferedReaderrd=newBufferedReader(newInputStreamReader(is));
                String line;
                responses = newStringBuffer();
                while ((line = rd.readLine()) != null) {
                    responses.append(line);
                }
                rd.close();

            } else {

                inputStream = newBufferedInputStream(con.getErrorStream());
                return convertInputStreamToString(inputStream);
            }


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            assert con != null;
            con.disconnect();
        }
        return responses != null ? responses.toString() : "";
    }


staticpublic String convertInputStreamToString(InputStream inputStream)throws IOException {
    BufferedReaderbufferedReader=newBufferedReader(newInputStreamReader(inputStream));
    String line;
    Stringresult="";
    while ((line = bufferedReader.readLine()) != null) {
        result += line;
    }
        /* Close Stream */
    inputStream.close();
    return result;
}

where data is string of json object new JsonObject.accumulate()... etc to map with your service object

Post a Comment for "In Android How To Post Data To Webservice Which Is Created In Wcf?"