如何创建REST API,以及如何消费REST API与Android应用REST、API、Android

2023-09-04 13:39:07 作者:流苏▼暮凉

我们如何创建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();
}

}