こんにちは、アプリを開発する必要があります。Android アプリケーションの SOAP Webserices を介して、mysql データベースのスピナーからデータベースを挿入します...
私は以下のWebサービスコードを使用しています:
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://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)
助けてください....ここで発生したエラーは何ですか.解決策を教えてください...