程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> 數據-小白求助,很迷茫Android中寫post請求,這個應該怎麼寫那

數據-小白求助,很迷茫Android中寫post請求,這個應該怎麼寫那

編輯:編程綜合問答
小白求助,很迷茫Android中寫post請求,這個應該怎麼寫那

如圖,他們提供的數據接口是這樣說的,圖片說明
於是我就這麼寫的,不對啊,該怎麼寫那圖片說明

最佳回答:


給你分享一個我的android訪問http的類,超級好用,從來沒讓我擔心過

 public class MyHttpClient {
    /** 
     * 通過HttpClient發送GET請求 
     * @param path 請求路徑 
     * @param params 請求參數 
     * @param ecoding 請求編碼 
     * @return 請求是否成功 
     */  
    public HttpResponse sendHttpClientGETRequest(String path,Map<String, String> params, String ecoding) throws Exception {  
        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);  
        HttpGet httpGet=new HttpGet(url.toString());  
        DefaultHttpClient client=new DefaultHttpClient();  
        client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
        client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);
        HttpResponse response=client.execute(httpGet); 
        if(response.getStatusLine().getStatusCode()==200){  
            return response;  
        }  
        return null;  
    } 

        /** 
    * 通過HttpClient發送Post請求 
    * @param path 請求路徑 
    * @param params 請求參數 
    * @param ecoding 請求編碼 
    * @return 請求是否成功 
    */  
    public HttpResponse sendHttpClientPOSTRequest(String path,  
           Map<String, String> params, String ecoding) throws Exception {  
       List<NameValuePair> pair=new ArrayList<NameValuePair>();//存放請求參數  
       if(params!=null && !params.isEmpty()){  
           for (Map.Entry<String, String> entry : params.entrySet()) {  
               pair.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));  
           }  
       }  
       UrlEncodedFormEntity enFormEntity=new UrlEncodedFormEntity(pair,ecoding);  
       HttpPost httpPost=new HttpPost(path);  
       httpPost.setEntity(enFormEntity);  
       DefaultHttpClient client=new DefaultHttpClient();  
       client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);
       client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);
       HttpResponse response=client.execute(httpPost);  
       if(response.getStatusLine().getStatusCode()==200){  
           return response;  
       } 
       return null;  
    }

    /** 
    * 通過HttpClient發送Post請求 
    * @param path 請求路徑 
    * @param params 請求參數 
    * @param ecoding 請求編碼 
    * @return 請求是否成功 
    */  
    public HttpResponse sendHttpClientPOSTRequest(String path,  
           Map<String, String> params, String ecoding, int timeout) throws Exception {  
       List<NameValuePair> pair=new ArrayList<NameValuePair>();//存放請求參數  
       if(params!=null && !params.isEmpty()){  
           for (Map.Entry<String, String> entry : params.entrySet()) {  
               pair.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));  
           }  
       }  
       UrlEncodedFormEntity enFormEntity=new UrlEncodedFormEntity(pair,ecoding);  
       HttpPost httpPost=new HttpPost(path);  
       httpPost.setEntity(enFormEntity);  
       DefaultHttpClient client=new DefaultHttpClient();  
       client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
       client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
       HttpResponse response=client.execute(httpPost);  
       if(response.getStatusLine().getStatusCode()==200){  
           return response;  
       } 
       return null;  
    }
    }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved