2

is it possible that I send a request from my Android app to a web service and in return I get a data for example a XML file from the web service which I parse in android?

Thanks

kai

Adil Bhatty
  • 17,190
  • 34
  • 81
  • 118
  • 2
    Yes it is completely possible, what kind of services are you hoping to interact with? – JeremyFromEarth May 18 '10 at 04:41
  • I think you need a starting point about web services with Android. This article is a good start IMHO http://senior.ceng.metu.edu.tr/2009/praeda/2009/01/11/a-simple-restful-client-at-android/ – Ömer May 18 '10 at 11:36

1 Answers1

2

This is a method I wrote for handling just that. In my case I am using JSON for the data that I recieve, because it is much more compact than XML. I suggest using Google's GSON library for converting objects to and from json like that:

Gson gson = new Gson();
JsonReply result = gson.fromJson(jsonResult, JsonReply.class);

Where JsonReply is just a pojo for holding some data. You can see Google's java docs about how to use gson in your case. In addition I must say that this method works with all kinds of characters. I am using it mostly for sendign cyrillic data.

public String postAndGetResult(String script, List<NameValuePair> postParameters){
    String returnResult = "";
    BufferedReader in = null;
    try {
        HttpParams httpParameters = new BasicHttpParams();
        HttpProtocolParams.setContentCharset(httpParameters, "UTF-8");
        HttpProtocolParams.setHttpElementCharset(httpParameters, "UTF-8");
        HttpClient client = new DefaultHttpClient(httpParameters);
        client.getParams().setParameter("http.protocol.version",
                HttpVersion.HTTP_1_1);
        client.getParams().setParameter("http.socket.timeout",
                new Integer(2000));
        client.getParams().setParameter("http.protocol.content-charset",
                "UTF-8");
        httpParameters.setBooleanParameter("http.protocol.expect-continue",
                false);
        HttpPost request = new HttpPost(SERVER + script + "?sid="
                + String.valueOf(Math.random()));
        request.getParams().setParameter("http.socket.timeout",
                new Integer(5000));
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
                postParameters, "UTF-8");
        request.setEntity(formEntity);
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity()
                .getContent()));
        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
            sb.append(line + NL);
        }
        in.close();
        returnResult = sb.toString();
    } catch (Exception ex) {
        return "";
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            }
        }
    }
    return returnResult;
}

I hope this helps. Have fun :)