我正在开发一个Android应用程序,通过新的HttpPost(url)将数据发送到服务器(托管在hostinger.es中 - 免费测试应用程序);多么好,当我在手机上运行应用程序时,我总是在我的日志中得到以下答案:
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 :(得分: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;
}
答案 1 :(得分: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);
}
答案 2 :(得分:0)
感谢您的回答。问题是hostinger不允许在免费帐户中远程连接数据库,我不知道。为了解决这个问题,我已经改为其他免费的网络托管服务提供商,现在它工作正常。
答案 3 :(得分:0)
如果Web服务正常运行,并且在Android Emulator中,您将收到403错误。。然后,我认为您应该将User-Agent放在您的httpconnection代码中。通常,您会在最新的Android 29/30中得到这种错误
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");