2011-12-26 52 views
0

我試圖進行http - post調用。 服務器,我送他這個要求需要有兩個參數:
1.第一個參數是詮釋 2.第二個參數是枚舉,我送的字符串未能發送http post請求中的兩個參數

我嘗試做兩種方式和他們都失敗:

第一種方式:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
nameValuePairs.add(new BasicNameValuePair("intVal", "-100")); 
nameValuePairs.add(new BasicNameValuePair("enumVal", "enumAsString")); 
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

我得到的酒吧請求「作爲響應。

方式二:

 DefaultHttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost(SERVER_ADDRESS + METHOD_NAME); 
try 
{ 
    HttpParams postParams = new BasicHttpParams(); 
      postParams.setIntParameter("intVal", -100); 
      postParams.setParameter ("enumVal" , "enumAsString"); 

      httppost.setParams(postParams); 

      HttpResponse p = httpclient.execute(httppost); 
     } 
     catch(Exception e) 
     { 

        .... 

     } 

這樣,我又看到了從服務器獲取的參數 - 但第一個參數(即必須是int)是0,第二個參數(需要被枚舉)也是0。

P.S:在服務器上運行的代碼是WCF代碼 - 使用REST。

請..我必須解決這個問題...

謝謝。

+1

什麼是服務器上運行的方式是什麼?你有權訪問它來添加一些調試,看看收到了什麼?如果你不知道你的代碼是否甚至發送了正確的數據,那麼知道發回的內容並沒有什麼特別的用處。 – 2011-12-26 07:43:13

+0

在服務器上運行的代碼是WCF代碼 - 使用REST – Yanshof 2011-12-26 07:45:28

+1

顯示您用於向Web服務器發送請求的完整代碼。 – 2011-12-26 08:07:33

回答

1

這是兩種不同的方式,你可以用HttpPostHttpUrlConnection發送PARAM使用的Andorid:

第一種方式:

HttpClient httpclient; 
HttpPost httppost; 
ArrayList<NameValuePair> postParameters; 
httpclient = new DefaultHttpClient(); 
httppost = new HttpPost("your login link"); 


postParameters = new ArrayList<NameValuePair>(); 
postParameters.add(new BasicNameValuePair("param1", "param1_value")); 
postParameters.add(new BasicNameValuePair("param2", "param2_value")); 

httppost.setEntity(new UrlEncodedFormEntity(postParameters)); 

HttpResponse response = httpclient.execute(httppost); 

第二種方式是:

 String charset = "UTF-8"; 
    String query = String.format("debug_data=%s&" 
          + "client_auth_hash=%s&" 
          + "timestamp=%s&" 
          + "client_api_ver=%s&", 
          URLEncoder.encode("1", charset), 
          URLEncoder.encode(hash, charset), 
          URLEncoder.encode(timeStamp, charset), 
          URLEncoder.encode(clientApiVersion, charset)); 

     System.setProperty("http.keepAlive", "false"); 
     HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); 
     connection.setDoOutput(true); 
     connection.setConnectTimeout(5000); //miliseconds 
     connection.setRequestMethod("POST"); 
     connection.setRequestProperty("Charset", charset); 
     connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset); 
     OutputStream output = null; 
     try { 
      output = connection.getOutputStream(); 
      output.write(query.getBytes(charset)); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      if (output != null) 
       try { 
        output.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
     } 

     int status = ((HttpURLConnection) connection).getResponseCode(); 
     Log.d("", "Status : " + status); 

     for (Entry<String, List<String>> header : connection 
       .getHeaderFields().entrySet()) { 
      Log.d("Headers", 
        "Headers : " + header.getKey() + "=" 
          + header.getValue()); 
     } 

     InputStream response = new BufferedInputStream(connection.getInputStream()); 

     int bytesRead = -1; 
     byte[] buffer = new byte[30 * 1024]; 
     while ((bytesRead = response.read(buffer)) > 0) { 
     //read the response in pieces if it's needed 
      byte[] buffer2 = new byte[bytesRead]; 
     } 
     connection.disconnect(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 

兩種方式都正常工作。

2

這工作在我的情況..試試這個,讓我知道發生什麼事..

List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(); 
nameValuePair.add(new BasicNameValuePair("uname","test")); 
nameValuePair.add(new BasicNameValuePair("pws","test")); 
HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost(url); 
httppost.addHeader("Content-Type", "application/x-www-form-urlencoded"); 
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8)); 

// Execute HTTP Post Request 
HttpResponse response = httpclient.execute(httppost); 
+0

這種方式是行不通的。 – Yanshof 2011-12-26 09:19:30

0

好了,從什麼途徑warking 但是這是我解決這個問題,它的做工精細

JSONObject json = new JSONObject(); 
json.put("intVal", -100); 
json.put("enumVal", enumAsString); 


StringEntity t = new StringEntity(json.toString(), "UTF-8"); 

httppost.setEntity(t); 


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