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.
Asked
Active
Viewed 1,110 times
1
-
There are plenty of tutorials available on the internet. Kindly search. We are not tutors. – Sankar V Apr 20 '13 at 05:33
-
actually dude i searched so much things,i am not getting the proper one,if you know kindly help me.@SankarV – Jitendra Patidar Apr 20 '13 at 05:34
1 Answers
1
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();
}
}

Daniel Conde Marin
- 7,588
- 4
- 35
- 44
-
Thanks dude,but i am beginner of Rest ,could you give some links where i have to start learning for creating REST,@Daniel – Jitendra Patidar Apr 20 '13 at 05:40
-
Just google it, and don't get married with Android when doing your search, given that rest comes a lot before than Android – Daniel Conde Marin Apr 20 '13 at 05:43
-
Dude i searched lot,but i didnt get the proper solution to know about the starting level of Rest,if you know .Let me@Daniel – Jitendra Patidar Apr 20 '13 at 05:53