有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android库使用asp。NETWebAPI

我正在用asp构建一个web服务。net web api,以便与安卓应用程序对话

有什么好的、简单的库可以与安卓一起工作,从而使连接到web服务变得快捷和简单

我正在寻找一些将我排除在较低级别的网络和线程之外的东西,并且我可以将我的安全性集成到其中。(基于令牌的身份验证)

我曾经遇到过一个库,用于发出asyn http请求,但我似乎再也找不到它了。这就是为什么我想问问其他人在用什么,看看他们会推荐什么


共 (1) 个答案

  1. # 1 楼答案

    @Zapnologica:您可以将本机HttpClient和Gson结合起来,向ASP的REST web服务发出请求。NET Web API,如下面的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;
        }
    }