我正在开发一个应用程序,我需要在其中使用网络服务。因为我得到了 php 文件和 json 数据,所以被要求做网络服务。由于我是开发新手,所以我不知道如何在 android native 中进行 web 服务编码。经过长时间的搜索,我了解到通过使用 SOAP 和 REST,我们可以在 Android 中使用 Web 服务。
任何人都可以给我建议任何 Material 以及如何在 android native 中使用 JSON web 服务。 我们是否需要使用 SOAP 和 REST 服务在 android 中实现 Json Web 服务,否则或者有任何其他可能性在不使用 SOAP 和 REST 服务的情况下获取代码。
由于我是开发新手,我们将不胜感激。
最佳答案
在我看来,最简单的方法是使用 HttpClient 发出请求.
然后用一些库解析结果,例如GSON
如果您还必须在 JSON 中发送参数,您可以使用 POST
和 HttpClient
来发送 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/