简体   繁体   中英

Android using ksoap2 to connect to webservice failing

So im having abit of an issue with connecting to the w3schools webservice.I am trying to get a feel for implementing webservices with the ksoap2 library in android studio.I have hardcoded a value to return something as soon as the screen loads but I dont think I am hitting the webservice.Please assist.

The manifest file >

AndroidManifest.xml

        <?xml version="1.0" encoding="utf-8"?>
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
            package="com.example.mjivan.webservicestut" >

            <application

                android:allowBackup="true"
                android:icon="@mipmap/ic_launcher"
                android:label="@string/app_name"

                android:supportsRtl="true"
                android:theme="@style/AppTheme" >
                <activity android:name=".MainActivity" >
                    <intent-filter>
                        <action android:name="android.intent.action.MAIN" />

                        <category android:name="android.intent.category.LAUNCHER" />
                    </intent-filter>
                </activity>
            </application>
            <uses-permission android:name="android.permission.INTERNET"></uses-permission>
        </manifest>

MainActivity.java

      package com.example.mjivan.webservicestut;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.ksoap2.transport.HttpTransportSE;

public class MainActivity extends Activity {

    private TextView txt;
    private String celsius;
    private static final String SOAP_ACTION = "https://www.w3schools.com/xml/tempconvert.asmx?op=CelsiusToFahrenheit";
    private static final String METHOD_NAME = "CelsiusToFahrenheit";
    private static final String NAMESPACE = "https://www.w3schools.com/";
    private static final String URL = "https://www.w3schools.com/xml/tempconvert.asmx";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        final EditText edt = (EditText) findViewById(R.id.value_to_convert);
        Button btn = (Button) findViewById(R.id.convert);
        txt = (TextView) findViewById(R.id.answer);
    }

        private class LongOperation extends AsyncTask<String, Void,String> {

            @Override
            protected String doInBackground(String... params) {
                SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
                Request.addProperty("Celsius", "32");


                SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                soapEnvelope.dotNet = true;
                soapEnvelope.setOutputSoapObject(Request);

                HttpTransportSE aht = new HttpTransportSE(URL);
                try {
                    aht.call(SOAP_ACTION, soapEnvelope);
                    SoapPrimitive resultString = (SoapPrimitive) soapEnvelope.getResponse();
                    return "" + resultString;
                    //txt.setText("Status :" + resultString);
                } catch (Exception e) {

                    return null;
                }
            }
                @Override
                protected void onPostExecute (String result)
                {
                    TextView txt = (TextView) findViewById(R.id.answer);
                    txt.setText(result); // txt.setText(result);
                    // might want to change "executed" for the returned string passed
                    // into onPostExecute() but that is upto you
                }

                @Override
                protected void onPreExecute()
                {

                }


                @Override
                protected void onProgressUpdate (Void...values)
                {

                }

        }



}

activity_main.xml

  //layout file

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/value_to_convert" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Convert"
            android:id="@+id/convert"
            android:layout_gravity="center_horizontal" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/answer"
            android:textColor="@color/background_floating_material_dark" />
    </LinearLayout>

Logcat

      03-14 11:47:41.051 15992-15992/com.example.mjivan.webservicestut I/InjectionManager: Inside getClassLibPath caller 
