1.首先在后台端简单写xml,按着QQ的登录界面即可
2.在Main方法中
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import shangguigu.java.checkloding_fanny_android.R;
import shangguigu.java.checkloding_fanny_android.shangguigu.java.checkloding_domain.shangguigu.java.User;
import shangguigu.java.checkloding_fanny_android.shangguigu.java.checkloding_manager.CheckLoding;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText et_main_username;
private EditText et_main_password;
private Button btn_main_loding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
init();
data();
}
private void data() {
btn_main_loding.setOnClickListener(this);
}
private void init() {
et_main_username = (EditText) findViewById(R.id.et_main_username);
et_main_password = (EditText) findViewById(R.id.et_main_password);
btn_main_loding = (Button) findViewById(R.id.btn_main_loding);
}
@Override
public void onClick(View v) {
if (v == btn_main_loding) {
final String username = et_main_username.getText().toString();
final String password = et_main_password.getText().toString();
if (username.equals("")||password.equals("")) {
Toast.makeText(this, "用户名或密码为空", Toast.LENGTH_SHORT).show();
return;
}
new Thread(new Runnable() {
@Override
public void run() {
User user = CheckLoding.checkLoding(username, password);
if (user!=null) {
Intent intent = new Intent(MainActivity.this,activity_menu.class);
Bundle bundle = new Bundle();
bundle.putSerializable("user",user);
intent.putExtras(bundle);
startActivity(intent);
}else {
Looper.prepare();
Toast.makeText(MainActivity.this,"没有这个值",Toast.LENGTH_SHORT).show();
}
}
}).start();
}
}
}
3.需要创建一个封装类,实现 Serializable接口也只是拥有三个属性,username,password,id4.需要创建一个实现登录的方法,我们把这个方法定义在一个登录的类里
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import shangguigu.java.checkloding_fanny_android.shangguigu.java.checkloding_domain.shangguigu.java.User;
/**
* Created by Dell on 2016/8/9.
*/
public class CheckLoding {
public static User checkLoding (String username,String password) {
User user = null;
try {
URL url = new URL("http://192.168.51.5:8080/student_webforandroid/checkLodingforAndroid");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.connect();
OutputStream os = connection.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject();
jsonObject.put("username",username);
jsonObject.put("password",password);
jsonArray.put(jsonObject);
bw.write(jsonArray.toString());
bw.flush();
bw.close();
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String userjson = br.readLine();
if (!userjson.equals("error")) {
JSONArray userJsonArray = new JSONArray(userjson);
JSONObject userJsonObject = userJsonArray.getJSONObject(0);
user = new User();
user.setId(userJsonObject.getInt("id"));
user.setUsername(userJsonObject.getString("username"));
user.setPassword(userJsonObject.getString("password"));
}
br.close();
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return user;
}
}
与web后台相连,因此需要一个servlet
<pre name="code" class="java">import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.fanny.domain.User;
import com.fanny.managrimpl.StudentImpl;
import com.fanny.managrimpl.StudentManager;
public class checkLodingforAndroid extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
InputStream is = request.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String json = br.readLine();
// System.out.println(json);
try {
JSONArray jsonArray = new JSONArray(json);
JSONObject jsonObject =jsonArray.getJSONObject(0);
String username = jsonObject.getString("username");
String password = jsonObject.getString("password");
User user = null;
StudentManager sm = new StudentImpl();
user = sm.CheckLoging(username, password);
System.out.println(user);
if (user!=null) {
JSONArray jsonArray2 = new JSONArray();
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("username", username);
jsonObject2.put("password", password);
jsonObject2.put("id", user.getId());
jsonArray2.put(jsonObject2);
//传送给安卓
OutputStream os = response.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write(jsonArray2.toString());
bw.flush();
bw.close();
} else {
OutputStream os = response.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
bw.write("error");
bw.flush();
bw.close();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
登录成功后会需要显示,所以要新建一个activity,进行带参传递
</pre><pre code_snippet_id="1822449" snippet_file_name="blog_20160811_6_3116162" name="code" class="java">import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;
import shangguigu.java.checkloding_fanny_android.R;
import shangguigu.java.checkloding_fanny_android.shangguigu.java.checkloding_domain.shangguigu.java.User;
public class Result extends AppCompatActivity {
private TextView tv_id;
private TextView tv_username;
private TextView tv_password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_result);
init();
indata();
}
private void indata() {
User user = (User) getIntent().getSerializableExtra("user");
if (user!=null) {
int id = user.getId();
String username = user.getUsername();
String password = user.getPassword();
tv_id.setText(id+"");
tv_username.setText(username);
tv_password.setText(password);
}
}
private void init() {
tv_id = (TextView) findViewById(R.id.tv_id);
tv_username = (TextView) findViewById(R.id.tv_username);
tv_password = (TextView) findViewById(R.id.tv_password);
}
}
安卓端与servlet的数据交互(HttpURLConnection)方式
字符流字节流的转换
Json的生成与解析
Activity的带参跳转
URL url = newURL("http://192.168.56.1:8080/LoginDemo/cn.edu.hpu.android/CheckAndroidLogin");
//建立Httpconnect并设置相关属性
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();//建立对象
httpURLConnection.setDoOutput(true);//可以输出
httpURLConnection.setDoInput(true);//允许输入
httpURLConnection.setUseCaches(false);//不使用缓存
httpURLConnection.setRequestMethod("POST");//使用post传输
httpURLConnection.connect();//连接
OutputStream os = httpURLConnection.getOutputStream();//得到连接的输出流
OutputStreamWriter osw = newOutputStreamWriter(os); );//转换成字符流
BufferedWriter bw = newBufferedWriter(osw);//批量传输
InputStream is =httpURLConnection.getInputStream();//得到输入流
InputStreamReader isr = newInputStreamReader(is);//转换成字符流
BufferedReader br = newBufferedReader(isr);//批量读取
bw.write();//写入数据
br.readLine();//读取数据
生成
JSONArray jsonArray = newJSONArray();
JSONObjectobject = newJSONObject();
object.put("username",username);
object.put("password",password);
jsonArray.put(object);
解析
JSONArray userjsonarr = newJSONArray(userjson);
JSONObject userobj = userjsonarr.getJSONObject(0);
user = newUser();
user.setId(userobj.getInt("id"));
user.setUsername(userobj.getString("username"));
user.setPassword(userobj.getString("password"));
Activity之间的带参跳
第一种方法
Intentintent = newIntent(MainActivity.this,ResultActivity.class);
intent.putExtra(“id”user.getId());
intent.putExtra(“username”,user.getUsername());
intent.putExtra(“id”,user.getPassword());
startActivity(intent);
第二种方法
Intentintent = newIntent(MainActivity.this,ResultActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable(“user”,user);
intent.putExtras(bundle);
startActivity(intent);