Android网络存储

创建两个项目

  1. Android项目: 用于发送数据到服务器
  2. web项目: 用于构建服务器端接收数据

代码

  • 用于向服务器发送数据在这里插入代码片
import android.util.Log;
import com.android.demo_filestream.studentdao.Student;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class HttpSave{
    String path = Constant.SERVEURL+"Servlet";
    public  HttpSave(Student student) throws Exception {
        Map<String, String> params = new HashMap<String, String>();
        params.put("stuId", student.getStuId());
        params.put("stuName", student.getStuName());
        params.put("stuSex", student.getStuName());
        params.put("stuAge", student.getStuName());
        params.put("stuHome", student.getStuName());
        params.put("stuDept", student.getStuName());
//        String key = null;
//        String value = null;
//        // 获取键值对的迭代器
//        Iterator it = params.entrySet().iterator();
//        while (it.hasNext()) {
//            Map.Entry entry = (Map.Entry) it.next();
//            key = (String) entry.getKey();
//            value = (String) entry.getValue();
//            System.out.println("key:" + key + "---" + "value:" + value);
//        }
          sendGETRequest(path, params, "UTF-8");
    }
//    @Override
//    public void run() {
//        try {
//            URL url=new URL(path);
//            Log.d("filename",name);
//            HttpURLConnection connection= (HttpURLConnection) new  URL(path).openConnection();
//            Log.d("path",path);
//            connection.setRequestMethod("GET");
//            connection.setConnectTimeout(50000);
//            connection.setReadTimeout(30000);
//            if (connection.getResponseCode() != 200) {
//                System.out.println("请求url失败");
//            }else System.out.println("success");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
        private static boolean sendGETRequest(String path, Map<String, String> params, String ecoding) throws Exception{
            //拼接数据到URI
            int flag=0;
            final StringBuilder url = new StringBuilder(path);
            url.append("?");
            for(Map.Entry<String, String> entry : params.entrySet()){
                url.append(entry.getKey()).append("=");
                url.append(URLEncoder.encode(entry.getValue(), ecoding));
                url.append("&");
            }
            url.deleteCharAt(url.length() - 1);
            System.out.println(url);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        HttpURLConnection conn = (HttpURLConnection)new URL(url.toString()).openConnection();
                        conn.setConnectTimeout(50000);
                        conn.setReadTimeout(30000);
                        conn.setRequestMethod("GET");
                        if(conn.getResponseCode()==200){
                            System.out.println("发送成功");
                        }else {
                            System.out.println("发送失败");
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
            return true;
        }
}


package com.idea.xxx;

import java.io.IOException;

import javax.security.auth.message.callback.PrivateKeyCallback.Request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.idea.S_T.Student;
import com.idea.daoimp.Studentimpl;

@WebServlet("/Servlet")
public class Servlet extends HttpServlet{
    public Servlet() {
		// TODO Auto-generated constructor stub
    	super();
	}
    @Override
    public void destroy() {
    	// TODO Auto-generated method stub
    	super.destroy();
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    	// TODO Auto-generated method stub
    	Student student=new Student();
        student.setStuId(req.getParameter("stuId"));
        student.setStuName(req.getParameter("stuName"));
        student.setStuSex(req.getParameter("男"));
        //student.setStuAge(Integer.parseInt(req.getParameter("stuAge")));
        student.setStuAge(22);
        student.setStuHome(req.getParameter("stuHome"));
        student.setStuDept(req.getParameter("stuDept"));
        Studentimpl stu=new Studentimpl();
        stu.save(student);
    	String stuId=req.getParameter("stuId");
    	System.out.println(stuId);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    	// TODO Auto-generated method stub
    	super.doPost(req, resp);
    }
    @Override
    public void init() throws ServletException {
    	// TODO Auto-generated method stub
    	super.init();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值