【问题标题】:Android and JSP Communication ErrorAndroid和JSP通信错误
【发布时间】:2013-04-16 07:37:56
【问题描述】:

我有这个 JSP,它只检查 usernamepassword"out.write" 10 基于 truefalse.

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

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

现在我的安卓代码是这样的

   @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 表单对其进行了测试,它工作得非常好。

任何帮助将不胜感激。

【问题讨论】:

    标签: android apache jsp http


    【解决方案1】:
    1. 一个变量名为xml,但据我所知,它可能不包含任何XML;您正在返回 HTML。
    2. 为什么1 会位于返回字符串的第 10 位?
    3. 您正在记录xml 的内容;它的价值是什么?

    【讨论】:

    • 首先我必须说xml是类级别的String变量。
    • 首先我必须说“xml”是类级别的字符串变量。一位朋友正在使用类似的代码,它返回类似 1 或 0 大小写而不是 1 的内容。该日志代码根本不运行。
    • @AnasAzeem 您应该查看 JSON 并使用 JSON 解析器。以这种硬编码的方式依赖 Web 服务器的响应格式注定要失败。还有,如果你不知道xml的内容是什么,就给它加个log call看看就好了。
    • xml 始终为 Null,记录它的代码永远不会执行。你能推荐一些 JSON 教程吗,我对此一无所知。谢谢
    • 是时候添加一些日志调用来查看问题所在了。代码是否成功连接到您的页面?
    猜你喜欢
    • 2018-10-30
    • 1970-01-01
    • 2011-01-25
    • 2012-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多