0

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

这是我用于 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;
}

请告诉我将其解析为字符的详细代码。我正在使用 toast 来查看响应。在此先感谢。任何问题请发表评论。

4

2 回答 2

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);
于 2012-04-23T11:58:16.820 回答
0

目前尚不完全清楚您想要什么,但是在您的代码中,您似乎想通过http://www.sencide.com/blog/login.phpurl 登录。

该登录页面返回一个字符串“登录失败”因此您可以检查该字符串。

这样做,取消注释登录 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"))
于 2012-04-23T12:13:04.177 回答