【问题标题】:Android (Java) HTTP POST JSON to server, but the server tells me no POST variables were seenAndroid(Java)HTTP POST JSON到服务器,但服务器告诉我没有看到POST变量
【发布时间】: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


    【解决方案1】:

    已解决:

    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");
    

    【讨论】:

      【解决方案2】:

      你的朋友是对的,你确实需要键值对。

      StringEntity 只是一个插入到 http 消息数据字段中的原始字符串。这必须正确格式化,以便接收者解析它。

      尝试使用 URLEncodedFormEntity ,您将传递一个(1 个)键值对列表。键将是您要调用的参数,值将是 json 字符串

      示例:

      BasicNameValuePair param = new BasicNameValuePair("paramName",json);
      List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>()
      params.add(param);
      URLEncodedFormEntity entity = new URLEncodedFormEntity(params);
      entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
      httpPost.setEntity(entity);
      

      【讨论】:

      • 你能举个例子吗?我不太明白 id 如何使用它发送 JSONObject。
      • 编辑中提供的示例,您仍将其视为通过 http 发送的字符串
      【解决方案3】:

      实际上试试这个答案。除非您像这样正确创建身份验证标头,否则尝试将身份验证作为实体字符串发送同时附加文件(即 JSON)似乎会失败:

      how to send http request using http header

      【讨论】:

        猜你喜欢
        • 2016-07-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-08
        • 1970-01-01
        • 2016-04-06
        相关资源
        最近更新 更多