Android和JSP通信错误

时间:2013-04-16 07:37:57

标签: android apache jsp http

我有这个JSP只根据username或{{1}检查password"out.write"以及1 0true }}

false.

现在我的android代码就像这样

    <%@ 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是空的。

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

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

  1. 变量名为xml,但它可能不包含任何XML,我可以看到;你正在返回HTML。
  2. 为什么1位于返回字符串的第10位?
  3. 您正在记录xml的内容;它的价值是什么?