Android GET,POST向服务器端发送数据(发送)
Android GET,POST向服务器端发送数据(发送)
//目录结构
//strings.xml字符常量文件
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="title">通过Get和Post两种方式分别提交数据到服务器</string>
- <string name="app_name">GetAndPostRequest</string>
- <string name="book_name">书本名称</string>
- <string name="book_price">书本价格</string>
- <string name="success">提交成功</string>
- <string name="error">提交失败</string>
- <string name="get_request">Get请求提交</string>
- <string name="post_request">Post请求提交</string>
- </resources>
- <?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" />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/book_name"
- />
- <EditText
- android:id="@+id/book_name"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/book_price"
- />
- <EditText
- android:id="@+id/book_price"
- android:numeric="integer"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- >
- <Button
- android:id="@+id/get_reqeust"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/get_request"
- />
- <Button
- android:id="@+id/post_reqeust"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/post_request"
- />
- </LinearLayout>
- </LinearLayout>
- package sn.len.request;
- import java.io.OutputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.net.URLEncoder;
- import java.util.Map;
- public class RequestService
- {
- //get请求,有文件长度大小限制
- public static boolean getRequest(String urlPath) throws Exception
- {
- URL url=new URL(urlPath);
- HttpURLConnection con=(HttpURLConnection)url.openConnection();
- con.setRequestMethod("GET");
- con.setReadTimeout(5*1000);
- if(con.getResponseCode()==200)
- {
- return true;
- }
- return false;
- }
- //post请求,无文件长度大小限制
- public static boolean postRequest(String urlPath,Map<String,String> map) throws Exception
- {
- StringBuilder builder=new StringBuilder(); //拼接字符
- //拿出键值
- if(map!=null && !map.isEmpty())
- {
- for(Map.Entry<String, String> param:map.entrySet())
- {
- builder.append(param.getKey()).append('=').append(URLEncoder.encode(param.getValue(), "utf-8")).append('&');
- }
- builder.deleteCharAt(builder.length()-1);
- }
- //下面的Content-Length: 是这个URL的二进制数据长度
- byte b[]=builder.toString().getBytes();
- URL url=new URL(urlPath);
- HttpURLConnection con=(HttpURLConnection)url.openConnection();
- con.setRequestMethod("POST");
- con.setReadTimeout(5*1000);
- con.setDoOutput(true);//打开向外输出
- con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//内容类型
- con.setRequestProperty("Content-Length",String.valueOf(b.length));//长度
- OutputStream outStream=con.getOutputStream();
- outStream.write(b);//写入数据
- outStream.flush();//刷新内存
- outStream.close();
- //状态码是不成功
- if(con.getResponseCode()==200)
- {
- return true;
- }
- return false;
- }
- }
|
评论暂时关闭