RestFull Web Services:
If we want to store, retrieve the data from the remote data
base we have to take the help of the web services.To work with the web services
we need HTTP Networking Calls to interacting the remote database.
It is easy to communicate with a web service from
an Android application. All you need is a active web service, its WSDL url, web
method name, web service name space.
Here we are seeing the RestFull Web services Example
tutorial.
Android apps need data, and many use RESTful services (at
their heart, simple HTTP networking calls) to get and send data over the web.
In this tutorial we develope how to integrate Android apps with Remote
networks, and use RESTful web requests to access data from them. The course
concentrates on the different ways of making requests with the built-in classes
in the Android SDK, but also provides an overview of popular open-source
networking libraries, David Gassner will describe how to retrieve content with simple
GET and POST requests, parse the responses, and pass user credentials to
services that support HTTP basic authentication. He'll also show you how to
send parameters with your requests and format POST requests with JSON.
OutPut Of the Tutorial:
OutPut Of the Tutorial:
Create Android Application:
- File >> New >> Android Application
- Enter Project name: RestFullWebServicesApp
- Package: com.ambilpursunil.newapp
- Keep other default selections, click Next until you reach Finish

1.Simply Copy and Past the Code which is display below in the MainActivity.java
package com.ambilpursunil.newapp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.widget.TextView;
public class MainActivity1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy
policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.main);
//HTTP Networking Calls
HttpClient
httpClient = new DefaultHttpClient();
HttpPost
httpPost = new HttpPost(
"http://www.cheesejedi.com/rest_services/get_big_cheese.php?puzzle=1");
TextView
tv = (TextView) findViewById(R.id.textView1);
try {
HttpResponse
httpResponse = httpClient.execute(httpPost);
String
jsonResult = inputStreamtoString(
httpResponse.getEntity().getContent()).toString();
//JSONArray Calls
JSONArray
jsonArray = new JSONArray(jsonResult);
for (int i = 0; i < jsonArray.length(); i++) {
tv.append("id: " +jsonArray.getJSONObject(i).getString("id").toString()+ "\n");
tv.append("level:
" +
jsonArray.getJSONObject(i).getString("level")
.toString()
+"\n");
tv.append("time_in_secs:
" +
jsonArray.getJSONObject(i).getString("time_in_secs")
.toString()
+ "\n");
tv.append("par: " +
jsonArray.getJSONObject(i).getString("par")
.toString()
+ "\n");
tv.append("quote:
" +
jsonArray.getJSONObject(i).getString("quote")
.toString()
+ " \n");
tv.append("time_stamp:
" +
jsonArray.getJSONObject(i).getString("time_stamp")
.toString()
+ "\n\n");
}
}
catch (Exception e) {
e.getLocalizedMessage();
}
}
private StringBuilder inputStreamtoString(InputStream is) {
String
line = "";
StringBuilder
sb = new StringBuilder();
InputStreamReader
usr = new InputStreamReader(is);
BufferedReader
br = new BufferedReader(usr);
try {
while ((line = br.readLine()) != null) {
sb.append(line);
}
}
catch (IOException e) {
e.printStackTrace();
}
return sb;
}
}
2.Simple Copy and Past the below code: actvitiy_main.xml
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollview">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/textView1"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</ScrollView>
3.Copy and past the code for manifest.xml
<?xml
version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ambilpursunil.newapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"
/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
When our application launch data will be visible on the screen.
Please Send Your Comments To ambilpura.sunil@gmail.com
Stay Tune For Next Tutorial... WebServices Using GET Method In Android:
No comments:
Post a Comment