0

我有这个 JSP,它只检查usernameandpassword"out.write" 1or0基于trueorfalse.

    <%@ page import="java.io.*" %>    
    <%
        if(request.getParameter("username").equals("anas") && request.getParameter("password").equals("azeem"))
        {
            out.write("1");

        }
        else
            out.write("0");
    %> </br>

现在我的android代码是这样的

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_agent_login);

        edit_username = (EditText) findViewById(R.id.edit_username);
        edit_password = (EditText) findViewById(R.id.edit_password);
        btn_login = (Button) findViewById(R.id.btnLogin);

        if (android.os.Build.VERSION.SDK_INT > 9) { // must add this code in
                            // order not to get the
                            // Exception while executing
                            // program
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                    .permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        btn_login.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                username = edit_username.getText().toString();
                password = edit_password.getText().toString();

                Log.d(TAG, "Username:" + username);
                Log.d(TAG, "Password:" + password);

                try {
                    new Thread() {
                        public void run() {
                            try {
                                HttpClient client = new DefaultHttpClient();
                                HttpPost post = new HttpPost("http://10.0.2.2:8080/MyApp/login.jsp");
                                List<NameValuePair> pairs = new ArrayList<NameValuePair>(2);
                                pairs.add(new BasicNameValuePair("username",edit_username.getText().toString()));
                                pairs.add(new BasicNameValuePair("password",edit_password.getText().toString()));
                                post.setEntity(new UrlEncodedFormEntity(pairs));
                                HttpResponse response = client.execute(post);
                                HttpEntity httpEntity = response.getEntity();
                                xml = EntityUtils.toString(httpEntity);
                                Log.d("xml", ""+xml.length());   //To confirm anything is there in the "xml"
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }.start();

                } catch (Exception e) {
                    Log.d("xml", xml.toString());
                    Log.d("Server", e.toString());
                }

                try {
                    if (Integer.parseInt(String.valueOf(xml.charAt(10))) == 1) {  //****HERE*******
                        Toast.makeText(getApplicationContext(), "LoginSuccessful",Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(Agent_Login.this,AgentHome.class);
                        startActivity(intent);
                    } else {
                        Toast.makeText(getApplicationContext(),"Invalid Username or Password", Toast.LENGTH_SHORT).show();
                        edit_password.setText("");
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });

    }

现在在这里(在评论中)给出了一个NullPointerException,我认为这是因为它xml是空的。

所以,我的问题是如何从 Android 中的 JSP 获得回复。我在 PC 上使用 HTML 表单对其进行了测试,它工作得非常好。

任何帮助将不胜感激。

4

1 回答 1

0
  1. 一个变量已命名xml,但据我所知,它可能不包含任何 XML;您正在返回 HTML。
  2. 为什么会1位于返回字符串中的第 10 位?
  3. 您正在记录xml; 它的价值是什么?
于 2013-04-16T07:50:03.043 回答