<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.web.test"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".HTTPPostExample"
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>
<uses-sdk android:minSdkVersion="7" />
</manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.web.test"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".HTTPPostExample"
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>
<uses-sdk android:minSdkVersion="7" />
</manifest>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<Button
android:id="@+id/btn_sign_in"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text="Sign In"
android:layout_x="103px"
android:layout_y="197px"/>
<EditText
android:id="@+id/edit_id"
android:layout_width="250px"
android:layout_height="wrap_content"
android:hint="Username"
android:singleLine="true"
android:textSize="18sp"
android:layout_x="40px"
android:layout_y="32px" />
<EditText
android:id="@+id/edit_pwd"
android:layout_width="250px"
android:layout_height="wrap_content"
android:hint="Password"
android:singleLine="true"
android:textSize="18sp"
android:password="true"
android:layout_x="40px"
android:layout_y="86px" />
</AbsoluteLayout>
<AbsoluteLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<Button
android:id="@+id/btn_sign_in"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text="Sign In"
android:layout_x="103px"
android:layout_y="197px"/>
<EditText
android:id="@+id/edit_id"
android:layout_width="250px"
android:layout_height="wrap_content"
android:hint="Username"
android:singleLine="true"
android:textSize="18sp"
android:layout_x="40px"
android:layout_y="32px" />
<EditText
android:id="@+id/edit_pwd"
android:layout_width="250px"
android:layout_height="wrap_content"
android:hint="Password"
android:singleLine="true"
android:textSize="18sp"
android:password="true"
android:layout_x="40px"
android:layout_y="86px" />
</AbsoluteLayout>
HTTPPostExample.java(Activity class)
package android.web.test;
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import org.apache.http.*;
import org.apache.http.client.entity.*;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.*;
import org.apache.http.message.*;
import org.apache.http.protocol.HTTP;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import android.app.*;
import android.os.*;
import android.util.*;
import android.view.View;
import android.widget.*;
public class HTTPPostExample extends Activity {
EditText editId, editPwd;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Button btnLogin = (Button)findViewById(R.id.btn_sign_in);
editId = (EditText)findViewById(R.id.edit_id);
editPwd = (EditText)findViewById(R.id.edit_pwd);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doLogin();
}
});
}
private void doLogin(){
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.123.100:80/HelloWeb/androidPost.jsp");
List nvps = new ArrayList();
nvps.add(new BasicNameValuePair("id", editId.getText().toString()));
nvps.add(new BasicNameValuePair("pwd", editPwd.getText().toString()));
try {
UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps,HTTP.UTF_8);
httppost.setEntity(p_entity);
HttpResponse response = client.execute(httppost);
Log.v("STATUS", response.getStatusLine().toString());
HttpEntity responseEntity = response.getEntity();
Log.v("Entity", "Set response to responseEntity");
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
LoginHandler myLoginHandler = new LoginHandler(this);
xr.setContentHandler(myLoginHandler);
xr.parse(retrieveInputStream(responseEntity));
}catch(Exception e){
Log.e("ERROR--doLogin()", e.getMessage());
}
}
private InputSource retrieveInputStream(HttpEntity httpEntity) {
InputSource insrc = null;
try {
insrc = new InputSource(httpEntity.getContent());
} catch (Exception e) {
Log.e("ERROR--retrieveInputStream()", e.getMessage());
}
return insrc;
}
}
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import org.apache.http.*;
import org.apache.http.client.entity.*;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.*;
import org.apache.http.message.*;
import org.apache.http.protocol.HTTP;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import android.app.*;
import android.os.*;
import android.util.*;
import android.view.View;
import android.widget.*;
public class HTTPPostExample extends Activity {
EditText editId, editPwd;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Button btnLogin = (Button)findViewById(R.id.btn_sign_in);
editId = (EditText)findViewById(R.id.edit_id);
editPwd = (EditText)findViewById(R.id.edit_pwd);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doLogin();
}
});
}
private void doLogin(){
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.123.100:80/HelloWeb/androidPost.jsp");
List nvps = new ArrayList();
nvps.add(new BasicNameValuePair("id", editId.getText().toString()));
nvps.add(new BasicNameValuePair("pwd", editPwd.getText().toString()));
try {
UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps,HTTP.UTF_8);
httppost.setEntity(p_entity);
HttpResponse response = client.execute(httppost);
Log.v("STATUS", response.getStatusLine().toString());
HttpEntity responseEntity = response.getEntity();
Log.v("Entity", "Set response to responseEntity");
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
LoginHandler myLoginHandler = new LoginHandler(this);
xr.setContentHandler(myLoginHandler);
xr.parse(retrieveInputStream(responseEntity));
}catch(Exception e){
Log.e("ERROR--doLogin()", e.getMessage());
}
}
private InputSource retrieveInputStream(HttpEntity httpEntity) {
InputSource insrc = null;
try {
insrc = new InputSource(httpEntity.getContent());
} catch (Exception e) {
Log.e("ERROR--retrieveInputStream()", e.getMessage());
}
return insrc;
}
}
LoginHandler.java (SAX Event Handler class)
class LoginHandler extends DefaultHandler {
private boolean login = false;
private boolean id = false;
private boolean pwd = false;
private Activity activity;
public LoginHandler(Activity activity){
this.activity = activity;
}
@Override
public void startDocument() throws SAXException {
}
@Override
public void endDocument() throws SAXException {
// Nothing to do
}
@Override
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
if (localName.equals("login")) {
this.login = true;
} else if (localName.equals("id")) {
this.id = true;
} else if (localName.equals("pwd")) {
this.pwd = true;
}
}
@Override
public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
if (localName.equals("login"))
this.login = false;
else if (localName.equals("id"))
this.id = false;
else if (localName.equals("pwd"))
this.pwd = false;
}
@Override
public void characters(char ch[], int start, int length) {
if (this.id) {
Toast.makeText(activity, new String(ch, start,length), Toast.LENGTH_SHORT).show();
} else if (this.pwd) {
Toast.makeText(activity, new String(ch, start,length), Toast.LENGTH_SHORT).show();
}
}
}
private boolean login = false;
private boolean id = false;
private boolean pwd = false;
private Activity activity;
public LoginHandler(Activity activity){
this.activity = activity;
}
@Override
public void startDocument() throws SAXException {
}
@Override
public void endDocument() throws SAXException {
// Nothing to do
}
@Override
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
if (localName.equals("login")) {
this.login = true;
} else if (localName.equals("id")) {
this.id = true;
} else if (localName.equals("pwd")) {
this.pwd = true;
}
}
@Override
public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
if (localName.equals("login"))
this.login = false;
else if (localName.equals("id"))
this.id = false;
else if (localName.equals("pwd"))
this.pwd = false;
}
@Override
public void characters(char ch[], int start, int length) {
if (this.id) {
Toast.makeText(activity, new String(ch, start,length), Toast.LENGTH_SHORT).show();
} else if (this.pwd) {
Toast.makeText(activity, new String(ch, start,length), Toast.LENGTH_SHORT).show();
}
}
}
androidPost.jsp
<%@ page language="java" contentType="text/xml; charset=UTF-8"
pageEncoding="EUC-KR"%><?xml version="1.0" encoding="utf-8" ?>
<login>
<%
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
String pwd = request.getParameter("pwd");
System.out.println("id="+id);
System.out.println("pwd="+pwd);
%>
<id><%=id%></id>
<pwd><%=pwd%></pwd>
</login>
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.