服務器端使用MySql數據庫,固然也能夠換成其餘數據庫。本例子主要邏輯是:Android客戶端發送請求;服務器端接收請求,查看數據庫內容,返回數據;Android客戶端接收返回的信息。android
下面先給出Android客戶端的代碼:數據庫
public class MainActivity extends AppCompatActivity { private TextView textview; HttpClient httpClient; HttpResponse httpResponse; HttpEntity entity; Button btn_login; EditText editText; EditText editText2; Handler pic_hdl; String login_name; String login_pw; Handler handler = new Handler() { public void handleMessage(Message msg) { System.out.println("handler"); textview.setText(""); if (msg.what == 0x123) { //使用TextView文本顯示服務器響應 //使用setText會覆蓋原有的內容; 使用append不會覆蓋原有的內容,只會在原有內容後面添加要顯示的新內容 //textview.setText(msg.obj.toString() + "\n"); textview.append(msg.obj.toString() + "\n"); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textview = (TextView) findViewById(R.id.textView3); //建立DefaultClient對象 httpClient = new DefaultHttpClient(); editText = (EditText) findViewById(R.id.editText); editText2 = (EditText) findViewById(R.id.editText2); btn_login = (Button) findViewById(R.id.button); btn_login.setOnClickListener(new btn_login_onclicklistener()); } class btn_login_onclicklistener implements View.OnClickListener { @Override public void onClick(View v) { login_name = editText.getText().toString().trim(); login_pw = editText2.getText().toString().trim(); new Thread() { @Override public void run() { Thread t = new LoadPicThread(); t.start(); //httpclientGet(Login_name, Login_pw); }//run() }.start(); }//onClick() } //方法1,這種方式傳輸的值是中文時,在服務器上接收時會顯示亂碼 class LoadPicThread extends Thread { @Override public void run() { String name = MainActivity.this.login_name; String pw = MainActivity.this.login_pw; System.out.println("1"); HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = newHttpPost("http://192.168.1.108:8080/TestService/mymain/LoginServlet?Login_name=" + name + "&Login_pw=" + pw + ""); try { System.out.println("2"); HttpResponse rsp = httpClient.execute(httpPost); System.out.println("3.1.1"); HttpEntity entity = rsp.getEntity(); if (entity != null) { System.out.println("3.1"); InputStream is = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String line = null; while ((line = reader.readLine()) != null) { System.out.println(line); System.out.println("3.3"); Message msg = new Message(); msg.what = 0x123; msg.obj = line; handler.sendMessage(msg); }//while } else System.out.println("3.2"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }//run }//class.LoadPicThread //方法二 class postThread extends Thread{ @Override public void run(){ //取得界面輸入的用戶名和密碼 String name = MainActivity.this.login_name; String pw = MainActivity.this.login_pw; System.out.println("1"); try{ HttpClient httpClient = new DefaultHttpClient(); HttpPost post = newHttpPost("http://192.168.1.108:8080/TestService/mymain/LoginServlet"); System.out.println("2"); //若是傳遞多個參數,歲參數先進行封裝 List params=new ArrayList<>(); params.add(new BasicNameValuePair("Login_name",name)); params.add(new BasicNameValuePair("Login_pw",pw)); //設置請求參數 post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); //發送POST請求 HttpResponse response=httpClient.execute(post); //若是服務器成功的返回了響應 if(response.getStatusLine().getStatusCode()==200){ System.out.println("3.3"); Message msg = new Message(); msg.what = 0x123; msg.obj= EntityUtils.toString(response.getEntity()); Looper.prepare(); handler.sendMessage(msg); } }catch (Exception e){ e.printStackTrace(); } } } }
本客戶端是基於Android Studio來完成的,要是客戶端聯網,還須要在AndroidManifest.xml文件中加入:
<</span>uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<<span style="color:#000080;font-weight:bold;">uses-permissionandroid:name="android.permission.INTERNET" />
注:如不能識別Apache中的一些方法,則需在build.gradle中的android{}中添加useLibrary'org.apache.http.legacy'
例如個人項目添加後是這樣的:android {
compileSdkVersion 23 buildToolsVersion "23.0.3" useLibrary 'org.apache.http.legacy'
下一篇將會講到本客戶端相關的服務器開發,請你們多多關注!