Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android學習筆記(六)之post請求

Android學習筆記(六)之post請求

編輯:關於Android編程

下面用兩種方法

第一種:

是類似於直接請求訪問url

代碼如下:

[java] 
package com.jouhu.util; 
 
import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.URL; 
import java.net.URLConnection; 
import java.util.concurrent.CountDownLatch; 
import java.util.concurrent.ExecutorService; 
import java.util.concurrent.Executors; 
 
public class AssignService { 
 
     
    public static String getServiceName(String assignUrl){ 
        String name=null; 
        try { 
            URL url = new URL(assignUrl); 
            URLConnection uc =  url.openConnection(); 
            uc.setConnectTimeout(5000); 
            uc.setReadTimeout(8000); 
            InputStream in = uc.getInputStream(); 
            BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
            name = br.readLine(); 
            br.close(); 
            in.close(); 
        } catch (Exception e) { 
            e.printStackTrace(); 
            return null; 
        } 
        return name; 
    } 
     
    public static void main(String[] arg) throws InterruptedException{ 
        final String URL_ASSIGN = "http://192.168.1.192:8888/ServiceAssign/assignService.action"; 
        int num=500; 
        final CountDownLatch begin = new CountDownLatch(1); 
        final CountDownLatch end = new CountDownLatch(num); 
        final ExecutorService exec = Executors.newFixedThreadPool(num); 
        for (int index = 0; index < num; index++) { 
            Runnable run = new Runnable() { 
                public void run() { 
                    try { 
                        begin.await(); 
                        AssignService.getServiceName(URL_ASSIGN); 
                    } catch (InterruptedException e) { 
                        e.printStackTrace(); 
                    } finally { 
                        end.countDown(); 
                    } 
                } 
            }; 
            exec.submit(run); 
        } 
        System.out.println("Start"); 
        begin.countDown(); 
        end.await(); 
        exec.shutdown(); 
        System.out.println("Over"); 
    } 

第二種:

通過POST將本地數據發送給服務器,string參數表

[java]
/**
 * GpsPoiService.java
 * 版權所有(C) 2012 
 * 創建:cuiran 2012-9-12 下午2:49:25
 */ 
package com.jouhu.service; 
 
import java.io.IOException; 
import java.io.UnsupportedEncodingException; 
import java.net.URLEncoder; 
import java.util.ArrayList; 
import java.util.List; 
 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.protocol.HTTP; 
import org.apache.http.util.EntityUtils; 
 
import android.util.Log; 
 
import com.jouhu.util.AssignService; 
import com.jouhu.util.BaseNameValuePair; 
import com.jouhu.util.Conf; 
 
/**
 * TODO
 * @author cuiran
 * @version TODO
 */ 
public class GpsPoiService { 
     
    private String url="http://219.143.94.189:8405/fastweb/fastweb.do?m=addgps";//服務地址 
     
    public String sendMsg(double x,double y,String address) { 
        //URLEncoder.encode(address, "gbk") 
        String re=""; 
         
        try{ 
            List<NameValuePair> datas = new ArrayList<NameValuePair>(); 
            datas.add(new BaseNameValuePair("x", ""+x)); 
            datas.add(new BaseNameValuePair("y", ""+y)); 
            datas.add(new BaseNameValuePair("address", ""+address)); 
 
            re=sendDataByPost(url, datas); 
//              StringBuffer str = new StringBuffer();           
//              str.append(url) 
//                 .append("&x=") 
//                 .append(x) 
//                  .append("&y=") 
//                 .append(y) 
//                 .append("&address=") 
//                 .append(URLEncoder.encode(address, "gbk")); 
//                  Log.i(Conf.TAG, str.toString()); 
//               re = AssignService.getServiceName(str.toString()); 
                 
        }catch(Exception e){ 
             Log.i(Conf.TAG, "-------"+e.getLocalizedMessage());    
        } 
         
        Log.i(Conf.TAG, re); 
        return re; 
         
    } 
     
    // 通過POST將本地數據發送給服務器,string參數表 
 
    public static String sendDataByPost(String url, List<NameValuePair> datas){ 
 
    org.apache.http.client.HttpClient client = new DefaultHttpClient(); 
 
    HttpPost post = new HttpPost(url); 
 
    HttpResponse resp = null; 
 
    String result = ""; 
 
    // post data 
     
        try { 
     
        post.setEntity(new UrlEncodedFormEntity(datas,HTTP.UTF_8)); 
     
        resp = client.execute(post); 
     
        result = EntityUtils.toString(resp.getEntity()); 
     
        } catch (UnsupportedEncodingException e) { 
     
        e.printStackTrace(); 
     
        } catch (ClientProtocolException e) { 
     
        e.printStackTrace(); 
     
        } catch (IOException e) { 
     
        e.printStackTrace(); 
     
        } 
 
        return result; 
 
    } 
 
     
     

 使用第二種常用的編碼是UTF-8,所以後台服務器也需要用該編碼接收參數。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved