我试图发布到 REST API json 对象,但我不断收到响应 404,但 url 工作正常。谁能告诉我为什么会这样?
这是我的代码:
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
HttpPost request = new HttpPost(
"http://grpsvil-webservice.si2001.it/RestChannelApp.svc/CheckCoupon");
// request.setHeader("Accept", "application/json");
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
try {
// Build JSON string
JSONStringer vehicle = new JSONStringer()
.object()
.key("CouponVerificationCode")
.value("adf")
.key("ApiKey")
.value("adfadf123")
.key("Token")
.value("fgsg342==")
.endObject();
Log.v("--", vehicle.toString());
StringEntity entity = new StringEntity(vehicle.toString());
request.setEntity(entity);
// Send request to WCF service
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
int resCode = response.getStatusLine().getStatusCode();
Log.v("--", response.getStatusLine().getStatusCode() + "");
if (resCode == 200) {
Toast.makeText(getApplicationContext(),
response.getStatusLine().getStatusCode() + "",
Toast.LENGTH_LONG).show();
BufferedReader in = new BufferedReader(
new InputStreamReader(response.getEntity()
.getContent()));
String line = "";
StringBuffer returnFromServer = new StringBuffer();
while ((line = in.readLine()) != null) {
returnFromServer.append(line);
}
// Toast what we got from server
Log.v("--", "!@# " + returnFromServer.toString());
if (entity != null) {
entity.consumeContent();
}
}
} catch (Exception e) {
// TODO: handle exception
}
Intent i = new Intent(Splash.this, Login.class);
startActivity(i);
finish();
return null;
}