1

lately i was searching in how to make a web service in eclipse to make a simple login page and i made a web service but the error i have here is when i call this web service to make it retrieve a password and username from database its give an unknown error i searched well and i find nothing so please help am stuck here.

here is the error :

 [ERROR] 2
 java.lang.ArrayIndexOutOfBoundsException: 2
at org.apache.axis2.databinding.utils.BeanUtil.deserialize(BeanUtil.java:630)
at org.apache.axis2.rpc.receivers.RPCUtil.processRequest(RPCUtil.java:153)
at org.apache.axis2.rpc.receivers.RPCUtil.invokeServiceClass(RPCUtil.java:206)
at org.apache.axis2.rpc.receivers.RPCMessageReceiver.invokeBusinessLogic(RPCMessageReceiver.java:117)
at org.apache.axis2.receivers.AbstractInOutMessageReceiver.invokeBusinessLogic(AbstractInOutMessageReceiver.java:40)
at org.apache.axis2.receivers.AbstractMessageReceiver.receive(AbstractMessageReceiver.java:114)
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:181)
at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:172)
at org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:146)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:409)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1044)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:313)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

here is the code:

package com.abod.abd;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class calculate {
    public String authentication(String userName,String password){

        String retrievedUserName = "";
        String retrievedPassword = "";
        String status = "";
        try{

            Class.forName("com.mysql.jdbc.Driver");
            Connection con =             DriverManager.getConnection("jdbc:mysql://localhost:3306/login","root","aa198965");
            PreparedStatement statement =  con.prepareStatement("SELECT * FROM `user`");

            ResultSet result = statement.executeQuery();

            while(result.next()){
                retrievedUserName = result.getString("username");
                retrievedPassword = result.getString("password");
            }

            if(retrievedUserName.equals(userName)&&retrievedPassword.equals(password)){
                status = "Success!";
            }

            else{
                status = "Login fail!!!";
            }

        }
        catch(Exception e){
            e.printStackTrace();
        }
        return status;
    }
}
Community
  • 1
  • 1

3 Answers3

1

I find the problem finally , it was in the android side I changed the parameters to match the webservice and every thing worked find

0

Android does not understand localhost. use 10.0.2.2 instead of localhost and give it a try again.

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Waqar Ahmed
  • 5,005
  • 2
  • 23
  • 45
  • thank you ,i did this to , and its not working the same error still here ,and the code above is refer to the web service ,not android does it effect ?? and any other help that u have please ?? – Abd Alghani Omar Dowabsheh Feb 11 '14 at 05:38
  • since you are executing this code from android device , so you need to tell android that connect to my localhost and android understand localhost as 10.0.2.2 – Waqar Ahmed Feb 11 '14 at 05:58
  • error shows your index is out of bound.check whether the query is executed or not. I guess your result set is emplt .check your user table and try to log whether it contains any data or not. – Waqar Ahmed Feb 11 '14 at 06:03
  • No this is web service code not the code of the android application , depending on your 2nd comment how to check the query ?? Am using eclipse to creat services so maybe its the same – Abd Alghani Omar Dowabsheh Feb 11 '14 at 11:20
0

If you want to connect to MySQL Database from Android you just need to follow these steps:

  • Download the driver mysql-connector-java-3.0.17-ga-bin.jar
  • Paste it to the folder libs in your android project.
  • Click ConfigureBuildPath->add jar to include the jar to the project.
  • Check permission in AndroidManifest.xml for INTERNET.

Use the next code for connecting:

try{
    Class.forName("com.mysql.jdbc.Driver").newInstance();
}catch(Exception e){
    System.err.println("Cannot create connection");
}
try{
    connection = DriverManager.getConnection("jdbc:mysql://192.168.xx.xx:3306/dbname","root","password");
    Statement statement = connection.createStatement();

    String query = "SELECT column1, column2 FROM table1 WHERE column3 = ";
    query = query +"'" +variable+"'";           
    ResultSet result = statement.executeQuery(query);
}catch(Exception e){
    System.err.println("Error");
}

Advice: If the instance of the drivers doesn't give any errors but you get an exception with the connection you should try to remove the Target SDK version from your manifest, as for some versions this gives problems.

Ref : https://stackoverflow.com/a/14982316/1318946

Community
  • 1
  • 1
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437