因为要做一个软件注册的功能,所以今天做了一个小例子,这里的服务器是自己的tomcat服务器,做过j2ee的应该是再熟悉不过了。
RegisterActivity.java
public class RegisterActivity extends Activity {
private static final String uriConnection = "10.0.2.2:8080/Register/servlet/RegisterServlet";
//private static final String uriConnection = "192.168.0.5:8080/Register/servlet/RegisterServlet";
Button registerButton;
Button registerLaterButton;
ProgressDialog progressDialog;
EditText firstText;
EditText lastText;
EditText emailText;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(ister);
Create(savedInstanceState);
firstText = (EditText) findViewById(ister_first_name_id);
lastText = (EditText) findViewById(ister_last_name_id);
emailText = (EditText) findViewById(ister_email_id);
registerButton = (Button) findViewById(ister_button_id);
registerLaterButton = (Button) findViewById(ister_later_button_id);
registerButton.setOnClickListener(registerListener);
registerLaterButton.setOnClickListener(registerLaterButtonListener);
}
Handler handler = new Handler() {
public void handleMessage(Message msg) {
progressDialog.dismiss();
finish();
};
};
View.OnClickListener registerListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
String firstName = Text().toString();
String lastName = Text().toString();
String emailName = Text().toString();
progressDialog = ProgressDialog.show(RegisterActivity.this,
"Register", "");
registerToHost(firstName, lastName, emailName);
}
};
// 处理客户端和服务器端的请求
private void registerToHost(String firstName, String lastName,
String emailName) {
HttpPost httpRequest = new HttpPost(uriConnection);
HttpResponse httpResponse = null;
List<NameValuePair> params = new ArrayList<NameValuePair>();
if (!firstName.equals("") && !lastName.equals("")
&& !emailName.equals("")) {
params.add(new BasicNameValuePair("firstName", firstName));
params.add(new BasicNameValuePair("lastName", lastName));
params.add(new BasicNameValuePair("emailName", emailName));
}
try {
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
httpResponse = new DefaultHttpClient().execute(httpRequest);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 处理从服务器短来的数据
if (StatusLine().getStatusCode() == 200) {
System.out.println("Connection OK");
handler.sendEmptyMessage(0);
/*
* byte[] data = new byte[2048]; try { data =
* Entity()); } catch
* (IOException e) { // TODO Auto-generated catch block
* e.printStackTrace(); }
*/
}
}
View.OnClickListener registerLaterButtonListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
};
}
l:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout android:orientation="vertical"
android:id="@+id/registration_screen_layout_id" android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="schemas.android/apk/res/android"> textstyle
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论