现在的位置: 首页 > 综合 > 正文

Android与Tomcat远程访问

2014年03月10日 ⁄ 综合 ⁄ 共 16908字 ⁄ 字号 评论关闭

原文出自:http://blog.csdn.net/GaoMatrix/article/details/6220647

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

RegisterActivity.java

  1. public class RegisterActivity extends Activity {  
  2.     private static final String uriConnection = "http://10.0.2.2:8080/Register/servlet/RegisterServlet";  
  3.     //private static final String uriConnection = "http://192.168.0.5:8080/Register/servlet/RegisterServlet";  
  4.     Button registerButton;  
  5.     Button registerLaterButton;  
  6.     ProgressDialog progressDialog;  
  7.     EditText firstText;  
  8.     EditText lastText;  
  9.     EditText emailText;  
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         setContentView(R.layout.register);  
  13.         super.onCreate(savedInstanceState);  
  14.         firstText = (EditText) findViewById(R.id.register_first_name_id);  
  15.         lastText = (EditText) findViewById(R.id.register_last_name_id);  
  16.         emailText = (EditText) findViewById(R.id.register_email_id);  
  17.         registerButton = (Button) findViewById(R.id.register_button_id);  
  18.         registerLaterButton = (Button) findViewById(R.id.register_later_button_id);  
  19.         registerButton.setOnClickListener(registerListener);  
  20.         registerLaterButton.setOnClickListener(registerLaterButtonListener);  
  21.     }  
  22.     Handler handler = new Handler() {  
  23.         public void handleMessage(Message msg) {  
  24.             progressDialog.dismiss();  
  25.             finish();  
  26.         };  
  27.     };  
  28.     View.OnClickListener registerListener = new View.OnClickListener() {  
  29.         @Override  
  30.         public void onClick(View v) {  
  31.             String firstName = firstText.getText().toString();  
  32.             String lastName = lastText.getText().toString();  
  33.             String emailName = emailText.getText().toString();  
  34.             progressDialog = ProgressDialog.show(RegisterActivity.this,  
  35.                     "Register""Registering waitting.....");  
  36.             registerToHost(firstName, lastName, emailName);  
  37.         }  
  38.     };  
  39.     // 处理客户端和服务器端的请求  
  40.     private void registerToHost(String firstName, String lastName,  
  41.             String emailName) {  
  42.         HttpPost httpRequest = new HttpPost(uriConnection);  
  43.         HttpResponse httpResponse = null;  
  44.         List<NameValuePair> params = new ArrayList<NameValuePair>();  
  45.         if (!firstName.equals("") && !lastName.equals("")  
  46.                 && !emailName.equals("")) {  
  47.             params.add(new BasicNameValuePair("firstName", firstName));  
  48.             params.add(new BasicNameValuePair("lastName", lastName));  
  49.             params.add(new BasicNameValuePair("emailName", emailName));  
  50.         }  
  51.         try {  
  52.             httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));  
  53.             httpResponse = new DefaultHttpClient().execute(httpRequest);  
  54.         } catch (UnsupportedEncodingException e) {  
  55.             // TODO Auto-generated catch block  
  56.             e.printStackTrace();  
  57.         } catch (ClientProtocolException e) {  
  58.             // TODO Auto-generated catch block  
  59.             e.printStackTrace();  
  60.         } catch (IOException e) {  
  61.             // TODO Auto-generated catch block  
  62.             e.printStackTrace();  
  63.         }  
  64.         // 处理从服务器短来的数据  
  65.         if (httpResponse.getStatusLine().getStatusCode() == 200) {  
  66.             System.out.println("Connection OK");  
  67.             handler.sendEmptyMessage(0);  
  68.             /* 
  69.              * byte[] data = new byte[2048]; try { data = 
  70.              * EntityUtils.toByteArray(httpResponse.getEntity()); } catch 
  71.              * (IOException e) { // TODO Auto-generated catch block 
  72.              * e.printStackTrace(); } 
  73.              */  
  74.         }  
  75.     }  
  76.     View.OnClickListener registerLaterButtonListener = new View.OnClickListener() {  
  77.         @Override  
  78.         public void onClick(View v) {  
  79.             finish();  
  80.         }  
  81.     };  
  82. }  

 

