我想在 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 }
最佳答案
确保您在应用程序标签之外的 list 中具有以下权限:
<uses-permission android:name="android.permission.INTERNET" />
关于Android,Http Post 不执行更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9080326/