03-14 11:47:41.051 15992-15992/com.example.mjivan.webservicestut W/System: ClassLoader referenced unknown path: /data/app/com.example.mjivan.webservicestut-1/lib/arm64
03-14 11:47:41.061 15992-15992/com.example.mjivan.webservicestut W/ResourcesManager: getTopLevelResources: /data/app/com.example.mjivan.webservicestut-1/base.apk / 1.0 running in com.example.mjivan.webservicestut rsrc of package com.example.mjivan.webservicestut
03-14 11:47:41.061 15992-15992/com.example.mjivan.webservicestut I/InjectionManager: Inside getClassLibPath + mLibMap{0=, 1=}
03-14 11:47:41.061 15992-15992/com.example.mjivan.webservicestut D/ResourcesManager: For user 0 new overlays fetched Null
03-14 11:47:43.191 15992-15992/com.example.mjivan.webservicestut W/System: ClassLoader referenced unknown path: /data/app/com.example.mjivan.webservicestut-1/lib/arm64
03-14 11:47:43.191 15992-15992/com.example.mjivan.webservicestut D/Minikin: FontFamily bestFont == NULL, so return vacant FakedFont
03-14 11:47:43.191 15992-15992/com.example.mjivan.webservicestut D/Minikin: FontFamily bestFont == NULL, so return vacant FakedFont
03-14 11:47:43.191 15992-15992/com.example.mjivan.webservicestut D/Minikin: FontFamily bestFont == NULL, so return vacant FakedFont
03-14 11:47:43.191 15992-15992/com.example.mjivan.webservicestut D/Minikin: FontFamily bestFont == NULL, so return vacant FakedFont
03-14 11:47:43.191 15992-15992/com.example.mjivan.webservicestut D/Minikin: FontFamily bestFont == NULL, so return vacant FakedFont
03-14 11:47:43.191 15992-15992/com.example.mjivan.webservicestut D/Minikin: FontFamily bestFont == NULL, so return vacant FakedFont
03-14 11:47:43.201 15992-15992/com.example.mjivan.webservicestut D/InjectionManager: InjectionManager
03-14 11:47:43.201 15992-15992/com.example.mjivan.webservicestut D/InjectionManager: fillFeatureStoreMap com.example.mjivan.webservicestut
03-14 11:47:43.201 15992-15992/com.example.mjivan.webservicestut I/InjectionManager: Constructor com.example.mjivan.webservicestut, Feature store :{}
03-14 11:47:43.201 15992-15992/com.example.mjivan.webservicestut I/InjectionManager: featureStore :{}
03-14 11:47:43.211 15992-15992/com.example.mjivan.webservicestut W/ResourcesManager: getTopLevelResources: /data/app/com.example.mjivan.webservicestut-1/base.apk / 1.0 running in com.example.mjivan.webservicestut rsrc of package com.example.mjivan.webservicestut
03-14 11:47:43.211 15992-15992/com.example.mjivan.webservicestut W/ResourcesManager: getTopLevelResources: /data/app/com.example.mjivan.webservicestut-1/base.apk / 1.0 running in com.example.mjivan.webservicestut rsrc of package com.example.mjivan.webservicestut
03-14 11:47:43.251 15992-15992/com.example.mjivan.webservicestut D/ClipboardExManager: no knox
03-14 11:47:43.261 15992-15992/com.example.mjivan.webservicestut D/Activity: performCreate Call Injection manager
03-14 11:47:43.261 15992-15992/com.example.mjivan.webservicestut I/InjectionManager: dispatchOnViewCreated > Target : com.example.mjivan.webservicestut.MainActivity isFragment :false
03-14 11:47:43.271 15992-15992/com.example.mjivan.webservicestut D/SecWifiDisplayUtil: Metadata value : SecSettings2
03-14 11:47:43.271 15992-15992/com.example.mjivan.webservicestut D/ViewRootImpl: #1 mView = com.android.internal.policy.PhoneWindow$DecorView{b756f4e I.E...... R.....ID 0,0-0,0}
03-14 11:47:43.271 15992-16417/com.example.mjivan.webservicestut D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
03-14 11:47:43.351 15992-16417/com.example.mjivan.webservicestut D/libEGL: loaded /vendor/lib64/egl/libGLES_mali.so
03-14 11:47:43.361 15992-16417/com.example.mjivan.webservicestut D/libEGL: eglInitialize EGLDisplay = 0x7f990ff178
03-14 11:47:43.361 15992-16417/com.example.mjivan.webservicestut I/OpenGLRenderer: Initialized EGL, version 1.4

                                                                                   [ 03-14 11:47:43.371 15992:16417 D/         ]
                                                                                   ro.exynos.dss isEnabled: 0
03-14 11:47:43.371 15992-16417/com.example.mjivan.webservicestut D/mali_winsys: new_window_surface returns 0x3000,  [1440x2560]-format:1
03-14 11:47:43.391 15992-15992/com.example.mjivan.webservicestut W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
03-14 11:47:43.391 15992-15992/com.example.mjivan.webservicestut W/DisplayListCanvas: DisplayListCanvas is started on unbinded RenderNode (without mOwningView)
03-14 11:47:43.391 15992-16417/com.example.mjivan.webservicestut D/libGLESv1: DTS_GLAPI : DTS is not allowed for Package : com.example.mjivan.webservicestut
03-14 11:47:43.481 15992-15992/com.example.mjivan.webservicestut D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 96 - 0, 0) vi=Rect(0, 96 - 0, 0) or=1
03-14 11:47:43.501 15992-15992/com.example.mjivan.webservicestut I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@a39acb5 time:30405778

In Your code you have not used Any Thread or AsyncTask to perform your Network Operation. that's why you have getting error NetworkOnMainThreadException

Add your part of code i put bellow in AsyncTask

 private class LongOperation extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
                Request.addProperty("Celsius", "32");


                SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                soapEnvelope.dotNet = true;
                soapEnvelope.setOutputSoapObject(Request);

                HttpTransportSE aht = new HttpTransportSE(URL);
                try {
                    aht.call(SOAP_ACTION, soapEnvelope);
                    SoapPrimitive resultString = (SoapPrimitive) soapEnvelope.getResponse();
            return ""+resultString;
                    txt.setText("Status :" + resultString);
                } catch (Excep

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            TextView txt = (TextView) findViewById(R.id.output);
            txt.setText("Executed"); // txt.setText(result);
            // might want to change "executed" for the returned string passed
            // into onPostExecute() but that is upto you
        }

        @Override
        protected void onPreExecute() {}

        @Override
        protected void onProgressUpdate(Void... values) {}
    }

To use AsyncTask you must subclass it. AsyncTask uses generics and varargs. The parameters are the following AsyncTask <TypeOfVarArgParams, ProgressValue, ResultValue> .

An AsyncTask is started via the execute() method. This execute() method calls the doInBackground() and the onPostExecute() method.

TypeOfVarArgParams is passed into the doInBackground() method as input. ProgressValue is used for progress information and ResultValue must be returned from doInBackground() method. This parameter is passed to onPostExecute() as a parameter.

The doInBackground() method contains the coding instruction which should be performed in a background thread. This method runs automatically in a separate Thread.

The onPostExecute() method synchronizes itself again with the user interface thread and allows it to be updated. This method is called by the framework once the doInBackground() method finishes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM