【伺服器+手機端項目】Android+Servlet+JSON綜合案例之Android結合serlvet【三】

來源:互聯網
上載者:User

終於到最後關頭了, 有點迫不及待啦,興奮咯~~~!

package com.example.servletandroiddemo;import com.example.servletandroiddemo.bean.ServletApplication;import com.example.servletandroiddemo.util.DownLoadUtil;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class RegisterActivity extends Activity implements OnClickListener {private EditText et_name, et_passwd, et_mail;private Button bt_ok;private Handler mHandler;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);System.out.println(msg.what);if (msg.what == ServletApplication.FAIL) {Toast.makeText(RegisterActivity.this, "註冊錯誤", 1000).show();} else if (msg.what == ServletApplication.SUCCESS) {Toast.makeText(RegisterActivity.this, "註冊成功", 1000).show();} else if (msg.what == ServletApplication.ERROR) {Toast.makeText(RegisterActivity.this, "請求異常", 1000).show();}}};setContentView(R.layout.register);getViews();setListener();}private void getViews() {// TODO Auto-generated method stubet_name = (EditText) findViewById(R.id.name);et_passwd = (EditText) findViewById(R.id.passwd);et_mail = (EditText) findViewById(R.id.mail);bt_ok = (Button) findViewById(R.id.ok);}private void setListener() {// TODO Auto-generated method stubbt_ok.setOnClickListener(this);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.ok:String name = et_name.getText().toString().trim();String passwd = et_passwd.getText().toString().trim();String mail = et_mail.getText().toString().trim();submitInfo(name, passwd, mail);break;}}/** * 提交註冊資訊 *  * @param name *            使用者名稱 * @param passwd *            密碼 * @param mail *            郵箱 */private void submitInfo(String name, String passwd, String mail) {// TODO Auto-generated method stubString registerpath = "http://193.168.1.122:8080/JsonDemo/servlet/RegisterServlet?type=register&name="+ name + "&passwd=" + passwd + "&mail=" + mail;System.out.println(registerpath);new DownLoadUtil(registerpath, mHandler).start();}}

註:我這裡沒有返回json格式的具體資料,因為我只返回了狀態代碼。不過很類似,在我的完整項目中, 有一個這樣的例子, 可以看下是怎麼做到的。

這裡主要是吧思路跟大家講清楚, 如果想要下載完整的項目的話點擊以下地址:(資料庫的名稱記得和訪問地址得改成自己設計的。

:http://download.csdn.net/detail/iliaokun/5749333

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.