关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。
想改进这个问题?将问题更新为 on-topic对于堆栈溢出。
2年前关闭。
Improve this question
我正在使用 asp.net web api 构建一个 web 服务,以便与一个 android 应用程序通信。
有哪些可以与 android 一起使用的又好又简单的库,可以快速轻松地连接到 Web 服务?
我正在寻找将我排除在较低级别的网络和线程之外并且我可以将我的安全性集成到其中的东西。 (基于 token 的身份验证)
我曾经遇到过一个用于发出异步 http 请求的库,但我似乎无法再次找到它。这就是为什么我想我会问其他人正在使用什么,看看有人会推荐什么。
最佳答案
@Zapnologica :您可以结合 native HttpClient 和 Gson 向 ASP.NET Web API 的 REST Web 服务发出请求,如 post .如果您仍然真的想要高级解决方案,那么您可以使用 AndroidAnnotations + Spring Framework + Gson,如本 post 中的第 3.2 节。 .但是,我建议您对 HttpClient 使用第一种方法,因为您以后可以轻松地自定义代码。
更新:
例如,使用 HttpClient 方法,从第一篇文章开始,您可以创建一个 JSONHttpClient 来为您解析 JSON 对象并从您的 Web 服务发送/接收
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;
}
}
关于java - 使用 asp.net web api 的 Android 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22173763/