-1

I am working on a user login page from android to C# .net service it accepts only XML and have to use HTTP POST method I am able to hit the server and get response using JSON but the USERNAME and LOGIN credentials alone coludnt be passed properly i get the response as

Input was not in Correct Format

could some one help me

i got the response from server using JSON code aas below:

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(
                "http://mydomain.com/api.svc/usr/?fmt=json");
        httppost.addHeader("POINT_ID", "WEB02");
        httppost.addHeader("AUTH_CODE", "JAbmQX5pbBpMTF0pMTbCg==");
        List<NameValuePair> paarams = new ArrayList<NameValuePair>();
        paarams.add(new BasicNameValuePair("Mode", "login"));
        paarams.add(new BasicNameValuePair("LoginId", "api@api.in"));
        paarams.add(new BasicNameValuePair("Password", "123456"));
        try {
            httppost.setEntity(new UrlEncodedFormEntity(paarams, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            // writing error to Log
            e.printStackTrace();
        }
        /*
         * Execute the HTTP Request
         */
        try {
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity respEntity = response.getEntity();
            statCode = response.getStatusLine().getStatusCode();
            if (respEntity != null) {
                // EntityUtils to get the response content
                 content = EntityUtils.toString(respEntity);
            }
        } catch (ClientProtocolException e) {
            // writing exception to log
            e.printStackTrace();
        } catch (IOException e) {
            // writing exception to log
            e.printStackTrace();
        }

The xml looks like as below

   <User>
     <Mode>login</Mode>
     <LoginId>api@api.in</LoginId>
     <Password>123456</Password>
     <Name>Guest</Name>
     <SessionId>5</SessionId>
     <Id>7</Id>
     <Guest>Y</Guest>
     <Country>USD</Country>
   </User>

i was able to get till User but the login and password failed to hit properly

I was then said that api accepts only xml so i tried with below xml hardcoding the username , password in the xml itself

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(
                "http://mydomain.com/api.svc/usr/?fmt=json");
        httppost.addHeader("POINT_ID", "WEB02");
        httppost.addHeader("AUTH_CODE", "JAbmQX5pbBpMTF0pMTbCg==");
        try {
            StringEntity se = new StringEntity( "<User><Mode>login</Mode><LoginId>api@api.in</LoginId><Password>123456</Password><Name>My API</Name><SessionId>5</SessionId><Id>7</Id><Guest>Y</Guest><Country>USD</Country></User>", HTTP.UTF_8);
            se.setContentType("text/xml");
            httppost.setEntity(se);
            HttpResponse httpresponse = httpclient.execute(httppost);
            HttpEntity resEntity = httpresponse.getEntity();
            statCode = httpresponse.getStatusLine().getStatusCode();
            content=EntityUtils.toString(resEntity);        
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

But for the above xml i get only the Request Error html page with HTML tags and CSS

I tried browsing so links like below

  1. http://developer.appcelerator.com/question/148810/send-xml-data-to-webservice
  2. how to post xml data to server in android
  3. Android, send and receive XML via HTTP POST method
  4. http://sunil-android.blogspot.in/2013/04/jquery-ajax-request-and-response.html
  5. Sending HTTP POST Request In Java
  6. Ajax post data in android java

but most of those links are passing the username password along with the api link but the api im trying uses post,beforesend and data so some one help me to authenticate user login for this api

THANKS IN ADVANCE

Community
  • 1
  • 1
JalilIrfan
  • 158
  • 1
  • 14
  • In which format does the server expect the authentication? You tagged the question with "basic-authentication", this would mean you should send it in the request headers and not in the XML body. – Henry Jul 22 '14 at 08:14
  • Thank you Henry server expects the authentication in XML format Can you tel me as what tag do i have to put – JalilIrfan Jul 22 '14 at 09:12

1 Answers1

0

Some how I myself got the answer for my problem . Hope it helps some one Only one line of code missed to get the work done Add the below line

    httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8");

just before

    httppost.setEntity(se);

in the second set of coding that i have given in my question.

Thats it now i get the exact response without any problem

JalilIrfan
  • 158
  • 1
  • 14