如果对Android连接服务器原理以及流程不了解同学可以先看看我的这篇分析连接流程的博客
Android连接服务器流程详解
下面是代码部分,解释一下用到的技术,客户端采用最原始的HttpUrlConnnection,服务器端用的Servlet,现在我不建议采用这样的技术了。
客户端使用Okhttp,服务器端使用Struts框架即可。
配置
客户端 android studio 3.2 +netBeans(编写Java web的小众软件,MyEclipse也可以)
服务器 windows server 2012R2版 +XAMMP(Apache+MySQL+PHP+tomacat集成软件包)+MySQL-Front
客户端代码
Activity-login.xml
LogindemoActivity
import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import java.util.ArrayList; import java.util.List; public class LoginDemoActivity extends AppCompatActivity { /** Called when the activity is first created. */ private Button loginBtn; private EditText inputUsername; private EditText inputPassword; private String responseMsg = ""; private static final int REQUEST_TIMEOUT = 5*1000;//设置请求超时10秒钟 private static final int SO_TIMEOUT = 10*1000; //设置等待数据超时时间10秒钟 private static final int LOGIN_OK = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login_demo); loginBtn = (Button)findViewById(R.id.login_btn_login); inputUsername = (EditText)findViewById(R.id.login_edit_account); inputPassword = (EditText)findViewById(R.id.login_edit_pwd); loginBtn.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { Thread loginThread = new Thread(new LoginThread()); loginThread.start(); } }); } private boolean loginServer(String username, String password) { boolean loginValidate = false; //使用apache HTTP客户端实现 String urlStr = "http://服务器IP/Javaweb项目名/LoginServlet"; HttpPost request = new HttpPost(urlStr); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("username",username)); params.add(new BasicNameValuePair("password",password)); try { //设置请求参数项 request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); HttpClient client = getHttpClient(); //执行请求返回相应 HttpResponse response = client.execute(request); //判断是否请求成功 if(response.getStatusLine().getStatusCode()==200) { loginValidate = true; //获得响应信息 responseMsg = EntityUtils.toString(response.getEntity()); } }catch(Exception e) { e.printStackTrace(); } return loginValidate; } //初始化HttpClient public HttpClient getHttpClient() { BasicHttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, REQUEST_TIMEOUT); HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT); HttpClient client = new DefaultHttpClient(httpParams); return client; } //Handler Handler handler = new Handler() { public void handleMessage(Message msg) { switch(msg.what) { case 0: Toast.makeText(getApplicationContext(), "登录成功!", Toast.LENGTH_SHORT).show(); break; case 1: Toast.makeText(getApplicationContext(), "登录失败", Toast.LENGTH_SHORT).show(); break; case 2: Toast.makeText(getApplicationContext(), "URL错误", Toast.LENGTH_SHORT).show(); break; } } }; //LoginThread线程类 class LoginThread implements Runnable { @Override public void run() { String username = inputUsername.getText().toString(); String password = inputPassword.getText().toString(); System.out.println("username="+username+":password="+password); //URL合法,但是这一步并不验证密码是否正确 boolean loginValidate = loginServer(username, password); System.out.println("-----------bool is :"+loginValidate+"----------response:"+responseMsg); Message msg = handler.obtainMessage(); if(loginValidate) { if(responseMsg.equals("true")) { msg.what = 0; handler.sendMessage(msg); }else { msg.what = 1; handler.sendMessage(msg); } }else { msg.what = 2; handler.sendMessage(msg); } } } }整个客户端就一个LogindemoActivity和acticity_login布局文件,注意导入httpClient的方法
导入httpClient
Gradle.script—>build gradle(module:app)
第一步:
在androd{…}里面加上这句:
第二步:如果报错Classnotfound,试试这个加进去把原来的替换掉
implementation \'com.android.support:appcompat-v7:26.0+\'这里是因为你自己使用的不匹配或者不兼容导致的错误