Android,Http Post 不执行更新

标签 android xml web-services http-post

我想在 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/

相关文章:

regex - 匹配 XML 数据中的特定 CDATA 部分

asp.net - 创建没有 ASMX 文件的 Web 服务?

java - 使用 jsonbody 请求将 JSON 转换为 string/int

java - 即使没有屏幕旋转,也要让 ViewModel 保持 Activity 状态

android - 使用电子邮件列表测试器发布封闭式 Alpha 测试后无法获取激活 URL

jQuery - xpath 找到?

objective-c - XML 解析问题

java - 如何在现有的Web 项目中集成Web 服务?

android - Wearable.DataApi.deleteDataItems 不工作

android - 如何判断安卓设备是否有触摸屏?