服务器端使用MySql数据库,当然也可以换成其他数据库。本例子主要逻辑是: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'
下一篇将会讲到本客户端相关的服务器开发,请大家多多关注!