Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8720721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:06:00+00:00 2026-06-13T07:06:00+00:00

Possible Duplicate: Android HttpClient : NetworkOnMainThreadException Im having troubles to create an android app

  • 0

Possible Duplicate:
Android HttpClient : NetworkOnMainThreadException

Im having troubles to create an android app which suppose to be a client for a web service
I have created in java (eclipse).
It is really basic but still it doesn’t work..

The web service class:

package com.ws;

public class Converter {

    public Converter () {
    }

    public String sayHello(String name) {
        return "Hello " + name + "!"; 
    }
}

The class in the android app: (I used this tutorial for that)

package MyConverter.Android;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class access extends Activity {

    private String METHOD_NAME = "";
    // our webservice method name
    private String NAMESPACE = "http://ws.com";
    // Here package name in webservice with reverse order.
    private String SOAP_ACTION = NAMESPACE + METHOD_NAME;
    // NAMESPACE + method name
    // private static final String URL =
    private static final String URL = "http://my-ip:8080/ConverterWebSrvice/services/Converter";

    // you must use ipaddress here, don’t use Hostname or localhost

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.access);

        Button b2 = (Button) findViewById(R.id.button2);
        b2.setOnClickListener(new OnClickListener(){ 
            public void onClick(View v){      
                ws();
            }});
        }

    public void ws() {
        String name = "Dan";

        METHOD_NAME = "sayHello";
        try {
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
            request.addProperty("name", name);
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            androidHttpTransport.call(SOAP_ACTION, envelope);
            Object result = envelope.getResponse();
            ((TextView) findViewById(R.id.textView1)).setText(name
                    + " Longitude is : " + result.toString());

        } catch (Exception E) {
            E.printStackTrace();
            ((TextView) findViewById(R.id.textView1)).setText("ERROR:"
                    + E.getClass().getName() + ":" + E.getMessage());
        }
    }
}

This is how I run the whole thing:
1. I do ‘Run as > run on server’ for the Converter project
2. I do ‘Run as > android application’ for the app project

The emulator runs and when I press the button I got the “Unfortunately” message.
In LogCat (in eclipse) I see the following messages:

10-19 22:29:41.250: W/System.err(643): android.os.NetworkOnMainThreadException
10-19 22:29:41.260: W/System.err(643):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
10-19 22:29:41.270: W/System.err(643):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
10-19 22:29:41.270: W/System.err(643):  at libcore.io.IoBridge.connectErrno(IoBridge.java:144)
10-19 22:29:41.270: W/System.err(643):  at libcore.io.IoBridge.connect(IoBridge.java:112)
10-19 22:29:41.270: W/System.err(643):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
10-19 22:29:41.280: W/System.err(643):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
10-19 22:29:41.280: W/System.err(643):  at java.net.Socket.connect(Socket.java:842)
10-19 22:29:41.290: W/System.err(643):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:76)
10-19 22:29:41.290: W/System.err(643):  at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
10-19 22:29:41.300: W/System.err(643):  at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
10-19 22:29:41.300: W/System.err(643):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
10-19 22:29:41.300: W/System.err(643):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
10-19 22:29:41.300: W/System.err(643):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
10-19 22:29:41.310: W/System.err(643):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
10-19 22:29:41.310: W/System.err(643):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
10-19 22:29:41.320: W/System.err(643):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
10-19 22:29:41.320: W/System.err(643):  at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
10-19 22:29:41.320: W/System.err(643):  at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:188)
10-19 22:29:41.330: W/System.err(643):  at org.ksoap2.transport.ServiceConnectionSE.openOutputStream(ServiceConnectionSE.java:109)
10-19 22:29:41.330: W/System.err(643):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:157)
10-19 22:29:41.350: W/System.err(643):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:96)
10-19 22:29:41.350: W/System.err(643):  at MyConverter.Android.access.ws(access.java:53)
10-19 22:29:41.350: W/System.err(643):  at MyConverter.Android.access$1.onClick(access.java:37)
10-19 22:29:41.360: W/System.err(643):  at android.view.View.performClick(View.java:4084)
10-19 22:29:41.360: W/System.err(643):  at android.view.View$PerformClick.run(View.java:16966)
10-19 22:29:41.370: W/System.err(643):  at android.os.Handler.handleCallback(Handler.java:615)
10-19 22:29:41.380: W/System.err(643):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-19 22:29:41.390: W/System.err(643):  at android.os.Looper.loop(Looper.java:137)
10-19 22:29:41.390: W/System.err(643):  at android.app.ActivityThread.main(ActivityThread.java:4745)
10-19 22:29:41.402: W/System.err(643):  at java.lang.reflect.Method.invokeNative(Native Method)
10-19 22:29:41.402: W/System.err(643):  at java.lang.reflect.Method.invoke(Method.java:511)
10-19 22:29:41.410: W/System.err(643):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-19 22:29:41.410: W/System.err(643):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-19 22:29:41.410: W/System.err(643):  at dalvik.system.NativeStart.main(Native Method)
10-19 22:29:41.420: D/AndroidRuntime(643): Shutting down VM
10-19 22:29:41.420: W/dalvikvm(643): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
10-19 22:29:41.450: E/AndroidRuntime(643): FATAL EXCEPTION: main
10-19 22:29:41.450: E/AndroidRuntime(643): java.lang.NullPointerException
10-19 22:29:41.450: E/AndroidRuntime(643):  at MyConverter.Android.access.ws(access.java:60)
10-19 22:29:41.450: E/AndroidRuntime(643):  at MyConverter.Android.access$1.onClick(access.java:37)
10-19 22:29:41.450: E/AndroidRuntime(643):  at android.view.View.performClick(View.java:4084)
10-19 22:29:41.450: E/AndroidRuntime(643):  at android.view.View$PerformClick.run(View.java:16966)
10-19 22:29:41.450: E/AndroidRuntime(643):  at android.os.Handler.handleCallback(Handler.java:615)
10-19 22:29:41.450: E/AndroidRuntime(643):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-19 22:29:41.450: E/AndroidRuntime(643):  at android.os.Looper.loop(Looper.java:137)
10-19 22:29:41.450: E/AndroidRuntime(643):  at android.app.ActivityThread.main(ActivityThread.java:4745)
10-19 22:29:41.450: E/AndroidRuntime(643):  at java.lang.reflect.Method.invokeNative(Native Method)
10-19 22:29:41.450: E/AndroidRuntime(643):  at java.lang.reflect.Method.invoke(Method.java:511)
10-19 22:29:41.450: E/AndroidRuntime(643):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-19 22:29:41.450: E/AndroidRuntime(643):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-19 22:29:41.450: E/AndroidRuntime(643):  at dalvik.system.NativeStart.main(Native Method)
10-19 22:29:44.060: I/Process(643): Sending signal. PID: 643 SIG: 9

