Передача строки через вызов веб-службы с использованием ksoap, генерирующего предупреждения - PullRequest
0 голосов
/ 15 ноября 2011

Я реализовал код Android, который вызывает веб-сервис SOAP.Веб-сервис принимает параметр String в форме "abc # bcd # efg #".Когда я передаю такую ​​строку, я получаю предупреждения:

Обратите внимание: 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 ]

3 голосов
/ 16 ноября 2011

Я думаю, что ошибка (строка 236) в

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

Если это так, попробуйте использовать это решение: -

SoapObject result = (SoapObject)envelope.bodyIn;
2 голосов
/ 15 ноября 2011

Выберите один из них.

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

или

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

не оба из них

...