0

我希望通过android中的soap webservice在mysql数据库中插入下拉值...如果我必须从我的android应用程序中更改下拉值意味着更新值也保存在mysql数据库中...怎么办。请帮助我。

这是我的网络服务代码:

public class Insertion {

public String insertData(String userName,String userPassword){

try{

 Class.forName("com.mysql.jdbc.Driver");
 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/androidlogin","root","");
 PreparedStatement statement =  con.prepareStatement("INSERT INTO user(status) VALUES ('"+userName+"');");
 int result = statement.executeUpdate();
  }

   catch(Exception exc){
   System.out.println(exc.getMessage());
     }

   return "Insertion successfull!!";
     }

     }

这是我的安卓代码:

    public class InsertionExample extends Activity{
    private final String NAMESPACE = "http://ws.retailer.com";
private final String URL = "http://192.168.1.168:8085/Retailer/services/GetCurrentDateTime?wsdl";
private final String SOAP_ACTION = "http://ws.retailer.com/insertData";
private final String METHOD_NAME = "insertData";
Button btninsert;
private Spinner spnMusketeers;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    btninsert = (Button)findViewById(R.id.btn_insert);  
    btninsert.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
         insertValues();
        }
    });
}

public void insertValues(){
Spinner userName = (Spinner) findViewById(R.id.spnMusketeers);
    String user_Name = userName.getContext().toString();

 spnMusketeers = (Spinner) findViewById(R.id.spnMusketeers);
 List<String> lsMusketeers = new ArrayList<String>();
 lsMusketeers.add("Q");
 lsMusketeers.add("C");
 lsMusketeers.add("P");
 lsMusketeers.add("I");
 lsMusketeers.add("F");

 ArrayAdapter<String> aspnMusketeers = 
   new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, 
    lsMusketeers);
 aspnMusketeers.setDropDownViewResource
    (android.R.layout.simple_spinner_dropdown_item);
 spnMusketeers.setAdapter(aspnMusketeers);

  // Set up a callback for the spinner
   spnMusketeers.setOnItemSelectedListener(
 new OnItemSelectedListener() {
     public void onNothingSelected(AdapterView<?> arg0) { }

     public void onItemSelected(AdapterView<?> parent, View v, 
      int position, long id)  {
         SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
         PropertyInfo unameProp =new PropertyInfo();
         unameProp.setName("spnMusketeers");//Define the variable name in the web service method
         unameProp.setValue(status);//Define value for fname variable
         unameProp.setType(String.class);//Define the type of the variable
         request.addProperty(unameProp);//Pass properties to the variable



         SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
         envelope.setOutputSoapObject(request);
         HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

         try{
          androidHttpTransport.call(SOAP_ACTION, envelope);
             SoapPrimitive response = (SoapPrimitive)envelope.getResponse();

             TextView result = (TextView) findViewById(R.id.textView2);
             result.setText(response.toString());

      }
      catch(Exception e){

      } 
       // Code that does something when the Spinner value changes
     }
 });

  } 

  }

如果我必须在单击插入按钮后从微调器中选择值意味着它没有插入。应用程序被强制关闭...请查看我的代码并为我提供正确的解决方案。

4

1 回答 1

0

对此插入功能使用以下代码:

String selectedItem = parent.getItemAtPosition(pos).toString();
于 2012-08-24T06:27:00.913 回答