-3

hy I have to work with a server mobile app. In this app I am hitting to the url which takes the parameters such as user name and password and it returns the server response , it tells that server is up or not , Means yes or no. But it returns in a xml file . So How to save that xml file and then convert it to string . I want to convert it into string so that I can show him that server is up Yes and no. Please any help would be appreciated

this is a code I am using

`public String login(String uname,String password)
{
     InputStream is = null;
     String result = "";
     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

     try 
     {  
           HttpClient httpclient = new DefaultHttpClient();
           HttpPost httppost = new HttpPost("Your Url");
           nameValuePairs.add(new BasicNameValuePair("emailid", uname));
           nameValuePairs.add(new BasicNameValuePair("password", password));
           httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
           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();

       Log.v("log","Result :"+result);
  } 
  catch (Exception e)
  {
   Log.v("log", "Error converting result " + e.toString());
  }


    return result;
}`

but in this it is working for jason And I am getting the xml file ,so how to convert that file that is all y problem

amir zia
  • 1
  • 2
  • Why don't you include the code you've tried so far, and where it's failing. If you have no clue at all yet, google will certainly bring up a few examples for you. – 323go Jan 05 '15 at 08:02

1 Answers1

0

You can save the file and use XmlPullParser to parse it. It's fairly simple and it works good from my experience.

Sir Codesalot
  • 7,045
  • 2
  • 50
  • 56
  • can you please demonstrate it ho to do it in the code I shared above please? – amir zia Jan 05 '15 at 08:30
  • There's a tutorial for XmlPullParser at Vogella. [link](http://www.vogella.com/tutorials/AndroidXML/article.html) Saving the file is answered here [link](http://stackoverflow.com/questions/8986376/how-to-download-xml-file-from-server-and-save-it-in-sd-card) – Sir Codesalot Jan 05 '15 at 08:57
  • I had visited this , in my case I do not want to save it in the sd card or some where else, I just want to download and convert it to a string and to show in list that is all – amir zia Jan 05 '15 at 09:08
  • As far as I know, XmlPullParser only works with files so you can save it temporarily to the cache. [link](http://stackoverflow.com/questions/3425906/creating-temporary-files-in-android) – Sir Codesalot Jan 05 '15 at 10:05