To create the layout like this write following code inside activity_main
xml file:
Activity_main.xml
<RelativeLayoutxmlns: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">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Enter
Name:"
android:textSize="18dp"/>
<EditText
android:id="@+id/enterage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:ems="10"
android:inputType="number"/>
<TextView
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/enterage"
android:text="Enter
Age"
android:textSize="18dp"/>
<EditText
android:id="@+id/entername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/age"
android:ems="10"/>
<Button
android:id="@+id/ViewData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/entername"
android:layout_marginRight="28dp"
android:layout_marginTop="47dp"
android:text="ViewData"/>
<Button
android:id="@+id/Submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/ViewData"
android:layout_alignBottom="@+id/ViewData"
android:layout_alignParentLeft="true"
android:layout_marginLeft="39dp"
android:text="Submit"/>
</RelativeLayout>
Step 4:
Now Main_Activity
java file will handle all the request from the user to enter the name and age
of user into the MySQL database.
In this firstly I
fetch the name and age enter by the user and pass that detail to the custom
servlet (which I have created) with the help of AsyncTask service method
doInBackground().
Write the following
code inside the Main_Activity.java file:
MainActivity.java
publicclass MainActivity extends Activity implements OnClickListener {
public String EXTRA_DATA;
String
results = "";
Button
button;
Button
viewdata;
String
post_result = "";
static
ArrayList<UserDetail>user;
@Override
publicvoid onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
// pointing towards
the activity_main xml layout file
setContentView(R.layout.activity_main);
// button which
handles the Submit request
button = (Button)
findViewById(R.id.Submit);
// listener for
Submit button
button.setOnClickListener(new OnClickListener() {
publicvoid onClick(View view) {
button.setClickable(false);
// call
LongRunningPostIO class doInBackGround() method
// to submit data
into database
new
LongRunningPostIO().execute();
}
});
// button which
handles View_Data request
viewdata = (Button)
findViewById(R.id.ViewData);
// listener for
view_Data
viewdata.setOnClickListener(new OnClickListener() {
publicvoid onClick(View view) {
// call
LongRunningGetIO class doInBackGround() method
// to retrieve data
from MySQL database
newLongRunningGetIO().execute();
}
});
}
privateclassLongRunningGetIOextends AsyncTask<Void, Void, String> {
// method which
store the response from server into the results variable
protected String
getASCIIContentFromEntity(HttpEntity entity)
throws
IllegalStateException, IOException {
InputStream
in = entity.getContent();
StringBuffer
out = new StringBuffer();
int n = 1;
while (n > 0) {
byte[] b = newbyte[4096];
n
= in.read(b);
if (n > 0)
out.append(new String(b, 0, n));
}
results = out.toString();
// passing data to
getData() method to parse the JSON response
getData(results);
return out.toString();
}
@Override
protected String
doInBackground(Void... params) {
HttpClient
httpClient = new DefaultHttpClient();
HttpContext
localContext = new BasicHttpContext();
/*
* Here remember to give local address of
server as 10.0.2.2 in
* emulator localhost not going to work
HttpGetServlet is the
* servlet that handles my request to
view database HttpGetData
* is the .java file of HttpGetServlet servlet
*/
HttpGet
httpGet = new HttpGet(
"http://10.0.2.2:8080/HttpGetServlet/HttpGetData");
String
text = null;
try {
// get the response
from the server
HttpResponse
response = httpClient.execute(httpGet,
localContext);
HttpEntity
entity = response.getEntity();
// call above
method to obtain the response in an String
text
= getASCIIContentFromEntity(entity);
}
catch (Exception e) {
e.printStackTrace();
return
e.getLocalizedMessage();
}
return text;
}
protectedvoid onPostExecute(String
results) {
// this is the
method that automatically calls
// after
doInBackGround() get called
}
}
publicvoid getData(String
results) {
if (results == "") {
// if the result
obtained is blank
Toast.makeText(getApplicationContext(),
"Sorry No data
in database", Toast.LENGTH_SHORT).show();
}
else {
try {
// parse the JSON
result to obtain the data
// store it into an
List<UserDetail>
user = new
ArrayList<UserDetail>();
JSONArray
ja = new JSONArray(results);
ArrayList<String>arrays
= new
ArrayList<String>();
int n1 = ja.length();
for (int i = 0; i < n1;
i++) {
UserDetail
detail = new UserDetail();
JSONObject
jo = ja.getJSONObject(i);
String
name = jo.getString("name");
String
age = jo.getString("age");
detail.setName(name);
detail.setAge(age);
user.add(detail);
}
// call another
activity to display the data to the user
// in the form of
custom ListView
Intent
i = new Intent(this, List_Name_Age.class);
startActivity(i);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
publicvoid onClick(View arg0) {
}
privateclass LongRunningPostIO extends AsyncTask<Void,
Void, String> {
protected String
getASCIIContentFromEntity(HttpEntity entity)
throws
IllegalStateException, IOException {
InputStream
in = entity.getContent();
StringBuffer
out = new StringBuffer();
int n = 1;
while (n > 0) {
byte[] b = newbyte[4096];
n
= in.read(b);
if (n > 0)
out.append(new String(b, 0, n));
}
post_result = out.toString();
if (post_result != "") {
Toast.makeText(getApplicationContext(),
"Your Data has
been inserted", Toast.LENGTH_SHORT)
.show();
}
else {
Toast.makeText(getApplicationContext(),
"Sorry! data
can't be inserted", Toast.LENGTH_SHORT)
.show();
}
return out.toString();
}
@Override
protected String
doInBackground(Void... arg0) {
// Get the name
enter by the user
EditText
name = (EditText) findViewById(R.id.entername);
// Get the age
enter by the user
EditText
age = (EditText) findViewById(R.id.enterage);
// convert name and
age into the String form
String
entered_name = name.getText().toString();
String entered_age =
age.getText().toString();
HttpClient
httpClient = new DefaultHttpClient();
HttpContext
localContext = new BasicHttpContext();
// pass name and
age parameter to the server
HttpGet
httpGet = new HttpGet(
"http://10.0.2.2:8080/HttpGetServlet/HelloWorldServlet?name="
+
entered_name + "&age=" + entered_age);
String
text = null;
try {
HttpResponse
response = httpClient.execute(httpGet,
localContext);
HttpEntity
entity = response.getEntity();
text
= getASCIIContentFromEntity(entity);
}
catch (Exception e) {
e.printStackTrace();
return
e.getLocalizedMessage();
}
return text;
}
protectedvoid onPostExecute(String
results) {
}
}
No comments:
Post a Comment