当android使用sql web服务时app崩溃

时间:2014-03-03 23:15:36

标签: java android eclipse web-services ksoap2

我建立了netbeans ws,它在我的sql数据库上读写数据。 香港专业教育学院检查扔netbeans和那里的代码工作得很好。 我已经将ksoap libary下载到我的eclipse中并在那里构建了我的客户端。当它试图运行应用程序时它崩溃并出现错误

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

这是我的代码,我想发送数字'1'并从我的sql数据库获取相同的id。

公共类Main_WebService3_3_Activity扩展了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

如果有帮助,这是我的网络服务代码

@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";
}

0 个答案:

没有答案