-1

I deploy my webservice on web successfull. I can call it from android 2.3 from my application. But it not able to call webservice from my app which is running on android 4.0 it gives null error. I use minimum sdk to API 10

My Code for calling webservice is below

                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                request.addProperty("number", username.getText().toString());
                request.addProperty("password", password.getText().toString());
                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                        SoapEnvelope.VER11);
                envelope.setOutputSoapObject(request);
                envelope.dotNet = true;
                try {
                    Toast.makeText(getBaseContext(), "Wait while connecting server",Toast.LENGTH_LONG).show();
                    HttpTransportSE androidHttpTransport = new HttpTransportSE(
                            URL);
                    androidHttpTransport.call(SOAP_ACTION, envelope);
                    SoapObject result = (SoapObject) envelope.bodyIn;
                    txt.setText("upto result");
                    if (result != null) {
                        String h = String.valueOf(result.getProperty(0));
                        txt.setText(h);
                        if (h.equalsIgnoreCase("true")) {
                            Intent myIntent = new Intent(getBaseContext(),
                                    ContactList.class);
                            myIntent.putExtra("mobileNumber", username
                                    .getText().toString());
                            startActivity(myIntent);
                        } else {
                            Toast.makeText(getBaseContext(), "Not register or invalid password",Toast.LENGTH_LONG).show();
                            txt.setText("Invalid Credential");
                        }
                    } else {
                        txt.setText("Error while connecting server");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    txt.setText("Check internet connection" + e.getMessage());

                }

You Can Download my app from here

What is the error in my code.

Thanks in advance

Sagar Chavan
  • 507
  • 1
  • 7
  • 15
  • which line it gives 'null error'? show us a logcat – Nezam Nov 01 '13 at 04:55
  • 1
    look like the case of `android.os.NetworkOnMainThreadException` read this http://stackoverflow.com/a/6343299/1436931 – Mohsin Naeem Nov 01 '13 at 04:57
  • 1. Read your logcat; 2. Google the Exception; 3. Search Stackoverflow; 4. Post a question here; including logcat. 99% of the questions don't make it past step 3. – 323go Nov 01 '13 at 04:59
  • It give no exception it give error 'null' – Sagar Chavan Nov 01 '13 at 05:13
  • @Nezam `androidHttpTransport.call(SOAP_ACTION, envelope);` This line give me error 'null' and also i am printing error to the TextView if you have Android device 4.0 then please download app from above link and install @Naeem I not getting any exception like `android.os.NetworkOnMainThreadException` Please read the question carefully it works on android device running on 2.3 but not on 4.0 or later – Sagar Chavan Nov 01 '13 at 05:28
  • @SagarChavan `it works on android device running on 2.3 but not on 4.0 ` it is defiantly `NetworkOnMainThread` problem. move your network request in separate thread. – Mohsin Naeem Nov 01 '13 at 06:07

1 Answers1

0

This may be related to the fact that in API 11 and above accessing the net in Main thread is not allowed you may have to use AsyncTask.

Embed your code block in an AsyncTask, that should solve it.

halfer
  • 19,824
  • 17
  • 99
  • 186
Nezam
  • 4,122
  • 3
  • 32
  • 49