Passing string via web service call using ksoap generating warnings

只愿长相守 提交于 2019-12-08 10:30:12

问题


I have implemented an Android code that calls a SOAP web service. The web service takes a String parameter in the form "abc#bcd#efg#". When I pass such a String I get warnings as follows:

Note this: ArrayList items2 = new ArrayList();

From 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 

Due to this the application runs but still I do not get the response intended

My Android code:

 //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;       

 }

my web service code:

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

     }    

Please help me solve the problem. Thanks


回答1:


I think the error is (line 236) in

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

If so then try using this solution:-

SoapObject result = (SoapObject)envelope.bodyIn;



回答2:


Choose one of them.

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

or

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

not both of them



来源:https://stackoverflow.com/questions/8136217/passing-string-via-web-service-call-using-ksoap-generating-warnings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!