2014-09-03 87 views
0

我正在開發一個Android應用程序,通過新的HttpPost(url)向服務器發送數據(託管在hostinger.es中 - 免費測試應用程序); howevere,當我在我的手機上運行應用程序,我總是在我的日誌如下回答:Android httpPost(URL) - > 403 Forbidden

09-03 15:24:40.916: response: 
<html> 
<head> 
<title>Error 403 - Forbidden</title> 
<meta http-equiv="Refresh" content="0;url=http://www.hostinger.co/error_404?" /> 
</head> 
<body> 
</body> 
</html> 

我的服務器代碼的PHP/MySQL。我已經更改了我的文件夾和文件.php的FileZilla中的權限,但仍然得到相同的答案。

我的客戶的規範要求:

private static final String url = "http://yyy.esy.es/XXXX/process.php/senddata/"; 
// yyy subdomain in hostinger 
// XXXX the folder where my php files are 

protected void sendDataToServer(final TestData data) { 
    ArrayList<NameValuePair> keyValuePairs = new ArrayList<NameValuePair>(); 
    keyValuePairs.add(new KeyValuePair(Constant.FB_ID, data.getId())); 

    .... 
    ResponseHandler<String> res = new BasicResponseHandler(); 
    HttpPost postMethod = new HttpPost(url); 

    // Data that is to be sent 
    postMethod.setEntity(new UrlEncodedFormEntity(keyValuePairs)); 

    // Execute HTTP Post Request 
    String response = httpClient.execute(postMethod, res); 

有人能幫助我嗎?

在此先感謝

+0

嗨,如果你真的想得到很好的答案,請展示te帖子的代碼。你希望人們如何幫助你? – 2014-09-03 20:56:29

+0

完成!我剛剛添加了一些代碼 – 2014-09-03 21:08:53

+0

使用Chrome瀏覽器開發人員工具,嘗試在該URL上執行POST。是否也有錯誤? – alpinescrambler 2014-09-03 22:16:23

回答

0

感謝您的回答。問題是hostinger不允許遠程連接到數據庫的免費帳戶,我不知道。爲了解決這個問題,我已經更換爲其他免費的虛擬主機提供商,現在它工作正常。

0

我希望我可以幫你,即使下面的配置用來對付證書。不過,我會盡力爲您提供簡化版本,並提及它未經測試。具有證書的那個被測試和使用,所以它的工作
如果你有任何評論就說出來。

//Method for getting the http client 
protected synchronized HttpClient getHttpClient() throws Exception { 

    HttpClient client = null; 
    HttpParams params = new BasicHttpParams(); 
    HttpProtocolParams.setContentCharset(params, 
      HTTP.DEFAULT_CONTENT_CHARSET); 
    HttpConnectionParams.setConnectionTimeout(params, TIMEOUT);// (params, 
                   // TIMEOUT); 
    ConnPerRoute connPerRoute = new ConnPerRouteBean(MAX_CONN_PER_ROUTE); 
    ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute); 
    ConnManagerParams.setMaxTotalConnections(params, 20); 
    // registers schemes for http 
    SchemeRegistry schemeRegistry = new SchemeRegistry(); 
    schemeRegistry.register(new Scheme(Protocol.HTTP.toString(), 
      PlainSocketFactory.getSocketFactory(), 80)); 
    ClientConnectionManager connManager = new ThreadSafeClientConnManager(
      params, schemeRegistry); 
    client = new DefaultHttpClient(connManager, params); 
    return client; 
} 

//methods for executing the post. contentURL is the url that you use for post. 
protected HttpEntity executePostRequest(String contentURL, StringEntity body) { 
    HttpEntity entity = null; 
    HttpPost httpPost = new HttpPost(contentURL); 

    httpPost.setEntity(body); 
    httpPost.setHeader("Content-type", "application/x-www-form-urlencoded"); 
    HttpResponse response = executeHttpPost(httpPost); 
    if (response != null) { 
     entity = response.getEntity(); 
    } 
    return entity; 
} 
private synchronized HttpResponse executeHttpPost(HttpUriRequest httpPost) { 
    Exception exception = null; 
    try { 
     HttpClient request = getHttpClient(); 
     HttpResponse response = request.execute(httpPost); 
     handleStatusCode(response); 
    } catch (Exception e) { 
     Log.e(TAG, "error in executing the post request"); 
     if (e instanceof HttpResponseException) { 
      Log.e(TAG, "HttpResponseException"); 
      if (this.HTTP_STATUS_CODE == HTTP_STATUS_UNAUTHORIZED) { 
       exception = new AuthenticationException(
         "Unauthorized Exception"); 
      } else { 
       exception = new HttpException(
         "Http not authorized Exception"); 
      } 
     } else if (this.HTTP_STATUS_CODE == HTTP_STATUS_BAD_REQUEST) { 
      Log.e(TAG, "BAD Request. error in get execute"); 
      exception = new Exception("Bad Request"); 
     } 
    } 
    if (exception != null) { 
     Log.e(TAG, "Exception." + exception.getMessage()); 
    } 
    return null; 
} 
0

也許你可以嘗試發佈JSON風格,看看會發生什麼?例如:

protected void sendDataToServer(final TestData data) { throws Exception 
{ 

    Map<String,String> params = new HashMap<String, String>(); 
    params.put(Constant.FB_ID, data.getId()); // both strings??? 


DefaultHttpClient httpclient = new DefaultHttpClient(); 


HttpPost httpost = new HttpPost(url); 

//convert the passed in parameters into JSON object 
JSONObject holder = getJsonObjectFromMap(params); 


StringEntity se = new StringEntity(holder.toString()); 


httpost.setEntity(se); 

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


ResponseHandler responseHandler = new BasicResponseHandler(); 
httpclient.execute(httpost, responseHandler); 
}