2011-08-17 72 views
5

我目前正在一個應用程序.. 在該應用程序中我必須使用get和post兩種方法.. get方法正常工作,但在post方法突然我得到的響應像如何設置標題使用http post方法

"invalid method.post required".. 

我的代碼是:

 String list_url = "************************"; 
    try { 

     String appand = TimeStampForAuth.TimeStameMethod(main.this); 
     String auth = android.util.Base64.encodeToString(("iphone" + ":" + 
      "2a5a262d5a") 
     .getBytes("UTF-8"), android.util.Base64.NO_WRAP); 

     DefaultHttpClient client = new DefaultHttpClient(); 
     HttpPost post = new HttpPost(list_url+appand); 

     Log.d("URL_LIST", list_url+appand); 

     List nvps = new ArrayList(); 
     nvps.add(new BasicNameValuePair("login",Login)); 
     nvps.add(new BasicNameValuePair("password",password)); 
     nvps.add(new BasicNameValuePair("title",title)); 
     nvps.add(new BasicNameValuePair("category_id",cat_id)); 




     UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps,HTTP.UTF_8); 
     post.addHeader("Authorization", "Basic " + auth); 
     post.setEntity(p_entity); 
     HttpResponse response = client.execute(post); 
     HttpEntity responseEntity = response.getEntity(); 
     String s = EntityUtils.toString(responseEntity); 
     Log.d("List response", s); 

    } 
    catch(Exception e){ 
     System.out.println(e.toString()); 
    } 

我失蹤somethinf或者是什麼,我不know..is所有的東西是有效的POST方法....

請幫助我儘早...

感謝你.........

+0

會不會是服務器端的問題?已經嘗試用RESTClient-Addon啓動請求? – Tima 2011-08-17 11:57:46

+0

有沒有問題從serverside..serverside請求發送作爲獲取請求使用此方法..是否有可能..請幫助我..我不知道這個代碼出了什麼問題.. – Kutbi 2011-08-18 05:12:06

回答

1

嘗試使用setHeader()代替addHeader()

1

你可以嘗試的HttpClient 4.1 Android版:http://code.google.com/p/httpclientandroidlib/ (Android使用的HttpClient版本4.0中存在錯誤。) 您也可以使用httpClient.log.enableDebug(true)更多地調試它。

如果你不想使用外部庫,我會開始調試網絡流量。 使用名爲Wireshark的軟件,您可以輕鬆查看並檢查所有Http請求/響應。

若需授權我個人會用:

httpClient.getCredentialsProvider().setCredentials(
    new AuthScope(hostname, port), 
    new UsernamePasswordCredentials(user, pass)); 
-1

用這個代替:

HttpURLConnection connection = (HttpsURLConnection) serviceURL.openConnection(); 
    connection.setRequestMethod("POST");