嗨,我必须开发应用程序是通过 android 应用程序中的肥皂网络服务将来自 spinner 的数据库插入 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!!";
}
}
我已将以下代码用于 android spinner 示例:
public class InsertionExample extends Activity{
private final String NAMESPACE = "http://xcart.com";
private final String URL = "http://192.168.1.168:8085/XcartLogin/services/Insertion?wsdl";
private final String SOAP_ACTION = "http://xcart.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(){
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
Spinner userName = (Spinner) findViewById(R.id.spnMusketeers);
List<String> lsMusketeers = new ArrayList<String>();
lsMusketeers.add("Q");
lsMusketeers.add("P");
lsMusketeers.add("C");
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) {
// Code that does something when the Spinner value changes
}
});
String user_Name = userName.getContext().toString();
//Pass value for userName variable of the web service
PropertyInfo unameProp =new PropertyInfo();
unameProp.setName("userName");//Define the variable name in the web service method
unameProp.setValue(user_Name);//Define value for fname variable
unameProp.setType(String.class);//Define the type of the variable
request.addProperty(unameProp);
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
}
}
这里如何更改我的代码来解决这个问题。
我的 logcat 窗口说:
08-23 02:48:40.030: D/AndroidRuntime(4055): Shutting down VM
08-23 02:48:40.030: W/dalvikvm(4055): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-23 02:48:40.060: E/AndroidRuntime(4055): FATAL EXCEPTION: main
08-23 02:48:40.060: E/AndroidRuntime(4055): java.lang.NullPointerException
08-23 02:48:40.060: E/AndroidRuntime(4055): at com.android.soap.InsertionExample.insertValues(InsertionExample.java:63)
08-23 02:48:40.060: E/AndroidRuntime(4055): at com.android.soap.InsertionExample$1.onClick(InsertionExample.java:43)
08-23 02:48:40.060: E/AndroidRuntime(4055): at android.view.View.performClick(View.java:2408)
08-23 02:48:40.060: E/AndroidRuntime(4055): at android.view.View$PerformClick.run(View.java:8816)
08-23 02:48:40.060: E/AndroidRuntime(4055): at android.os.Handler.handleCallback(Handler.java:587)
08-23 02:48:40.060: E/AndroidRuntime(4055): at android.os.Handler.dispatchMessage(Handler.java:92)
08-23 02:48:40.060: E/AndroidRuntime(4055): at android.os.Looper.loop(Looper.java:123)
08-23 02:48:40.060: E/AndroidRuntime(4055): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-23 02:48:40.060: E/AndroidRuntime(4055): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 02:48:40.060: E/AndroidRuntime(4055): at java.lang.reflect.Method.invoke(Method.java:521)
08-23 02:48:40.060: E/AndroidRuntime(4055): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-23 02:48:40.060: E/AndroidRuntime(4055): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-23 02:48:40.060: E/AndroidRuntime(4055): at dalvik.system.NativeStart.main(Native Method)
请帮助我......这里发生了什么错误。给我解决方案......