从android中使用Web服务的最佳方法是什么? [英] What are the best methods to consume a web service from android?

查看:92
本文介绍了从android中使用Web服务的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我哪些是从Android中使用Web服务的最好的,轻松的和灵活的方法?我正在使用eclipse。

解决方案

由于您只关心消费Webservice,我认为您已经知道如何从Web服务器。您是否使用JSON或XML或任何其他类型的数据格式?



我自己更喜欢JSON,特别是对于Android。
您的问题仍然缺乏一些重要信息。



我个人使用apache-mime4j和httpmime-4.0.1库进行Web服务。



使用这些库我使用以下代码

  public void get(String url){
HttpResponse httpResponse = null;
InputStream _inStream = null;
HttpClient _client = null;
try {

_client = new DefaultHttpClient(_clientConnectionManager,_httpParams);
HttpGet get = new HttpGet(url);

httpResponse = _client.execute(get,_httpContext);
this.setResponseCode(httpResponse.getStatusLine()。getStatusCode());

HttpEntity entity = httpResponse.getEntity();
if(entity!= null){
_inStream = entity.getContent();
this.setStringResponse(IOUtility.convertStreamToString(_inStream));
_inStream.close();
Log.i(TAG,getStringResponse());
}
} catch(ClientProtocolException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
} finally {
try {
_inStream.close();
} catch(异常忽略){}
}
}

我通过_client.execute([method],[extra optional params])发出请求
请求的结果放在一个HttpResponse对象中。



从该对象可以获取状态代码和包含结果的实体。
从实体我拿取内容。在我的情况下,内容将是实际的JSON字符串。您将它作为InputStream检索,将流转换为字符串,并使用它执行任何所需的操作。



例如

  JSONArray result = new JSONArray(_webService.getStringResponse ()); // getStringResponse是一个自定义getter / setter,用于检索从WebService类中的输入流转换的字符串。 

根据您如何构建JSON。我的深度嵌套在数组中的对象
但处理这是基本的循环。

  JSONObject objectInResult = result .getJSONObject(count); // count将由while或for循环决定。 

在这种情况下,您可以从当前JSON对象中提取数据,如:

  objectInResult.getString(name); //假设json对象有一个键值对,其名称作为键。 


Can anyone tell me which is the best, ease and flexible method to consume web service from android? I'm using eclipse.

解决方案

Since you only care about consuming a webservice, I assume you already know how to send data from the web server. Do you use JSON or XML, or any other kind of data format?

I myself prefer JSON, especially for Android. Your question still lacks some vital information.

I personally use apache-mime4j and httpmime-4.0.1 libraries for web services.

With these libraries I use the following code

public void get(String url) {
    HttpResponse httpResponse = null;
    InputStream _inStream = null;
    HttpClient _client = null;
    try {

        _client = new DefaultHttpClient(_clientConnectionManager, _httpParams);
        HttpGet get = new HttpGet(url);

        httpResponse = _client.execute(get, _httpContext);
        this.setResponseCode(httpResponse.getStatusLine().getStatusCode());

        HttpEntity entity = httpResponse.getEntity();
        if(entity != null) {
            _inStream = entity.getContent();
            this.setStringResponse(IOUtility.convertStreamToString(_inStream));
            _inStream.close();
            Log.i(TAG, getStringResponse());
        }
    } catch(ClientProtocolException e) {
        e.printStackTrace();
    } catch(IOException e) {
        e.printStackTrace();
    } finally {
        try {
            _inStream.close();
        } catch (Exception ignore) {}
    }
}

I make a request via _client.execute([method], [extra optional params]) The result from the request is put in a HttpResponse object.

From this object you can get the status code and the entity containing the result. From the entity I take the content. The content would in my case be the actualy JSON string. You retrieve this as an InputStream, convert the stream to a string and do whatever you want with it.

For example

JSONArray result = new JSONArray(_webService.getStringResponse()); //getStringResponse is a custom getter/setter to retrieve the string converted from an inputstream in my WebService class.

Depending on how you build your JSON. mine is nested deeply with objects in the array etc. But handling this is basic looping.

JSONObject objectInResult = result.getJSONObject(count);//count would be decided by a while or for loop for example.

You can extract data from the current JSON object in this case like:

objectInResult.getString("name"); //assume the json object has a key-value pair that has name as a key.

这篇关于从android中使用Web服务的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