android - 如何在 Android 中使用 JSON Web 服务?

标签 android json web-services rest soap

我正在开发一个应用程序,我需要在其中使用网络服务。因为我得到了 php 文件和 json 数据,所以被要求做网络服务。由于我是开发新手,所以我不知道如何在 android native 中进行 web 服务编码。经过长时间的搜索,我了解到通过使用 SOAP 和 REST,我们可以在 Android 中使用 Web 服务。

任何人都可以给我建议任何 Material 以及如何在 android native 中使用 JSON web 服务。 我们是否需要使用 SOAP 和 REST 服务在 android 中实现 Json Web 服务,否则或者有任何其他可能性在不使用 SOAP 和 REST 服务的情况下获取代码。

由于我是开发新手,我们将不胜感激。

最佳答案

在我看来,最简单的方法是使用 HttpClient 发出请求.

然后用一些库解析结果,例如GSON

如果您还必须在 JSON 中发送参数,您可以使用 POSTHttpClient 来发送 JSON 字符串。

编辑: 示例可能如下所示:

public class DataObject {
    private Integer intField;
    private String strField;
    // This is a class which object will be sent through http
}

public class Client {
    public DataObject getData() {
        URL url = new URL("http://www.android.com/");
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        try {
            InputStream in = new BufferedInputStream(urlConnection.getInputStream());
            //Here you have to read data from InputStream to String
            String jsonData = readStream(in);
            //Here you parse String using GSON
            return (new Gson()).fromJson(jsonData,DataObject.class);
        finally {
            urlConnection.disconnect();
        }
    }
}

关于android - 如何在 Android 中使用 JSON Web 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12295103/

相关文章:

c# - 持续运行的Web服务

android - StrictMode 混淆 - 这是错误的线程吗?

android - 最后一项点击空指针异常

javascript - 数组数组转换为对象数组

json - 使用 Swift 4 JSONDecoder.DataDecodingStrategy.custom(_ :)

android -如何使用 ksoap 将双值传递给服务

Android 如何从 Geocoder 返回的地址中获取街道名称

java - Dalvik JNI 何时开始支持固定?

javascript - JSON.stringify() 和 JavaScript 对象

java - SAX 解析异常 : XML document structures must start and end within the same entity