-1

我成功地在网络上部署了我的网络服务。我可以从我的应用程序中的 android 2.3 调用它。但它无法从我在 android 4.0 上运行的应用程序调用 web 服务,它给出了 null 错误。我使用最小 SDK 到 API 10

我调用网络服务的代码如下

                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());

                }

你可以从这里下载我的应用程序

我的代码有什么错误。

提前致谢

4

1 回答 1

0

这可能与在 apis 11 及更高版本中不允许在主线程中访问网络有关,您可能必须使用AsyncTask.

将您的代码块嵌入AsyncTask到应该可以解决的问题中。

学会做一个适当的谷歌搜索

希望能解决您的疑问。

于 2013-11-01T06:03:39.810 回答