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

  • Home
  • SEARCH
  • 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 8554429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:52:48+00:00 2026-06-11T14:52:48+00:00

We’ve created a program allowing connection from the android device to a server to

  • 0

We’ve created a program allowing connection from the android device to a server to send and return information. Running on Android 2.3.3 on the emulator, the app works fine.
Once we put it onto our Android 4.0.4 phone, the app crashed when trying to connect to the server.

We stripped the application down just to leave the connection code to try and debug and work out why it was crashing. Same story, on the V4 phone it connected on the V2 emulator it crashed.

We tried a V4.1 emulator and it crashed – yet on a version 2 Samsung tablet it worked.
When connecting to our server through the devices web broswer all versions were able to connect but not through the app.

Thanks for your help

package com.example.servertester;


import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

private Context context = this;

    EditText text, password;
    Button button;
    Toast message;
    String proxyAddress, serverAddress;
    int proxyPort;
    private final HTTPCom HTTPC = new HTTPCom();

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

    text = (EditText) findViewById (R.id.text);
    password = (EditText) findViewById (R.id.password);
    button = (Button) findViewById (R.id.button);
    text.setText("");
    button.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
                String response = "";

                JSONObject jobject = new JSONObject();

                String request = jobject.toString();                
                response = HTTPC.HTTPJSONPost(context, "", "" + request, serverAddress, proxyAddress, proxyPort); //--- connection with proxy
                //response = HTTPC.HTTPJSONPost(context, "", "" + request, serverAddress, null, null); //--- connection without proxy


                message = Toast.makeText(MainActivity.this, response, Toast.LENGTH_LONG);
                message.show();

        }

    });
}

}

Note: just for showing the code on here I have left the ip addresses undefined however in the orginal code they are defined.
We get the same result if we connect via a proxy or not.

    09-11 08:50:33.083: D/AndroidRuntime(662): Shutting down VM
09-11 08:50:33.083: W/dalvikvm(662): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
09-11 08:50:33.123: E/AndroidRuntime(662): FATAL EXCEPTION: main
09-11 08:50:33.123: E/AndroidRuntime(662): android.os.NetworkOnMainThreadException
09-11 08:50:33.123: E/AndroidRuntime(662):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
09-11 08:50:33.123: E/AndroidRuntime(662):  at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
09-11 08:50:33.123: E/AndroidRuntime(662):  at libcore.io.IoBridge.connectErrno(IoBridge.java:144)
09-11 08:50:33.123: E/AndroidRuntime(662):  at libcore.io.IoBridge.connect(IoBridge.java:112)
09-11 08:50:33.123: E/AndroidRuntime(662):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
09-11 08:50:33.123: E/AndroidRuntime(662):  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
09-11 08:50:33.123: E/AndroidRuntime(662):  at java.net.Socket.connect(Socket.java:842)
09-11 08:50:33.123: E/AndroidRuntime(662):  at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
09-11 08:50:33.123: E/AndroidRuntime(662):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
09-11 08:50:33.123: E/AndroidRuntime(662):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
09-11 08:50:33.123: E/AndroidRuntime(662):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
09-11 08:50:33.123: E/AndroidRuntime(662):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
09-11 08:50:33.123: E/AndroidRuntime(662):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
09-11 08:50:33.123: E/AndroidRuntime(662):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:653)
09-11 08:50:33.123: E/AndroidRuntime(662):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
09-11 08:50:33.123: E/AndroidRuntime(662):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
09-11 08:50:33.123: E/AndroidRuntime(662):  at com.example.servertester.HTTPCom.HTTPPost(HTTPCom.java:68)
09-11 08:50:33.123: E/AndroidRuntime(662):  at com.example.servertester.HTTPCom.HTTPJSONPost(HTTPCom.java:27)
09-11 08:50:33.123: E/AndroidRuntime(662):  at com.example.servertester.MainActivity$1.onClick(MainActivity.java:41)
09-11 08:50:33.123: E/AndroidRuntime(662):  at android.view.View.performClick(View.java:4084)
09-11 08:50:33.123: E/AndroidRuntime(662):  at android.view.View$PerformClick.run(View.java:16966)
09-11 08:50:33.123: E/AndroidRuntime(662):  at android.os.Handler.handleCallback(Handler.java:615)
09-11 08:50:33.123: E/AndroidRuntime(662):  at android.os.Handler.dispatchMessage(Handler.java:92)
09-11 08:50:33.123: E/AndroidRuntime(662):  at android.os.Looper.loop(Looper.java:137)
09-11 08:50:33.123: E/AndroidRuntime(662):  at android.app.ActivityThread.main(ActivityThread.java:4745)
09-11 08:50:33.123: E/AndroidRuntime(662):  at java.lang.reflect.Method.invokeNative(Native Method)
09-11 08:50:33.123: E/AndroidRuntime(662):  at java.lang.reflect.Method.invoke(Method.java:511)
09-11 08:50:33.123: E/AndroidRuntime(662):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-11 08:50:33.123: E/AndroidRuntime(662):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-11 08:50:33.123: E/AndroidRuntime(662):  at dalvik.system.NativeStart.main(Native Method)
  • 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-11T14:52:49+00:00Added an answer on June 11, 2026 at 2:52 pm

    NetworkOnMainThreadException occurs when you any server related operation in Main Ui thread.

    Move your server related code in AsyncTask or in Different Thread.

    What Google Says

    The exception that is thrown when an application attempts to perform a
    networking operation on its main thread.This is only thrown for
    applications targeting the Honeycomb SDK or higher. Applications
    targeting earlier SDK versions are allowed to do networking on their
    main event loop threads, but it’s heavily discouraged.

    How to FIX : The problem here is simply that you need to make your web service calls (or what-have-you) on a separate thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
I have a view passing on information from a database: def serve_article(request, id): served_article
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string

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.