Android版(Java)的HTTP POST JSON服务器,但是服务器告诉我没有POST变量被视为 [英] Android (Java) HTTP POST JSON to server, but the server tells me no POST variables were seen
问题描述
在我大学项目组的成员创造了这个PHP为我们的应用程序。
A member of my group project at university created this php for our application.
if ( $_SERVER['REQUEST_METHOD'] != 'POST' ) {
header( 'HTTP/1.0 405 Method Not Allowed' );
output( 2, 'only POST is accepted; received ' . $_SERVER['REQUEST_METHOD'], true );
}
# catch empty POST requests
if ( count( $_POST ) == 0 ) {
output( 3, 'no POST variables were sent' );
}
我的code经过一次测试,但它始终是失败的第二个测试。与错误code'3''没有POST变量被送到
My code passes the first test, but it is always failing this second test. with the error code '3' 'no POST variables were sent'
这是Android code负责发送POST请求:
This is the android code responsible for sending the POST request:
public void uploadToServer(Vector<PointOfInterest> pois) {
try {
JSONObject auth = new JSONObject();
JSONObject walk = new JSONObject();
JSONArray points = new JSONArray();
walk.put("walk name", "TEST NAME");
walk.put("walk desc short", "TEST SHORT");
walk.put("walk desc long", "TEST LONG");
for(int i = 0; i < pois.size(); i ++){
JSONObject location = new JSONObject();
location.put("name", pois.get(i).getName());
location.put("timestamp", 0);
location.put("lattitude",pois.get(i).getLattitude());
location.put("longitude",pois.get(i).getLongitude());
location.put("Description",pois.get(i).getDescription());
points.put(location);
}
auth.put("data", walk);
walk.put("locations", points);
auth.put("authorization","");
auth.put("hash","3b6decebf0bab0e0a08c18a94849d6df1e536d65");
auth.put("salt", "dave");
UploadASyncTask upload = new UploadASyncTask();
upload.execute(auth);
} catch (Exception e) {
e.printStackTrace();
}
}
private class UploadASyncTask extends AsyncTask<JSONObject, Void, Void>{
@Override
protected Void doInBackground(JSONObject...auth) {
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://project.chippy.ch/upload.php");
String json = "";
json = auth.toString();
StringEntity se = new StringEntity(json);
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httpPost.setEntity(se);
httpPost.setHeader("User-Agent", "WalkingTourCreator/1.0");
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
//httpPost.setHeader("data", json);
HttpResponse httpResponse = httpclient.execute(httpPost);
InputStream inputStream = httpResponse.getEntity().getContent();
String result = "";
if(inputStream != null){
result = convertInputStreamToString(inputStream);
}
else{
result = "Did not work!";
}
Log.d("RESULT", result);
}catch(Exception e){
Log.e("ERROR IN SEVER UPLOAD", e.getMessage());
}
return null;
}
任何意见或任何明显的问题?
Any ideas, or any obvious problems?
谁做的PHP的人说这样的:
The guy who made the php said this:
后需要键 - 值对(据我可以告诉);你需要发送的关键。用JSON作为内容的数据
"POST needs key-value pairs (as far as I can tell); you'll need to send a key of "data" with the JSON as the contents."
据我所知,没有我这样做呢?
As far as i am aware aren't i doing this?
我跟随了很多本网站的帖子和其他各种布洛格斯等,他们似乎都做类似的事情,以我在做什么。
I have followed a lot of posts on this site and various other bloggs etc, and they all seem to do a similar thing to what i am doing.
干杯
克里斯。
Cheers Chris.
推荐答案
解决:
HttpParams params = new BasicHttpParams();
//params.setParameter("data", auth);
HttpClient httpclient = new DefaultHttpClient(params);
HttpPost httpPost = new HttpPost("http://project.chippy.ch/upload.php");
List<NameValuePair> postParams = new ArrayList<NameValuePair>();
postParams.add(new BasicNameValuePair("data", auth.toString()));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParams);
entity.setContentEncoding(HTTP.UTF_8);
httpPost.setEntity(entity);
被要求代替
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://project.chippy.ch/upload.php");
String json = "";
json = auth.toString();
StringEntity se = new StringEntity(json);
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httpPost.setEntity(se);
httpPost.setHeader("User-Agent", "WalkingTourCreator/1.0");
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
这篇关于Android版(Java)的HTTP POST JSON服务器,但是服务器告诉我没有POST变量被视为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!