【发布时间】:2012-01-31 13:29:36
【问题描述】:
我想通过 Android 中的网络服务更新数据。执行相同的 POST 操作但执行“httpClient.execute(httppost)”时捕获异常,异常消息为 NULL
任何人请帮助我!
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("https://myurl");
try {
String xmln = XMLfunctions.getXMLn();
String Authorization= "Basic usrnameandpasswrd";
httppost.setHeader("Accept", "text/xml");
httppost.setHeader("Authorization", Authorization);
httppost.setHeader("Content-Type", "text/xml;charset=utf-8");
int ln=xmln.length();
String sln="";
sln=sln+ln;
httppost.setHeader("Content-Length", sln);
//Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("UserID", "0085b488"));
nameValuePairs.add(new BasicNameValuePair("FName", "Prasanth"));
nameValuePairs.add(new BasicNameValuePair("LName", "san"));
nameValuePairs.add(new BasicNameValuePair("Email", "prasanth.raghavan@xyz.com"));
nameValuePairs.add(new BasicNameValuePair("Image", "image encryptdAAAAASUVORK5CYII="));
nameValuePairs.add(new BasicNameValuePair("Culture", "en_IN"));
nameValuePairs.add(new BasicNameValuePair("Timezone", "5.0000"));
nameValuePairs.add(new BasicNameValuePair("DaylightSavings", "false"));
nameValuePairs.add(new BasicNameValuePair("PhoneNumber", "9999999999"));
StringEntity entity = new StringEntity(nameValuePairs.toString(), "UTF-8");
httppost.setEntity(entity);
BasicHttpResponse responseGet =(BasicHttpResponse) httpClient.execute(httppost);
//tried this too
//HttpResponse responseGet = httpClient.execute(httppost);
// Execute HTTP Post Request
// not executing HttpEntity httpEntity = responseGet.getEntity();
// not executing String line = EntityUtils.toString(httpEntity);
} catch (ClientProtocolException e) {
String err=e.getMessage(); // err = NULL Here
}
catch (IOException e)
{ // TODO Auto-generated catch block }
【问题讨论】:
-
那么..为什么要在 POST 中设置 Content-Length 和 Content-Type,然后将 完全不同 的内容编码到您提供的 StringEntity 中?这从一开始就看起来很糟糕。
标签: android xml web-services http-post