安卓http提交,get提交,post提交

public static String GetSubmit() {  

        JSONStringer vehicle = null;  

        String resultData = ""; 

        InputStreamReader in = null; 

        HttpURLConnection urlConn = null; 

        BufferedReader buffer = null;

        URL url = null;

        String path="http://XXXXX.com/testWebService3/Main/GetUser";  

        try {

        // 把要传递的参数转化为json,并拼接参数

vehicle = new JSONStringer().object().  

         key("Name").value("jinlong1").key("Age").value(15)   

        .endObject();

path = path+"?user1="+vehicle.toString();

 

    // 参数有汉字的时候要进行URLEncoder编码

vehicle = new JSONStringer().object()

        .key("Name").value(URLEncoder.encode("李金龙2", "utf-8")).key("Age").value(15)   

        .endObject();

path = path+"&user2="+vehicle.toString();

    url = new URL(path); 

    urlConn = (HttpURLConnection) url.openConnection(); 

            

    // 设置提交方式为get

    urlConn.setRequestMethod("GET");

               

   urlConn.setConnectTimeout(3000);// 设置超时时间  

    

   // 读取返回结果

               in = new InputStreamReader(urlConn.getInputStream()); 

               buffer = new BufferedReader(in); 

               String inputLine = null; 

               while ((inputLine = buffer.readLine()) != null) { 

                   resultData += inputLine + " "; 

               } 

               

               // 关闭http链接

               urlConn.disconnect(); 

} catch (Exception e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}  

        return resultData;  

    } 

 

//post提交方式

public static String PostSubmit(String action,List<BasicNameValuePair> list) throws ClientProtocolException, IOException{  

        

        String path="http://XXXXX.com/testWebService3/Main/GetUserInfo";  

        HttpPost httpPost=new HttpPost(path);  

        // 对http的请求头进行设置

        httpPost.setHeader("Accept", "application/json");  

        httpPost.setHeader("Content-type", "application/json");

        JSONStringer vehicle = null;  

       

        

        try {

        //把要传递的参数转化为json

vehicle = new JSONStringer().object().key("user1").object()  

        .key("Name").value("李金龙").key("Age").value(15)   

        .endObject().key("user2").object()  

        .key("Name").value("李金龙2").key("Age").value(16)  

        .endObject().endObject();

} catch (JSONException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}  

        

        StringEntity entity = new StringEntity(vehicle.toString(),HTTP.UTF_8);  //要带utf-8的参数,否则有汉字的时候出错

        

         // 设置参数

        httpPost.setEntity(entity); 

        

        String result="";  

        

        // 执行访问

        HttpResponse response=new DefaultHttpClient().execute(httpPost);  

        

        if(response.getStatusLine().getStatusCode()==HttpURLConnection.HTTP_OK){  

                HttpEntity entity1=response.getEntity();  

                result=EntityUtils.toString(entity1, HTTP.UTF_8);

        }  

        return result;  

      }

原文地址:https://www.cnblogs.com/btbear/p/3321076.html