layout/register.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <LinearLayout android:orientation="vertical"  
  3.     android:id="@+id/registration_screen_layout_id" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">  
  5.     <ScrollView android:scrollbarStyle="outsideInset"  
  6.         android:id="@+id/registration_screen_scroll_view_id"  
  7.         android:background="#ffffffff" android:layout_width="fill_parent"  
  8.         android:layout_height="fill_parent" android:layout_weight="1.0"  
  9.         xmlns:android="http://schemas.android.com/apk/res/android">  
  10.         <LinearLayout android:orientation="vertical"  
  11.             android:background="#ffffffff" android:layout_width="fill_parent"  
  12.             android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android">  
  13.             <TextView android:textSize="14.0dip" android:textColor="#ff000000"  
  14.                 android:gravity="left" android:id="@+id/registration_benefits_view_id"  
  15.                 android:paddingLeft="30.0dip" android:paddingTop="10.0dip"  
  16.                 android:paddingRight="30.0dip" android:paddingBottom="10.0dip"  
  17.                 android:layout_width="fill_parent" android:layout_height="wrap_content"  
  18.                 android:text="@string/STR_REGISTRATION_BENEFITS"  
  19.                 android:layout_weight="0.0" />  
  20.             <EditText android:textSize="20.0dip" android:id="@+id/register_first_name_id"  
  21.                 android:background="@android:drawable/edit_text"  
  22.                 android:paddingLeft="10.0dip" android:paddingRight="10.0dip"  
  23.                 android:layout_width="fill_parent" android:layout_height="wrap_content"  
  24.                 android:layout_marginLeft="30.0dip" android:layout_marginRight="30.0dip"  
  25.                 android:minHeight="60.0dip" android:hint="@string/STR_FIRST_NAME"  
  26.                 android:maxLines="1" android:singleLine="true"  
  27.                 android:selectAllOnFocus="true" android:layout_weight="1.0" />  
  28.             <EditText android:textSize="20.0dip" android:id="@+id/register_last_name_id"  
  29.                 android:background="@android:drawable/edit_text"  
  30.                 android:paddingLeft="10.0dip" android:paddingRight="10.0dip"  
  31.                 android:layout_width="fill_parent" android:layout_height="wrap_content"  
  32.                 android:layout_marginLeft="30.0dip" android:layout_marginRight="30.0dip"  
  33.                 android:minHeight="60.0dip" android:hint="@string/STR_LAST_NAME"  
  34.                 android:maxLines="1" android:singleLine="true"  
  35.                 android:selectAllOnFocus="true" android:layout_weight="1.0" />  
  36.             <EditText android:textSize="20.0dip" android:id="@+id/register_email_id"  
  37.                 android:background="@android:drawable/edit_text"  
  38.                 android:paddingLeft="10.0dip" android:paddingRight="10.0dip"  
  39.                 android:layout_width="fill_parent" android:layout_height="wrap_content"  
  40.                 android:layout_marginLeft="30.0dip" android:layout_marginRight="30.0dip"  
  41.                 android:minHeight="60.0dip" android:hint="@string/STR_EMAIL"  
  42.                 android:maxLines="1" android:singleLine="true"  
  43.                 android:selectAllOnFocus="true" android:layout_weight="1.0"  
  44.                 android:inputType="textEmailAddress" />  
  45.             <!--<view android:gravity="left" android:layout_gravity="center"  
  46.                 android:id="@+id/registration_privacy_link_id" android:layout_width="fill_parent"  
  47.                 android:layout_height="wrap_content" android:layout_marginLeft="23.0dip"  
  48.                 android:layout_marginRight="23.0dip" android:layout_weight="0.0"  
  49.                 class="com.dataviz.dxtg.common.android.SelfExpandingWebView" />  
  50.         --></LinearLayout>  
  51.     </ScrollView>  
  52.     <RelativeLayout android:layout_width="fill_parent"  
  53.         android:layout_height="wrap_content" android:layout_marginTop="5.0dip"  
  54.         android:layout_weight="0.0" xmlns:android="http://schemas.android.com/apk/res/android">  
  55.         <Button android:textSize="18.0dip" android:textStyle="bold"  
  56.             android:id="@+id/register_button_id" android:focusable="true"  
  57.             android:layout_width="wrap_content" android:layout_height="wrap_content"  
  58.             android:text="@string/STR_REGISTER_NOW"  
  59.             android:layout_alignParentRight="true" />  
  60.         <Button android:textSize="18.0dip" android:textStyle="bold"  
  61.             android:id="@+id/register_later_button_id"  
  62.             android:focusable="true" android:layout_width="wrap_content"  
  63.             android:layout_height="wrap_content" android:text="@string/STR_REGISTER_LATER"  
  64.             android:layout_toLeftOf="@id/register_button_id" />  
  65.         <Button android:textSize="18.0dip" android:textStyle="bold"  
  66.             android:id="@+id/registration_register_force_close_button_id"  
  67.             android:focusable="true" android:visibility="gone"  
  68.             android:layout_width="wrap_content" android:layout_height="wrap_content"  
  69.             android:text="@string/STR_EXIT" android:layout_toLeftOf="@id/register_button_id" />  
  70.     </RelativeLayout>  
  71. </LinearLayout>  

 

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

  1. public class RegisterServlet extends HttpServlet{  
  2.     public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{  
  3.         String firstName=request.getParameter("firstName");  
  4.         String lastName=request.getParameter("lastName");  
  5.         String emailName=request.getParameter("emailName");  
  6.         //response.setContentType(C);  
  7.         System.out.println("firstName:"+firstName);  
  8.         System.out.println("lastName:"+lastName);  
  9.         System.out.println("emailName:"+emailName);  
  10.     }  
  11. }  

 

抱歉!评论已关闭.