Android与Tomcat远程访问

因为要做一个软件注册的功能,所以今天做了一个小例子,这里的服务器是自己的tomcat服务器,做过j2ee的应该是再熟悉不过了。

RegisterActivity.java

public class RegisterActivity extends Activity { private static final String uriConnection = "http://10.0.2.2:8080/Register/servlet/RegisterServlet"; //private static final String uriConnection = "http://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(R.layout.register); super.onCreate(savedInstanceState); firstText = (EditText) findViewById(R.id.register_first_name_id); lastText = (EditText) findViewById(R.id.register_last_name_id); emailText = (EditText) findViewById(R.id.register_email_id); registerButton = (Button) findViewById(R.id.register_button_id); registerLaterButton = (Button) findViewById(R.id.register_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 = firstText.getText().toString(); String lastName = lastText.getText().toString(); String emailName = emailText.getText().toString(); progressDialog = ProgressDialog.show(RegisterActivity.this, "Register", "Registering waitting....."); 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 (httpResponse.getStatusLine().getStatusCode() == 200) { System.out.println("Connection OK"); handler.sendEmptyMessage(0); /* * byte[] data = new byte[2048]; try { data = * EntityUtils.toByteArray(httpResponse.getEntity()); } catch * (IOException e) { // TODO Auto-generated catch block * e.printStackTrace(); } */ } } View.OnClickListener registerLaterButtonListener = new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }; }

layout/register.xml:

<?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="http://schemas.android.com/apk/res/android"> <ScrollView android:scrollbarStyle="outsideInset" android:id="@+id/registration_screen_scroll_view_id" android:background="#ffffffff" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1.0" xmlns:android="http://schemas.android.com/apk/res/android"> <LinearLayout android:orientation="vertical" android:background="#ffffffff" android:layout_width="fill_parent" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView android:textSize="14.0dip" android:textColor="#ff000000" android:gravity="left" android:id="@+id/registration_benefits_view_id" android:paddingLeft="30.0dip" android:paddingTop="10.0dip" android:paddingRight="30.0dip" android:paddingBottom="10.0dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/STR_REGISTRATION_BENEFITS" android:layout_weight="0.0" /> <EditText android:textSize="20.0dip" android:id="@+id/register_first_name_id" android:background="@android:drawable/edit_text" android:paddingLeft="10.0dip" android:paddingRight="10.0dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="30.0dip" android:layout_marginRight="30.0dip" android:minHeight="60.0dip" android:hint="@string/STR_FIRST_NAME" android:maxLines="1" android:singleLine="true" android:selectAllOnFocus="true" android:layout_weight="1.0" /> <EditText android:textSize="20.0dip" android:id="@+id/register_last_name_id" android:background="@android:drawable/edit_text" android:paddingLeft="10.0dip" android:paddingRight="10.0dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="30.0dip" android:layout_marginRight="30.0dip" android:minHeight="60.0dip" android:hint="@string/STR_LAST_NAME" android:maxLines="1" android:singleLine="true" android:selectAllOnFocus="true" android:layout_weight="1.0" /> <EditText android:textSize="20.0dip" android:id="@+id/register_email_id" android:background="@android:drawable/edit_text" android:paddingLeft="10.0dip" android:paddingRight="10.0dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="30.0dip" android:layout_marginRight="30.0dip" android:minHeight="60.0dip" android:hint="@string/STR_EMAIL" android:maxLines="1" android:singleLine="true" android:selectAllOnFocus="true" android:layout_weight="1.0" android:inputType="textEmailAddress" /> <!--<view android:gravity="left" android:layout_gravity="center" android:id="@+id/registration_privacy_link_id" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="23.0dip" android:layout_marginRight="23.0dip" android:layout_weight="0.0" class="com.dataviz.dxtg.common.android.SelfExpandingWebView" /> --></LinearLayout> </ScrollView> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="5.0dip" android:layout_weight="0.0" xmlns:android="http://schemas.android.com/apk/res/android"> <Button android:textSize="18.0dip" android:textStyle="bold" android:id="@+id/register_button_id" android:focusable="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/STR_REGISTER_NOW" android:layout_alignParentRight="true" /> <Button android:textSize="18.0dip" android:textStyle="bold" android:id="@+id/register_later_button_id" android:focusable="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/STR_REGISTER_LATER" android:layout_toLeftOf="@id/register_button_id" /> <Button android:textSize="18.0dip" android:textStyle="bold" android:id="@+id/registration_register_force_close_button_id" android:focusable="true" android:visibility="gone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/STR_EXIT" android:layout_toLeftOf="@id/register_button_id" /> </RelativeLayout> </LinearLayout>

而在服务器端很简单就是一个servlet接收传递过来的值就可以了,然后通过传递过来的值做相应的操作。

public class RegisterServlet extends HttpServlet{ public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ String firstName=request.getParameter("firstName"); String lastName=request.getParameter("lastName"); String emailName=request.getParameter("emailName"); //response.setContentType(C); System.out.println("firstName:"+firstName); System.out.println("lastName:"+lastName); System.out.println("emailName:"+emailName); } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值