服务端接收客服端数据,通过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
扫描二维码关注公众号,回复:
495390 查看本文章
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>
<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; } }