Android客户端和服务器交互
来源:互联网 发布:和明星合影软件 编辑:程序博客网 时间:2024/04/30 18:58
Android客户端和服务器交互实现流程:
(客户端输入的用户名和密码提交到服务器端判断后给出相应的操作。)
客户端代码:
activity_login.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".LoginActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#00f" android:textSize="30px" android:text="Usrname: " /> <EditText android:id="@+id/name" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" > <requestFocus /> </EditText> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#00f" android:textSize="30px" android:text="Password: " /> <EditText android:id="@+id/psd" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputType="textPassword" /> <Button android:id="@+id/login" android:layout_width="102dp" android:layout_height="wrap_content" android:text=" Login " /> <TextView android:id="@+id/sign" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /></LinearLayout>
LoginActivity.java为按钮注册单击事件
login.setOnClickListener(new OnClickListener() {public void onClick(View v) {//单击按钮后发出请求,获取相应结果,并处理。final Handler myHandler =new Handler(){public void handleMessage(Message msg) {String reponse =(String)msg.obj;if(reponse.equals("true")){Intent intent=new Intent(LoginActivity.this,MainActivity.class);startActivity(intent);}else{sign.setText("Username or password incorrect!plz retry.");Toast.makeText(LoginActivity.this,"Username or password incorrect!plz retry.",Toast.LENGTH_LONG);}}};new Thread(new Runnable(){@Overridepublic void run() {LoginToServer logToServer=new LoginToServer();String result=logToServer.doGet(name.getText().toString(), psd.getText().toString());Message msg=new Message();msg.obj=result;//System.out.println(result);myHandler.sendMessage(msg);}}).start();}});
辅助类LoginToServer.java(通过相应方法连接到服务器并返回状态码)
public class LoginToServer {private String url="http://10.0.2.2:8080/zl/LoginServlet";String result="";public String doGet(String name,String psd){String urlStr=url+"?name="+name+"&psd="+psd;HttpClient hc=new DefaultHttpClient();HttpGet hg=new HttpGet(urlStr);try {HttpResponse resp=hc.execute(hg);//System.out.println(resp.getStatusLine().getStatusCode());if(resp.getStatusLine().getStatusCode()==HttpStatus.SC_OK){HttpEntity he=resp.getEntity();InputStream is=he.getContent();BufferedReader br=new BufferedReader(new InputStreamReader(is));String readLine=null;while((readLine=br.readLine())!=null){result +=readLine;}}else{result="error";}} catch (Exception e) {e.printStackTrace();}return result;}
注意 在AVM中localhost(127.0.0.1)代指的是模拟器本身即手机,要访问本机的Tomcat服务器需用android内置的IP:10.0.2.2
需要在manifest.xml中配置允许网络访问的权限
服务器端代码:
public class LoginServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String name=request.getParameter("name");String psd =request.getParameter("psd");response.setContentType("text/html");PrintWriter out = response.getWriter();//out.println(name);//out.println(psd);//这里判断的是固定的用户名和密码,当然可以数据库中读取if(name.equals("vonzhou") && psd.equals("vonzhou")){out.print(true);}else{out.print(false);}out.flush();out.close();}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {this.doGet(request, response);}}
- Android客户端和服务器交互
- Android客户端和服务器数据交互
- android客户端 与服务器交互
- Android 客户端与服务器交互
- 客户端和服务器数据交互
- UDP服务器和客户端交互
- TCP服务器和客户端交互
- 用Android搭建客户端 手机和服务器交互开发实例
- 用Android搭建客户端 手机和服务器交互开发实例
- 用Android搭建客户端 手机和服务器交互开发实例
- Android搭建客户端,实现手机和服务器的交互
- Android之使用HttpPost提交数据到服务器(Android手机客户端和后台服务器交互)
- Android 客户端与服务器交互方式
- Android客户端与服务器数据交互
- Android 客户端与服务器交互方式
- Android 客户端与服务器交互方式
- Android 客户端与服务器交互方式
- Android客户端与服务器交互中的token
- 我们该如何设计数据库(二)
- php文件怎么打开你知道吗?
- jquery常见问题总结
- 真机测试及布署Code Sign error问题总结
- 两张结构一样的表如何找不同的记录:UNION - UNION ALL - INTERSECT - MINUS / EXCEPT
- Android客户端和服务器交互
- 黑马程序员—Java 反射
- 动态规划——最大值
- andoid的智能指针
- Java 查找方法(普通查找,二分查找)
- 我们该如何设计数据库(三)
- 服务员-dp
- 如何避免扩展同名函数或宏
- C# using的用法