我想從Android應用程序將數據發送到服務器(.NET),但它不工作,這是我的代碼:如何將數據從我的android應用程序發佈到服務器?
DefaultHttpClient hc=new DefaultHttpClient();
ResponseHandler <String> res=new BasicResponseHandler();
HttpPost postMethod=new HttpPost(myURl);
postMethod.getParams().setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, Boolean.FALSE);
postMethod.getParams().setBooleanParameter("http.protocol.expect-continue", false);
postMethod.setHeader("Content-Type", "application/json");
JSONObject json = new JSONObject();
json.put("TOKEN", channel_token).toString();
json.put("APPLICATIONDATASOURCEID", data_src_id).toString();
json.put("NEWSTITLE", Title_edittext.getText().toString().trim()).toString();
json.put("NEWSDETAILS", Details_edittext.getText().toString()).toString();
json.put("ALERTSTARTSAT" , "12/03/2012/05/12");
json.put("ALERTENDSAT", "13/03/2012/06/12");
json.put("SENDPUSHNOTIFICATION", true);
json.put("EXPIREIMMEDIATELY", true);
Log.i("jason Object", json.toString());
StringEntity se = new StringEntity(json.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
postMethod.setEntity(se);
HttpResponse response = hc.execute(postMethod);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
String _response = convertStreamToString(is);
System.out.println("res " + _response);
得到這個從服務器響應「Expectaions失敗」。請告訴我問題在哪裏。
你在json字符串中傳遞正確的參數嗎?什麼是你的StatusCode? – waqaslam 2012-02-28 09:20:43
參數是否正確,狀態碼的含義。 – 2012-02-28 09:28:12
try postMethod.getParams()。setParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE,Boolean.TRUE); – 2012-02-28 09:37:46