2012-04-23 25 views
0

我从http post(一个php服务器)得到我的响应 但是,我想将其解析为简单字符。怎么做 。Android从http响应中将xml解析为字符

这里是我下面的方法,我使用HttpPost

public void postLoginData() 
    { 
    // Create a new HttpClient and Post Header 
    HttpClient httpclient = new DefaultHttpClient(); 

    /* login.php returns true if username and password is equal to saranga */ 
    // HttpPost httppost = new HttpPost("http://www.sencide.com/blog/login.php"); 
    HttpPost httppost = new HttpPost("http://advanmind.com/adapi/user/add/");//username and password is xyz 
    try { 
     // Add user name and password 
     EditText usermail = (EditText)findViewById(R.id.editText1); 
     String email = usermail.getText().toString(); 

     EditText pword = (EditText)findViewById(R.id.editText2); 
     String password = pword.getText().toString(); 

     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
     nameValuePairs.add(new BasicNameValuePair("email", email)); 
     nameValuePairs.add(new BasicNameValuePair("password", password)); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

     // Execute HTTP Post Request 
     Log.w("ADPORTAL", "Execute HTTP Post Request"); 
     HttpResponse response = httpclient.execute(httppost); 

     String str = inputStreamToString(response.getEntity().getContent()).toString(); 
     Log.w("ADPORTAL", str); 

     if(str.toString().equalsIgnoreCase("false")) 
     { 


      Log.w("ADPORTAL", "FALSE"); 
      Toast.makeText(getBaseContext(), str, 10000).show(); 
      result.setText("You are not registerd please register"); 

      //Intent AfterLogin = new Intent(this, AfterLogin.class); 
      //startActivity(AfterLogin); 
     }else 
     {Log.w("SENCIDE", "TRUE"); 

     // Intent AfterLogin = new Intent(this,AfterLogin.class); 
     // startActivity(AfterLogin); 
     Toast.makeText(getBaseContext(), str, 10000).show(); 
     //result.setText(str+" loginsuccess"); 

      // Intent registration = new Intent(this, Registration.class); 
      //startActivity(registration); 
     } 

    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

输入流方法

private StringBuilder inputStreamToString(InputStream is) { 
    String line = ""; 
    StringBuilder total = new StringBuilder(); 
    // Wrap a BufferedReader around the InputStream 
    BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
    // Read response until the end 
    try { 
     while ((line = rd.readLine()) != null) { 
      total.append(line); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    // Return full string 
    return total; 
} 

请告诉我详细代码解析它变成文字。我正在使用敬酒来查看response.thanks提前。任何问题,请做评论。

回答

0

我希望我正确地理解你的问题,但是这个代码将返回响应作为字符串(:

HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("http://advanmind.com/adapi/user/add/"); 
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
nameValuePairs.add(new BasicNameValuePair("email", email)); 
nameValuePairs.add(new BasicNameValuePair("password", password)); 
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
ResponseHandler<String> res = new BasicResponseHandler(); 
String response = httpclient .execute(postMethod, res); 
+0

我会检查...... – divaNilisha 2012-04-24 01:00:23

+0

非常感谢..它果然奏效为了我。 – divaNilisha 2012-04-24 10:22:05

0

这是不完全清楚自己想要什么,但是,你的代码中,它看起来像你想。在登录网址http://www.sencide.com/blog/login.php

该登录页面返回一个字符串“登录失败”所以,你可以检查字符串

做到这一点,unremark登录URL,备注添加用户线:

HttpPost httppost = new HttpPost("http://www.sencide.com/blog/login.php") 
    //HttpPost httppost = new HttpPost("http://advanmind.com/adapi/user/add/"); 

而改变这一行:

if(str.toString().equalsIgnoreCase("false")) 

要这样:

if(str.toString().equalsIgnoreCase("Login Failed")) 
+0

对不起,如果我张贴两台服务器.one是[(“http://advanmind.com/adapi/user/add/”)] other是[(“http://www.sencide.com/blog/login.php “)。但我的问题不是这样,我从服务器以XML格式得到正确的响应,我想以纯文本而不是xml读取。假设我的xml是 abc def。但我想读作[Item-abc,Item-def]或类似的东西(没有xml节点)。如果我抛出错误的问题,很抱歉。 – divaNilisha 2012-04-24 00:52:48