【发布时间】:2013-12-10 01:42:27
【问题描述】:
我在大学的小组项目的成员为我们的应用程序创建了这个 php。
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' );
}
我的代码通过了第一次测试,但第二次测试总是失败。错误代码“3”“未发送 POST 变量”
这是负责发送 POST 请求的 android 代码:
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;
}
有什么想法,或者有什么明显的问题吗?
做php的人是这么说的:
“POST 需要键值对(据我所知);您需要发送一个以 JSON 作为内容的“数据”键。”
据我所知,我不是这样做的吗?
我在这个网站和其他各种博客等上关注了很多帖子,他们似乎都在做与我正在做的事情相似的事情。
干杯 克里斯。
【问题讨论】:
标签: java android json http-post