[SOLVED] Can’t able to call .net webservice in android 4.0 it works fine for android 2.3


This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under
CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

Issue

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

Solution

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.

Answered By – Nezam

people found this article helpful. What about you?