【发布时间】:2015-03-02 17:59:37
【问题描述】:
hy 我必须使用服务器移动应用程序。在这个应用程序中,我点击了带有用户名和密码等参数的 url,它返回服务器响应,它告诉服务器是否启动,表示是或否。但它以 xml 文件的形式返回。那么如何保存该 xml 文件,然后将其转换为 string 。我想将其转换为字符串,以便我可以向他显示服务器已启动是和否。请任何帮助将不胜感激
这是我正在使用的代码
`public String login(String uname,String password)
{
InputStream is = null;
String result = "";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("Your Url");
nameValuePairs.add(new BasicNameValuePair("emailid", uname));
nameValuePairs.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch (Exception e)
{
Log.e("log_tag", "Error in http connection " + e.toString());
}
// convert response to string
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader( is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.v("log","Result :"+result);
}
catch (Exception e)
{
Log.v("log", "Error converting result " + e.toString());
}
return result;
}`
但在这方面它对 jason 有效而且我正在获取 xml 文件,那么如何转换该文件就是你的问题
【问题讨论】:
-
你为什么不包括你迄今为止尝试过的代码,以及它失败的地方。如果你还没有头绪,谷歌肯定会为你举几个例子。
标签: android xml-parsing server