我正在用asp.net web api构建一个web服务,以便与android应用程序交谈。Android库使用asp.net web api
有什么好的和简单的图书馆在那里与android一起工作,使得连接到一个web服务快速和容易?
我正在寻找一些东西,将我排除在较低级别的网络和线程上,并且我可以将我的安全性集成到其中。 (基于令牌的身份验证)
我偶然发现了一个用于制作asyn http请求的库,但我似乎无法再找到它。这就是为什么我想我会问别人正在使用什么,看看会推荐什么。
我正在用asp.net web api构建一个web服务,以便与android应用程序交谈。Android库使用asp.net web api
有什么好的和简单的图书馆在那里与android一起工作,使得连接到一个web服务快速和容易?
我正在寻找一些东西,将我排除在较低级别的网络和线程上,并且我可以将我的安全性集成到其中。 (基于令牌的身份验证)
我偶然发现了一个用于制作asyn http请求的库,但我似乎无法再找到它。这就是为什么我想我会问别人正在使用什么,看看会推荐什么。
@Zapnologica:您可以结合本地HttpClient和Gson向ASP.NET Web API的REST Web服务发出请求,如post。如果你仍然真的想要先进的解决方案,那么你可以使用AndroidAnnotations + Spring Framework + Gson像3.2.3这个post。不过,我建议您使用HttpClient的第一种方法,因为您可以稍后轻松自定义您的代码。
更新:
例如,HttpClient的方法,从第一篇文章,你可以做一个JSONHttpClient,它解析JSON对象为您和发送到/从您的网络服务接收
public class JSONHttpClient {
public <T> T PostObject(final String url, final T object, final Class<T> objectClass) {
DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
StringEntity stringEntity = new StringEntity(new GsonBuilder().create().toJson(object));
httpPost.setEntity(stringEntity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("Accept-Encoding", "gzip");
HttpResponse httpResponse = defaultHttpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
InputStream inputStream = httpEntity.getContent();
Header contentEncoding = httpResponse.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
inputStream = new GZIPInputStream(inputStream);
}
String resultString = convertStreamToString(inputStream);
inputStream.close();
return new GsonBuilder().create().fromJson(resultString, objectClass);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (ClientProtocolException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return null;
}
public <T> T PostParams(String url, final List<NameValuePair> params, final Class<T> objectClass) {
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
return PostObject(url, null, objectClass);
}
private String convertStreamToString(InputStream inputStream) {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
try {
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
return stringBuilder.toString();
}
public <T> T Get(String url, List<NameValuePair> params, final Class<T> objectClass) {
DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
try {
httpGet.setHeader("Accept", "application/json");
httpGet.setHeader("Accept-Encoding", "gzip");
HttpResponse httpResponse = defaultHttpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
InputStream inputStream = httpEntity.getContent();
Header contentEncoding = httpResponse.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
inputStream = new GZIPInputStream(inputStream);
}
String resultString = convertStreamToString(inputStream);
inputStream.close();
return new GsonBuilder().create().fromJson(resultString, objectClass);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (ClientProtocolException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return null;
}
public boolean Delete(String url, final List<NameValuePair> params) {
DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpDelete httpDelete = new HttpDelete(url);
HttpResponse httpResponse = null;
try {
httpResponse = defaultHttpClient.execute(httpDelete);
return httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_NO_CONTENT;
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return false;
}
}
普莱斯补充一些因为该链接将来可能变得无效,因此请在答案中提供解决方案的详细信息。 – abarisone
感谢提醒,我在我的答案中添加了示例代码 –