2014-01-24 165 views
1

當我通過下面的方法執行一個API時,我總是得到404作爲響應代碼。HTTP POST方法返回狀態碼404

private void execute() throws IllegalStateException, IOException, NoSuchAlgorithmException { 

    Map<String, String> comment = new HashMap<String, String>(); 
    comment.put("accounts-groups", "customers/enterprise"); 
    comment.put("companyType", "customer"); 
    comment.put("companyName", "Test"); 
    String json = new GsonBuilder().create().toJson(comment, Map.class); 
    Log.i(TAG, "json : "+json); 

    HttpResponse response = makeRequest(URL, json); 

    /*Checking response */ 
    if(response != null) { 
     InputStream inputStream = response.getEntity().getContent(); //Get the data in the entity 
     int statusCode = response.getStatusLine().getStatusCode(); 
     Log.i(TAG, "statusCode : "+statusCode); 
     String result; 
     // convert inputstream to string 
     if(inputStream != null) 
      result = convertStreamToString(inputStream); 
     else 
      result = "Did not work!"; 

     Log.i(TAG, "result : "+result); 
    } 
} 

private HttpResponse makeRequest(String uri, String json) throws NoSuchAlgorithmException { 
    Log.i(TAG, "uri : "+uri); 
    try { 
     HttpPost httpPost = new HttpPost(uri); 
     httpPost.setEntity(new StringEntity(json, HTTP.UTF_8)); 

     long timestamp = System.currentTimeMillis(); 

     String signatureKey = PRIVATE_KEY + timestamp; 

     byte[] bytesOfMessage = signatureKey.getBytes(HTTP.UTF_8); 

     MessageDigest md = MessageDigest.getInstance("MD5"); 
     byte[] thedigest = md.digest(bytesOfMessage); 
     char[] signature = Hex.encodeHex(thedigest); 

     String finalSignature = String.valueOf(signature); 

     Log.i(TAG, "finalSignature : "+finalSignature); 

     httpPost.setHeader("Timestamp", ""+timestamp); 
     httpPost.setHeader("Api_token", API_TOKEN); 
     httpPost.setHeader("Signature" , finalSignature); 

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

     return new DefaultHttpClient().execute(httpPost); 
    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

我不明白我錯了什麼地方。任何人都可以幫助我嗎?

回答

3

從維基:

404或Not Found錯誤消息是指示客戶端能夠與服務器, 通信,但服務器找不到請求什麼是HTTP標準響應代碼 。

所以,你的代碼是好的,但服務器找不到你正在尋找的資源。仔細檢查您的網址是否正確。


如何通過提琴手代理傳遞請求調試目的:

HttpParams params = new BasicHttpParams(); 

    // .... 

    HttpHost proxy = new HttpHost("192.168.1.12", 8888); // IP to your PC with fiddler proxy 
    params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); 

    // use params as a second parameter to: following constructor: 
    // public DefaultHttpClient (ClientConnectionManager conman, HttpParams params) 
+0

網址是正確的。他們在頂點代碼中使用的相同url並且工作正常。不知道java代碼中有什麼問題。 – Braj

+0

我會在窗口上使用fiddler來調試這個請求,這將顯示服務器的確切內容,以及返回的內容 - 這也允許保存此類請求並在桌面上進行測試。 – marcinj

+0

HTTPHost的第一個參數是我的系統IP地址,可以告訴我什麼是「params」n如何獲取它?我在ubuntu上 – Braj