0

ive build in netbeans ws that read and write data on my sql data base. ive checked throw netbeans and the code from there work just fine. i have downloaded the ksoap libary into my eclipse and build there my client. when it try to run the app it crash with the error

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

this is my code, i want to sent the number '1' and get from my sql data base the same id.

public class Main_WebService3_3_Activity extends Activity {

private static final String NAMESPACE = "http://it.gy.com/";
private static final String URL = "http://10.0.2.2:8080/MyAndroidWs/MyAndroidWs?WSDL";
private static final String SOAP_ACTION = "http://it.gy.com/insertDataToSql";
private static final String METHOD_NAME = "getDataFromSql";



EditText et1;
EditText et2;
EditText et3;

Button btn1;
Button btn2;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main__web_service3_3_);
    et1=(EditText) findViewById(R.id.editText1);
    et2=(EditText) findViewById(R.id.editText2);
    et3=(EditText) findViewById(R.id.editText3);
    btn1=(Button) findViewById(R.id.button1);
    btn2=(Button) findViewById(R.id.button2);
    btn1.setOnClickListener(btn1Listener);
//  btn2.setOnClickListener(btn2Listener);  


}

private OnClickListener btn1Listener=new OnClickListener() {        
    @Override
    public void onClick(View v) {                   
        Thread netWorkThread=new Thread(){
        public void run() {
        SoapObject requset=new SoapObject(NAMESPACE,METHOD_NAME);
        PropertyInfo propInfo=new PropertyInfo();
        propInfo.setName("id");
        propInfo.setValue(1);
        propInfo.setType(PropertyInfo.INTEGER_CLASS);
        requset.addProperty(propInfo);

        SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(requset);
        envelope.dotNet=false;

        try{            
        HttpTransportSE ht=new HttpTransportSE(URL);
        ht.call(SOAP_ACTION, envelope);
        final SoapPrimitive response=(SoapPrimitive)envelope.getResponse();
        final String str=response.toString();


        }catch (Exception e) {
            Toast.makeText(getApplicationContext(), "catch", Toast.LENGTH_SHORT).show();
        }
        }
        };          
        netWorkThread.start();
    }



};
private OnClickListener btn2Listener=new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }
};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main__web_service3_3_, menu);
    return true;
}
}



enter code here

if its help, this is my web service code

@WebMethod(operationName = "insertDataToSql")
public String insertDataToSql(@WebParam(name = "id") int id, @WebParam(name = "firstName") String firstName, @WebParam(name = "lastName") String lastName) throws ClassNotFoundException, SQLException {
       String driver="com.mysql.jdbc.Driver";
        String url="jdbc:mysql://localhost:3306/db_android";
        String sqlString=String.format("insert into db_android.table1(id,firstName,lastName) values("+id+","+firstName+","+lastName+")");
        int x=0;
        Class.forName(driver);
        Connection con=DriverManager.getConnection(url,"root","1234");
        if(con!=null){
            Statement stmt=con.createStatement();
            stmt.executeUpdate(sqlString);
            con.close();
            x=1;
    return "ok";
}
return  "unsucsess";
}
/**
 * Web service operation
 */
@WebMethod(operationName = "getDataFromSql")
public String getDataFromSql(@WebParam(name = "id") int id) throws ClassNotFoundException, SQLException {
        String st="";
        String driver="com.mysql.jdbc.Driver";
        String url="jdbc:mysql://localhost:3306/db_android";
        Class.forName(driver);
        Connection con=DriverManager.getConnection(url,"root","1234");
        String sqlString=String.format("select id,firstName,lastName from db_android.table1 Where id='"+id+"'");
         Statement stmt=con.createStatement();  
        if(con!=null){               
        ResultSet rs=stmt.executeQuery(sqlString);
        st+=rs.getString("id");
        st+=" ";
        st+=rs.getString("firstName");
        st+=" ";
        st=rs.getString("lastName");
            con.close();              
    return "ok";
}
return  "unsucsess";
}
flx
  • 14,146
  • 11
  • 55
  • 70
Gili Yaniv
  • 3,073
  • 2
  • 19
  • 34
  • PLEAE, use the forum search function before posting a question. This question has been asked about 1000 times before. https://stackoverflow.com/search?q=Can%27t+create+handler+inside+thread – SimonSays Mar 03 '14 at 23:26
  • i did, but none of the question i saw was related to sql. still I got no answer to my sql problem. – Gili Yaniv Mar 04 '14 at 17:03
  • Google for the error message. It will tell you what the reason is for it. It has nothing to to with SQL or the database at all. It has to do with Threads and async code execution. Good luck. – SimonSays Mar 04 '14 at 18:29

0 Answers0