`

get

 
阅读更多



  服务端接收客服端数据,通过http协议

之前的两个例子都是客服端解析服务端的数据,然而在实际应用中,客服端发送数据给服务端也是一个很重要的方面。比如客服端输入用户名和密码后,要给服务端接收,最后在数据库里面去查找看是否有这个用户
  本文以get方式发送数据给服务端

 


  服务器端

 

   就是一个Servlet

 

 

package com.lin.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 /*
  * 1、get方式提交请求参数,没有对中文参数编码
  * tomcat默认iso8859编码,转成二进制数据
  */
public class ManageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
    public ManageServlet() {
       
    }

 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String title= request.getParameter("title");
		String timelength= request.getParameter("length");
		/*
		 * 保存到数据库中
		 */
		 System.out.println("视频"+title);
		 System.out.println("时长"+timelength);
	}

	 
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		doGet(request, response);
	}

}

 

 

   在浏览器里输入:http://localhost:8080/videonews/ManageServlet?title=woshizhu&length=23

Servlet会打印

视频woshizhu
时长23

那么,如何在android客服端完成这个效果呢

也就是说,如何在手机上自己输入两项参数,然后手机的数据就会被Servlet接收呢?

好,我们新建一个android工程

layout.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/title" />

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/title"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/timelength" />

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/timelength"
android:numeric="integer"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="@string/button"
/>
</LinearLayout>

 

Activity

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.lin.servce.NewsService;
 
public class NewsmanageActivity extends Activity {
    /** Called when the activity is first created. */
	public EditText etitle;
	public EditText etimelength;
	public Button button;
	boolean result;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
       button.setOnClickListener(new View.OnClickListener() {
		
		public void onClick(View v) {
			// TODO Auto-generated method stub
			new Thread(new Runnable() {
				
				public void run() {
					// TODO Auto-generated method stub
					String title=etitle.getText().toString();
			    	String length=etimelength.getText().toString();
			    	  result=NewsService.save(title,length);
					 Message msg = new Message();
                     msg.what = 1;
                      handler.sendMessage(msg);  
				}
			}){}.start();
		}
	});
    }
 Handler handler=new Handler(){
    	
    	public void handleMessage(Message msg) {
    		  if (msg.what == 1) {
    			  if(result){
   		    		Toast.makeText(getApplicationContext(), R.string.success, 2).show();
    		    	}else{
    		    		Toast.makeText(getApplicationContext(), R.string.error, 2).show();
    		
    		    	}
	            }
    	};
    	
    };
    private void init() {
		// TODO Auto-generated method stub
		etitle=(EditText) findViewById(R.id.title);
		etimelength=(EditText) findViewById(R.id.timelength);
		button=(Button) findViewById(R.id.button);
		
	}
}

 

save方法两个参数为title和length

 

package com.lin.servce;

import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class NewsService {
//通过http把数据发到web应用里,服务器对路径长度的限制get post
	public static boolean save(String title, String length) {
 		String path="http://192.168.189.1:8080/videonews/ManageServlet";
 		Map<String,String> params=new HashMap<String,String>();
		params.put("title", title);
		params.put("length", length);
		 
//保存到map集合
		try {
			return sendGETRequest(path,params,"UTF-8");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

//得到路径
private static boolean sendGETRequest(String path, Map<String, String> params, String encoding)throws Exception {
	// TODO Auto-generated method stub
	StringBuilder builder=new StringBuilder(path);
	builder.append('?');
	for(Map.Entry<String, String> entry: params.entrySet()){
		builder.append(entry.getKey()).append("=");
		builder.append(URLEncoder.encode(entry.getValue(),encoding));
		builder.append("&");//多个这个没有影响
	}
	builder.deleteCharAt(builder.length()-1);//最后一个字符的索引
	System.out.println("url"+builder);
	HttpURLConnection conn=(HttpURLConnection) new URL(builder.toString()).openConnection();
	conn.setConnectTimeout(5000);
	conn.setRequestMethod("GET");//一定要大写
	if(conn.getResponseCode()==200){
		return true;
	}
	return false;
}

 
}

 

 

 

 

  • 大小: 49.1 KB
  • 大小: 1.1 KB
分享到:
评论

相关推荐

    GetIP PB获取IP的DLL

    Function uLong Gethost(ref string hostname) Library "GetIP60.dll" ALIAS FOR "Gethost;ansi" 调用方法 string ls_Host = space(256) int result result = Gethost(ls_Host) if result =0 then sle_1.text=...

    C++ 实现 HTTP HTTPS POST GET(包含curl版本和winhttp两种实现)

    C++ 实现 HTTP HTTPS POST GET(包含curl版本和winhttp两种实现)。 玩过抓包,网络协议分析的朋友肯定都知道http https post get,web端和用户的交互主要是通过post get完成的。 我这里有两种实现: 1:libcurl实现的...

    对Django 中request.get和request.post的区别详解

    Django 中request.get和request.post的区别 POST和GET差异: POST和GET是HTTP协议定义的与服务器交互的方法。GET一般用于获取/查询资源信息,而POST一般用于更新资源信息。另外,还有PUT和DELETE方法。 POST和GET都...

    Advanced GET EOD.v9.1.0.19.rar

    Advanced GET — 波浪分析工具 综观全世界股市的庄家无论是调研分析或操盘技术都是精益求精,甚至标准到令人目瞪口呆的地步,他们作为市场主力使用Advanced GET高级数学模型客观智能数浪,严格按照有关的技术和设定...

    Advanced GET EOD.v9.1.0.20.7z

    Advanced GET Advanced GET — 波浪分析工具 综观全世界股市的庄家无论是调研分析或操盘技术都是精益求精,甚至标准到令人目瞪口呆的地步,他们作为市场主力使用Advanced GET高级数学模型客观智能数浪,严格按照...

    getdata225软件

    getdata软件适用于从图片中获取数据,是科研的必备软件。 想引用别人论文中的某个数据(曲线)图,但论文中没有这个图的数据,直接把图抓过来显得太逊了,希望提取出这个图中的数据信息生成矢量图; 希望从这个图中...

    基于Labview的HTTP的GET与POST请求示例

    ... web 浏览器可能是客户端,而计算机上的网络应用程序也可能...在客户机和服务器之间进行请求-响应时,两种最常被用到的方法是:GET 和 POST。 GET - 从指定的资源请求数据。 POST - 向指定的资源提交要被处理的数据

    Winget 离线安装包 LTSC Server Winget

    包含Winget需要的所有文件,可以用于LTSC和Server系统的Winget安装!!!!!!!

    Api接口调用封装,实现POSt,GET等数据请求

    Api接口调用封装,实现POSt,GET等数据请求,Api接口调用封装,实现POSt,GET等数据请求,Api接口调用封装,实现POSt,GET等数据请求,Api接口调用封装,实现POSt,GET等数据请求,Api接口调用封装,实现POSt,GET等数据请求,...

    HTTP HTTPS POST GET(包含curl版本和winhttp两种实现)

    玩过抓包,网络协议分析的朋友肯定都知道http https post get,web端和用户的交互主要是通过post get完成的。 我这里有两种实现: 1:libcurl实现的CHttpClient类,该类实现了Htpp和Https的get post方法。 2:...

    SAP FRC接口调用calling RFC_METADATA_GET -- see log for details报错解决方案

    SAP.Middleware.Connector.RfcCommunicationException:“destination XXXX failed when calling RFC_METADATA_GET -- see log for details” 猜测的原因: 老的DLL库在获取接口实例时,会触发“RFC_METADATA_GET”...

    getaddrinfo.c 内部代码

    getaddrinfo.c 内部代码 getaddrinfo.c 内部代码 getaddrinfo.c 内部代码 getaddrinfo.c 内部代码

    Get.Smart.Season 1.srt

    Filename.....: Get.Smart.1965.S01E30.The.Last.One.In.Is.A.Rotten.Spy.avi Filesize.....: 183,820,288 bytes Runtime......: 25:25.762 (36582 frames) Video Codec..: XviD 1.1.2 Final (B-VOP//) Video Bit...

    批量getshell工具

    2017最新版的批量getshell工具,最全的cms识别系统,最快的getshell行动,各种cms,各种漏洞,各种getshell,你值得拥有!

    vue项目启动出现cannot GET /服务错误的解决方法

    出现 Cannot GET/: 控制台中并没有报错;npm run dev命令行窗口也没有报错。 原因 在网上查了一堆,发现这个问题还挺多呢,而且各个回答的解决方式都竟然有许多不同… 于是把能改的地方都改了…(基本上相当...

    西门子S7-1200的putget操作

    西门子S7-1200互相间的put/get操作 包括硬件配置 软件 DB putget设置

    利用getdata获取图形数据

    利用getdata获取图形数据

    c# 运用get/post 和使用webservice

    c# 运用get/post 和使用webservicec# 运用get/post 和使用webservicec# 运用get/post 和使用webservicec# 运用get/post 和使用webservice

    get-pip.py

    $ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py # 下载安装脚本 $ sudo python get-pip.py # 运行安装脚本

    GO接收GET/POST参数及发送GET/POST请求的实例详解

    Golang: 接收GET和POST参数 GET 和 POST 是我们最常用的两种请求方式,今天讲一讲如何在 golang 服务中,正确接收这两种请求的参数信息。 处理GET请求 1.1 接收GET请求 //接收GET请求 func Get(writer ...

Global site tag (gtag.js) - Google Analytics