0

I'm building an Android application where I connect to the database via the web services. When I send information via the web services, I got an xml result, I then convert it to a string and display it in the edittext just like this

Handler uiThreadHandler = new Handler() {
            public void handleMessage(Message msg) {
                Object o = msg.obj;

                EditText textIn = (EditText)((Activity)context).findViewById(R.id.editTextDoSelectResult);
                textIn.setText(o.toString());

                System.out.println(o.toString());
            }
        };

        Message msg = uiThreadHandler.obtainMessage();
        msg.obj = result;
        msg.arg1 = sid;
        uiThreadHandler.sendMessage(msg);
        }

I'm trying to parse the XML in order to get specific values out of the xml result and display it in the appropriate field, I think i should follow something like this

http://developer.android.com/training/basics/network-ops/xml.html

My question is, how can I use the xml result that i get in my code as an inputstream in XmlPullParser

if there's anyway i can improve this question please let me know

Mash
  • 174
  • 1
  • 1
  • 12

1 Answers1

0

Maybe it's help you.

To get inputstream:

private static InputStream downloadUrl(String urlString) throws IOException {
    URL url = new URL(urlString);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setReadTimeout(10000 /* milliseconds */);
    conn.setConnectTimeout(15000 /* milliseconds */);
    conn.setRequestMethod("GET");
    conn.setDoInput(true);
    // Starts the query
    conn.connect();
    return conn.getInputStream();
}

convert inputstream to string:

private static String convertStreamToString(InputStream is) {
    ByteArrayOutputStream oas = new ByteArrayOutputStream();
    copyStream(is, oas);
    String t = oas.toString();
    try {
        oas.close();
        oas = null;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return t;
}

set to edittext:

...
youredittext.setText(convertStreamToString(downloadUrl(your_url)));
...
andiisfh
  • 21
  • 2