如何创建REST API,以及如何消费REST API与Android应用 [英] How to create Rest api,and how to consume the rest api with android apps

查看:116
本文介绍了如何创建REST API,以及如何消费REST API与Android应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何创建REST API为我们的网站,我要连接的服务器,我用剩下的api.How Android应用程序与我的Andr​​oid应用程序,有什么方法都落后于连接的REST API。

how we have to create rest api for our websites,i want to connect the server with my android apps using rest api.How to connect those rest api with my android apps,what are the ways are behind those.

推荐答案

在这里,你有一个很好的REST API,与基本做HTTP POST和GET:

Here you have a nice rest api, with basics for doing Http POST and GET:

public class RestClient {

private ArrayList<NameValuePair> params;
private ArrayList<NameValuePair> headers;
private String url;
private String response;
private int responseCode;

public String GetResponse()
{
    return response;
}

public int GetResponseCode()
{
    return responseCode;
}

public RestClient(String url)
{
    this.url = url;
    params = new ArrayList<NameValuePair>();
    headers = new ArrayList<NameValuePair>();
}

public void AddParam(String name, String value)
{
    params.add(new BasicNameValuePair(name, value));
}

public void AddHeader(String name, String value)
{
    headers.add(new BasicNameValuePair(name, value));
}

public void Execute(RequestType requestType) throws Exception
{
    switch(requestType)
    {
        case GET:
        {
            String combinedParams = "";
            if (!params.isEmpty())
            {
                combinedParams += "?";
                for (NameValuePair p : params)
                {
                    String paramString = p.getName() + "=" + URLEncoder.encode(p.getValue(),"UTF-8");

                    if  (combinedParams.length() > 1)
                        combinedParams += "&" + paramString;
                    else
                        combinedParams += paramString;
                }
            }
            HttpGet request = new HttpGet(url + combinedParams);

            for (NameValuePair h: headers)
                request.addHeader(h.getName(),h.getValue());

            ExecuteRequest(request, url);
            break;
        }
        case POST:
        {
            HttpPost request = new HttpPost(url);

            for (NameValuePair h : headers)
            {
                request.addHeader(h.getName(), h.getValue());
            }

            if(!params.isEmpty()){
                request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
            }

            ExecuteRequest(request, url);
            break;
        }
    }
}

public void ExecuteRequest(HttpUriRequest request, String url) 
{
    HttpClient client = new DefaultHttpClient();
    HttpResponse httpResponse;
    try
    {
        httpResponse = client.execute(request);
        responseCode = httpResponse.getStatusLine().getStatusCode();

        HttpEntity entity = httpResponse.getEntity();

        if (entity != null)
        {
            InputStream in = entity.getContent();
            response = ConvertStreamToString(in);
            in.close();
        }
    }
    catch (ClientProtocolException e)  {
        client.getConnectionManager().shutdown();
        e.printStackTrace();
        } catch (IOException e) {
        Log.e("REST_CLIENT", "Execute Request: " + e.getMessage()); 
        client.getConnectionManager().shutdown();

        e.printStackTrace();
    }       
}

private String ConvertStreamToString(InputStream in)
{
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try 
    {
        while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }
    }
    catch (IOException e) 
    {
        e.printStackTrace();
    } 
    finally 
    {
        try 
        {
            in.close();
        } 
        catch (IOException e) 
        {
             Log.e("REST_CLIENT", "ConvertStreamToString: " + e.getMessage());  
            e.printStackTrace();
        }
    }
    return sb.toString();
}

}

这篇关于如何创建REST API,以及如何消费REST API与Android应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