1.AndroidManifest.xml添加网络访问权限
<uses-permission android:name="android.permission.INTERNET"/>
2.在web项目中新建servlet
package cn.com.servlet;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//服务器接受统一编码iso-8859-1,然后获取字符串时转化为utf-8
String name = new String(request.getParameter("name").getBytes("iso-8859-1"),"utf-8");
String password = request.getParameter("password");
System.out.println("name:"+name);
System.out.println("password:"+password);
if(name.equals("张三")&&password.equals("123")){
//往浏览器写字符串
response.getOutputStream().write("HttpClienget方式登录成功".getBytes("utf-8")); //写到浏览器的编码是utf-8
}
else{
response.getOutputStream().write("HttpClienget方式登录失败".getBytes("utf-8")); //写到浏览器的编码是utf-8
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//doGet(request, response);
//服务器接受统一编码iso-8859-1,然后获取字符串时转化为utf-8
String name = new String(request.getParameter("name").getBytes("iso-8859-1"),"utf-8");
String password = request.getParameter("password");
System.out.println("name:"+name);
System.out.println("password:"+password);
if(name.equals("张三")&&password.equals("123")){
//往浏览器写字符串
response.getOutputStream().write("HttpClienpost方式登录成功".getBytes("utf-8")); //写到浏览器的编码是utf-8
}
else{
response.getOutputStream().write("HttpClientpost方式登录失败".getBytes("utf-8")); //写到浏览器的编码是utf-8
}
}
}
3.文件布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="输入用户名"
android:text="张三"
/>
<EditText
android:id="@+id/password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="输入密码" />
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ClientGet提交"
android:onClick="onClick3"
/>
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ClientPost提交"
android:onClick="onClick4"
/>
</LinearLayout>
4.MainActivity.java
package com.example.get;
import java.io.UnsupportedEncodingException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText name,password;
private final int SUCCESS=1;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what==SUCCESS){
String result = (String) msg.obj;
if(result!=null){
Toast.makeText(MainActivity.this, result, 1).show();
}else{
Toast.makeText(MainActivity.this, "登陆失败", 1).show();
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText) findViewById(R.id.name);
password = (EditText) findViewById(R.id.password);
}
/**
* clientget提交
* @param view
*/
public void onClick3(View view){
final String loginName = name.getText().toString();
final String loginPasswor = password.getText().toString();
new Thread(){
@Override
public void run() {
final String result=LoginService.loginClientGet(loginName, loginPasswor);
if(result!=null){
//在主线程上运行
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, result, 1).show();
}
});
}
}
}.start();
}
public void onCli
补充:移动开发 , Android ,