我试图通过 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();
}
任何帮助,将不胜感激。