博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android的GET请求失败,一直无法发送
阅读量:4229 次
发布时间:2019-05-26

本文共 1937 字,大约阅读时间需要 6 分钟。

今天做安卓的时候,需要连接服务器,因为数据传的比较少 ,所以不想用post请求,用的get请求,但是一直无法成功,不得其原因,后来才知道get请求的URL中不能包含空格,

下面是post请求的代码,无注解,要详细看的,请看我以前的文章,网络请求必须开辟新的线程

private void sendRequest(final String sign){    new Thread(new Runnable() {        @Override        public void run() {            String url="http://"+ MyIp.ip+":8080/CarSafe/InsertBlogServlet";            HttpURLConnection connection=null;            try{                URL posturl=new URL(url);                connection=(HttpURLConnection)posturl.openConnection();                connection.setDoOutput(true);                connection.setDoInput(true);                connection.setRequestMethod("POST");                connection.setUseCaches(false);                connection.setInstanceFollowRedirects(true);                connection.connect();                DataOutputStream out=new DataOutputStream(connection.getOutputStream());                String content="car_id="+ URLEncoder.encode(String.format("%d",Person.car_id),"utf-8")+                        "&username="+URLEncoder.encode(Person.usename,"utf-8")+"&mysign="+URLEncoder.encode(sign,"utf-8");                out.writeBytes(content);                out.flush();                out.close();                BufferedReader reader=new BufferedReader(new InputStreamReader(connection.getInputStream()));                String line;                StringBuffer response=new StringBuffer();                while((line=reader.readLine())!=null){                    response.append(line);                }                reader.close();                Message message=new Message();                message.what=SEND_SIGN;                message.obj=response.toString();                handler.sendMessage(message);            }catch (Exception e){                e.printStackTrace();            }finally {                if(connection!=null){                    connection.disconnect();                }            }        }    }).start();}

转载地址:http://spjqi.baihongyu.com/

你可能感兴趣的文章
MySQL的基本管理
查看>>
MySQL 表结构与键值
查看>>
MySQL存储引擎,表记录管理
查看>>
Mysql多表查询语句,授权用户与密码更改
查看>>
MySQL 备份与恢复
查看>>
采用IC传感器的相对湿度测量
查看>>
皮带传动实验
查看>>
微软的人才观
查看>>
Blizzard的MPQ文件格式搜索算法
查看>>
你的变量究竟存储在什么地方?
查看>>
s3c2410 中断异常处理(转)
查看>>
对张孝祥C语言试题其中一题的探讨 (转载)
查看>>
一些好的网站
查看>>
WinCE中的虚拟地址和实际的物理地址是如何对应
查看>>
Microsoft Windows CE 的内存使用
查看>>
makefile入门
查看>>
中科院计算所Goddon CPU诞生历史!牛!
查看>>
ispPAC
查看>>
为人处世小技巧
查看>>
结构体知识汇总
查看>>