开发者

Sending and receiving data from a web service using android [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

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


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 :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