我正在用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
感謝提醒,我在我的答案中添加了示例代碼 –