使用kso​​ap生成警告通过Web服务调用传递字符串

时间:2011-11-15 12:18:24

标签: android web-services ksoap2 android-ksoap2

我已经实现了一个调用SOAP Web服务的Android代码。 Web服务采用“abc#bcd#efg#”形式的String参数。当我传递这样一个字符串时,我得到如下警告:

请注意: ArrayList items2 = new ArrayList();

来自logcat:

   11-15 17:44:35.511: INFO/System.out(304): arr2 is ann
   11-15 17:44:35.511: INFO/System.out(304): [ann]
   11-15 17:44:35.511: INFO/System.out(304): absent students are
   11-15 17:44:35.511: INFO/System.out(304): ann#
   11-15 17:44:35.511: INFO/System.out(304): arr2 is john
   11-15 17:44:35.511: INFO/System.out(304): [ann, john]
   11-15 17:44:35.521: INFO/System.out(304): absent students are
   11-15 17:44:35.521: INFO/System.out(304): ann#john#
   11-15 17:44:35.521: DEBUG/(304): items:
   11-15 17:44:35.521: DEBUG/string is(304): ann
   11-15 17:44:35.521: DEBUG/string is(304): john
   11-15 17:44:37.121: WARN/System.err(304): java.lang.ClassCastException:  org.ksoap2.serialization.SoapPrimitive
   11-15 17:44:37.121: WARN/System.err(304):     at com.example.display.call2(display.java:237)
   11-15 17:44:37.133: WARN/System.err(304):     at com.example.display$3.onClick(display.java:196)
   11-15 17:44:37.133: WARN/System.err(304):     at android.view.View.performClick(View.java:2408)
   11-15 17:44:37.133: WARN/System.err(304):     at android.view.View$PerformClick.run(View.java:8816)
   11-15 17:44:37.133: WARN/System.err(304):     at android.os.Handler.handleCallback(Handler.java:587)
   11-15 17:44:37.133: WARN/System.err(304):     at android.os.Handler.dispatchMessage(Handler.java:92)
   11-15 17:44:37.133: WARN/System.err(304):     at android.os.Looper.loop(Looper.java:123)
   11-15 17:44:37.133: WARN/System.err(304):     at android.app.ActivityThread.main(ActivityThread.java:4627)
   11-15 17:44:37.151: INFO/System.out(304): A is 

由于这个应用程序运行但仍然没有得到预期的响应

我的Android代码:

 //Button code to insert checkbox value 

alertDialog.setNegativeButton("Mark absent", new DialogInterface.OnClickListener()
    {
    public void onClick(DialogInterface dialog, int whichButton) 
    {

      final Button markabsent = (Button) findViewById(R.id.Button02);
      markabsent.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {

            // Perform action on click
             Toast.makeText(display.this,"You have marked the students absent",Toast.LENGTH_SHORT).show();

             SparseBooleanArray checkedabsent = lView.getCheckedItemPositions();



            for (int i = 0; i < arr2.length; i++) 
            {
                if (checkedabsent.get(i)) 
                {
                    System.out.println("arr2 is "+arr2[i]);
                    items2.add(arr2[i]);
                    System.out.println(items2);
                    s.append(arr2[i] + "#");

                    x=s.toString();
                    //s.deleteCharAt(s.length()-1);


                    System.out.println("absent students are\n" +s);


                }
            }           

            Log.d("", "items:");
            for (String string : items2)
            {
             Log.d("string is", string);   
            }


          }
      });  

    }
    });  

     alertDialog.show();

//Button to call web service    
      final Button submit = (Button) findViewById(R.id.Button03);
      submit.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v)
          {

            // Perform action on click
              Toast.makeText(display.this,"You have selected to submit data of students",Toast.LENGTH_SHORT).show();
              ProgressDialog dialog = new ProgressDialog(display.this);

              String a=call2(x);
              System.out.println("A is "+a);

              dialog.setMessage("Submitting data...");
              dialog.show();
              if(a.equalsIgnoreCase("success"))
              startActivity(new Intent(display.this,End.class));

          }

      });          



         }

//web service calling method ksoap used 
 public String call2(String x)
 {

    String b=""; 

     SoapObject request = new SoapObject(namespace, method_NAME);      
     request.addProperty("names",x.toString());

     SoapSerializationEnvelope envelope = new  SoapSerializationEnvelope(SoapEnvelope.VER11); 
             envelope.dotNet = true; 
             envelope.setOutputSoapObject(request);

             AndroidHttpTransport android = new AndroidHttpTransport(url);

             android.debug = true; 

             try 
             {

                android.call(soap_ACTION, envelope);

                SoapPrimitive res = (SoapPrimitive) envelope.getResponse();
                SoapObject result = (SoapObject)envelope.getResponse();
                Log.i("myapp",result.toString());
                System.out.println(" --- response ---- " + res); 
                b=result.toString();


                } catch (SocketException ex) { 
                ex.printStackTrace(); 
                } catch (Exception e) { 
                e.printStackTrace(); 
                } 


                return b;       

 }

我的网络服务代码:

  //splitting received array from android app and storing each value in database
         public String getnames(String names)
    {

        SqlConnection myConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=student;User ID=sa;Password=123");

        try
        {
            String[] s = names.Split('#');
            myConnection.Open();

            SqlCommand myCommand = new SqlCommand();
            myCommand.Connection = myConnection;

            foreach (string a in s)
            {

                myCommand.CommandText = "insert into record2(studentnames) values('"+a+"')";
                myCommand.ExecuteNonQuery();


            }


        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        finally
        {
            myConnection.Close();
        }

        return "success";
       }

     }    

请帮我解决问题。谢谢

2 个答案:

答案 0 :(得分:3)

我认为

中的错误是(第236行)
SoapObject result = (SoapObject)envelope.getResponse();

如果是,请尝试使用此解决方案: -

SoapObject result = (SoapObject)envelope.bodyIn;

答案 1 :(得分:2)

选择其中一个。

SoapPrimitive res = (SoapPrimitive) envelope.getResponse();

SoapObject result = (SoapObject)envelope.getResponse();

不是他们两个