後臺程序目錄截圖:java
後臺Myservlet.java代碼:android
/** * 本程序用於接收Android端提交的數據,並打印在控制檯上 * @author jiatao * @date 2015-5-3 * @version 1.0 */ package com.jt.appserver; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class Myservlet */ @WebServlet("/Myservlet") public class Myservlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Myservlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub this.doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String name = request.getParameter("name"); String pwd = request.getParameter("pwd"); PrintWriter out = response.getWriter(); out.println("name="+name+" pwd="+pwd);//index.jsp頁面提交的數據打印在瀏覽器中 System.out.println("name="+name+" pwd="+pwd);//將Android端提交的數據打印到控制檯上 } }
前臺Android端activity_get.xml代碼:web
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.jt.http_01.HttpGet" > <LinearLayout android:id="@+id/ll_name" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="帳戶:" /> <EditText android:id="@+id/edt_name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"/> </LinearLayout> <LinearLayout android:id="@+id/ll_pwd" android:layout_below="@+id/ll_name" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密碼:" /> <EditText android:id="@+id/edt_pwd" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1"/> </LinearLayout> <Button android:id="@+id/btn_log" android:layout_below="@+id/ll_pwd" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="登陸"/> </RelativeLayout>
前臺Android端HttpGetOrPost.java代碼:瀏覽器
/** * 經過GET或POST方法,向服務器提交數據的主程序 * @author jiatao * @date 2015-5-3 * @version 1.0 */ package com.jt.http_01; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import android.widget.EditText; public class HttpGetOrPost extends Activity { private EditText edtName, edtPwd; private Button btnLog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 去掉標題欄,必須寫在setContentView()以前 requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_get); initDisplay(); } public void initDisplay() { // TODO Auto-generated method stub edtName = (EditText) findViewById(R.id.edt_name); edtPwd = (EditText) findViewById(R.id.edt_pwd); btnLog = (Button) findViewById(R.id.btn_log); btnLog.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String url = "http://192.168.0.118:8080/web/Myservlet"; new HttpGetOrPostThread(edtName.getText().toString(), edtPwd .getText().toString(), url).start(); } }); } }
前臺Android端HttpGetOrPostThread.java代碼:服務器
/** * 經過GET或POST方法,向服務器提交數據的線程類 * @author jiatao * @date 2015-5-3 * @version 1.0 */ package com.jt.http_01; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import android.util.Log; /** * @author 賈濤 * @date 2015-5-3 下午3:33:23 */ public class HttpGetOrPostThread extends Thread { private String name; private String pwd; private String url; public HttpGetOrPostThread(String name,String pwd,String url){ this.name = name; this.pwd = pwd; this.url = url; } /** * 使用GET方式提交數據 */ private void doGet(){ url = url +"?name="+name+"&pwd="+pwd; try { URL httpUrl = new URL(url); HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection(); conn.setReadTimeout(5000); conn.setRequestMethod("GET"); BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String str; StringBuffer sb = new StringBuffer(); while((str = reader.readLine())!=null){ sb.append(str); } System.out.println("result:"+sb.toString()); Log.e("result:",sb.toString()); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 使用POST方式提交數據 */ private void doPost(){ try { URL httpUrl = new URL(url); HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection(); conn.setRequestMethod("POST"); conn.setReadTimeout(5000); OutputStream out = conn.getOutputStream(); String content = "name="+name+"&pwd="+pwd; out.write(content.getBytes()); BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuffer sb = new StringBuffer(); String str; while((str=reader.readLine())!=null){ sb.append(str); } System.out.println("result:"+sb.toString()); Log.e("result:",sb.toString()); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void run() { // TODO Auto-generated method stub super.run(); // doGet(); doPost(); } }