android联动数据关联,Android前后台联动,通过GET方法提交数据到后台

后台程序目录截图:

cb7c16faf0c4aafb6e0ea45d38a01dd6.png

后台Myservlet.java代码:

/**

* 本程序用于接收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代码:

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" >

android:id="@+id/ll_name"

android:layout_width="fill_parent"

android:layout_height="wrap_content">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="账户:" />

android:id="@+id/edt_name"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"/>

android:id="@+id/ll_pwd"

android:layout_below="@+id/ll_name"

android:layout_width="fill_parent"

android:layout_height="wrap_content">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="密码:" />

android:id="@+id/edt_pwd"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"/>

android:id="@+id/btn_log"

android:layout_below="@+id/ll_pwd"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="登录"/>

前台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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值