Any help would appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:06:01+00:00Added an answer on June 13, 2026 at 7:06 am

    You are doing network accesses in you UI/Main thread. Start looking at Android’s AsyncTask. Did a quick update on your code to include an AsyncTask which might need updating depending on your needs.

    public class LoginAcitivity extends Activity {
    
        private String METHOD_NAME = "";
        // our webservice method name
        private String NAMESPACE = "http://ws.com";
        // Here package name in webservice with reverse order.
        private String SOAP_ACTION = NAMESPACE + METHOD_NAME;
        // NAMESPACE + method name
        // private static final String URL =
        private static final String URL = "http://my-ip:8080/ConverterWebSrvice/services/Converter";
    
        // you must use ipaddress here, don’t use Hostname or localhost
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.access);
    
            Button b2 = (Button) findViewById(R.id.button2);
            b2.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    AccessWebServiceTask task = new AccessWebServiceTask();
                    task.execute();
                }
            });
        }
    
        public void ws() {
            String name = "Dan";
    
            METHOD_NAME = "sayHello";
            try {
                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                request.addProperty("name", name);
                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
                envelope.dotNet = true;
                envelope.setOutputSoapObject(request);
                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                androidHttpTransport.call(SOAP_ACTION, envelope);
                Object result = envelope.getResponse();
                ((TextView) findViewById(R.id.textView1)).setText(name
                    + " Longitude is : " + result.toString());
    
            } catch (Exception E) {
                E.printStackTrace();
                ((TextView) findViewById(R.id.textView1)).setText("ERROR:"
                    + E.getClass().getName() + ":" + E.getMessage());
            }
        }
    
        class AccessWebServiceTask extends AsyncTask<Void, Void, Void> {
    
            @Override
            protected Void doInBackground(Void... params) {
                ws();
                return null;
            }
    
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Android HttpClient and HTTPS I want to write app on Android which
Possible Duplicate: Android HttpClient : NetworkOnMainThreadException I have a php script on a server
Possible Duplicate: Android HttpClient : NetworkOnMainThreadException I am working on an application that is
Possible Duplicate: Android create shortcuts on the home screen I am having one TextView
Possible Duplicate: how to call RESTful web service from android? This is my web
Possible Duplicate: Android, QR code library NOT ZXING I want to create an app
Possible Duplicate: Android: How to create slide (on/off) button I want to create sliding
Possible Duplicate: Android Launch an application from another application I am having a problem
Possible Duplicate: Android Layout and positioning issue Design layout which support for all kind
Possible Duplicate: Android: How can I get the current foreground activity (from a service)?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.