最好的方法来使用来自Android的Web服务最好的、方法来、Web、Android

2023-09-07 08:36:37 作者:无言的期待、别让我发呆

谁能告诉我,这是最好的,轻松和灵活的方法从Android的日食使用Web服务。 你知道吗?

Can anyone tell me, which is the best,ease and flexible method to consuming web service from android eclipse. Any idea?

感谢您的precious时间!..

Thanks for your precious time!..

推荐答案

既然你只关心消费Web服务,我想你已经知道了如何从Web服务器发送数据。您使用JSON或XML中,或任何其他种类的数据格式?

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?

我自己preFER JSON,特别是为Android。 你的问题还是缺少一些重要信息。

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

我个人使用Apache-mime4j和httpmime-4.0.1库用于Web服务。

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

使用这些库我用下面的code

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) {}
    }
}

我提出通过_client.execute请求([法],[额外的可选PARAMS]) 来自请求的结果被放入的Htt presponse对象

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

从这个对象,你可以得到的状态code和包含结果的实体。 从实体我带的内容。内容将在我的情况是actualy JSON字符串。您检索这是一个InputStream,流转换为字符串,做任何你想做的事情。

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.

例如

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

根据你如何建立你的JSON。矿山与数组中的等物体深度嵌套 但是,处理,这是基本的循环。

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.

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

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.