【问题标题】:Using Android REST API HttpPost sends empty data on Post body使用 Android REST API HttpPost 在 Post 正文上发送空数据
【发布时间】:2013-07-31 09:29:09
【问题描述】:

我试图通过 HttpPost 发送一些数据,但结果是空的。如果我的方法和 HttpGET 部分运行良好,请在下面。但是当我尝试向 Post 正文添加一些数据并发送时,我没有收到任何错误,并且 Logcat(另一端的 REST Web 服务)显示 Post 数据为空。

 public void sendPhoto(View v) {
        final String kookojaUrlGet = "http://localhost/getData";
        final String kookojaUrlPost = "http://localhost/postData";
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpClient httpClient = new DefaultHttpClient();
                try {
                    HttpResponse getResponse = httpClient.execute(new HttpGet(kookojaUrlGet));
                    String returnedStuff = EntityUtils.toString(getResponse.getEntity());
                    Log.d("debug","http get returned " + returnedStuff);
                    JSONObject jsonGet = new JSONObject(returnedStuff);
                    int xxxTime = jsonGet.getInt("timestamp");
                    String xxxNone = jsonGet.getString("nonce");
                    String xxxApp = jsonGet.getString("appName");
                    String xxxCID = jsonGet.getString("csrfToken");
                    Log.d("debug","json was " + xxxTime + " " + xxxNone + " " + xxxApp + " " + xxxCID);
                    JSONObject c = new JSONObject();
                    c.put("_username","xxx@gmail.com");
                    c.put("_password","123");
                    c.put("_timestamp", xxxTime);
                    c.put("_nonce", xxxNone);
                    c.put("_appName", xxxApp);
                    c.put("CID", xxxCID);
                    c.put("body", photoPost);
                    c.put("long", photoLong);
                    c.put("lat", photoLat);
                    StringEntity sendStuff = new StringEntity(c.toString());
                    HttpPost xxxPost = new HttpPost(kookojaUrlPost);
                    xxxPost.setHeader("Accept", "application/json");
                    xxxPost.setHeader("Content-type", "application/json");
                    xxxPost.setHeader("Accept-Encoding", "gzip");
                    xxxPost.setEntity(sendStuff);
                    HttpResponse postResponse = httpClient.execute(xxxPost);
                    String postText = EntityUtils.toString(postResponse.getEntity());
                    Log.d("debug", "rest post response was " + postText);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

任何帮助将不胜感激。

【问题讨论】:

  • 您将 content-type 标头设置了两次,这可能不是一个好主意。
  • 我的错!我删除了那条线。它实际上在我的代码中被注释掉了。
  • localhost url 不太可能工作,除非您的服务器实际上在设备上
  • 在 AVD 中连接到 localhost 你需要使用 url 10.0.2.2 而不是 localhost
  • androidhive.info/2012/01/… 可能会对您有所帮助。

标签: android rest


【解决方案1】:

我通过使用“List”而不是 JSONObject 解决了这个问题。这是代码:

List<NameValuePair> c = new ArrayList<NameValuePair>(2);
c.add(new BasicNameValuePair("_username", "xxx@gmail.com"));
c.add(new BasicNameValuePair("_password", "123"));
c.add(new BasicNameValuePair("_timestamp", xxxTime2));
c.add(new BasicNameValuePair("_nonce", xxxNone));
c.add(new BasicNameValuePair("_appName", xxxApp));
c.add(new BasicNameValuePair("CID", xxxCID));
c.add(new BasicNameValuePair("body", photoPost));
c.add(new BasicNameValuePair("long", "4524524524"));
c.add(new BasicNameValuePair("lat", "242452452"));
xxxPost.setEntity(new UrlEncodedFormEntity(c));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多