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 7195413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:34:28+00:00 2026-05-28T20:34:28+00:00

I am currently having trouble connecting to my webservice on android. I am using

  • 0

I am currently having trouble connecting to my webservice on android. I am using a Jetty web service and building it using ANT. On my laptop it works perfectly and displays items from my database. However it won’t seem to connect on my Android application. Attached is the code and LOGCAT report.

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
public class Android2Servlet extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dbtest);
TextView txtresp = (TextView)findViewById(R.id.servlet_response);

String url = "http://(MY LAPTOPS IP):8085/DB";
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
try{
System.out.println("About to execute");
HttpResponse response = client.execute(request);
String helpedResp=HttpUnpack.request(response); 
System.out.println(helpedResp); 
txtresp.setText(helpedResp);
System.out.println(response);
}catch(Exception ex){
txtresp.setText("Failed!");
ex.printStackTrace();
}
} }

The HTTP Unpack File

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
public class HttpUnpack {
public static String request(HttpResponse response){
String result = "";
try{ InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null){
str.append(line + "\n");
}
in.close();
result = str.toString();
}catch(Exception ex){
result = "Error retrieving response";
}
return result;
}
}

The HTTP Unpack and the Android2Servlet do not cause any errors, however the text box only returns “Failed!”.

LOGCAT file

W/System.err(1282): android.os.NetworkOnMainThreadException
W/System.err(1282):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
W/System.err(1282):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
W/System.err(1282):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
W/System.err(1282):     at libcore.io.IoBridge.connect(IoBridge.java:112)
W/System.err(1282):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
W/System.err(1282):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
W/System.err(1282):     at java.net.Socket.connect(Socket.java:842)
W/System.err(1282):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
W/System.err(1282):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
W/System.err(1282):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
W/System.err(1282):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
W/System.err(1282):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
W/System.err(1282):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
W/System.err(1282):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
W/System.err(1282):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
W/System.err(1282):     at myFood.myFood.Android2Servlet.onCreate(Android2Servlet.java:24)
W/System.err(1282):     at android.app.Activity.performCreate(Activity.java:4465)
W/System.err(1282):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
W/System.err(1282):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
W/System.err(1282):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
W/System.err(1282):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
W/System.err(1282):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
W/System.err(1282):     at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err(1282):     at android.os.Looper.loop(Looper.java:137)
W/System.err(1282):     at android.app.ActivityThread.main(ActivityThread.java:4424)
W/System.err(1282):     at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(1282):     at java.lang.reflect.Method.invoke(Method.java:511)
W/System.err(1282):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
W/System.err(1282):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
W/System.err(1282):     at dalvik.system.NativeStart.main(Native Method)

Any response would be greatly helpful.

Regards.

EDIT:

I have now removed the network access and put it in another Java class. And called it form another class. And it is still returning the same errors. Any other advice?

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class androidconnectors {

    public static final String main(String args[]) throws Exception {


String txtresp = "";
try{
    String url = "http://(LAPTOPS IP):8085/Hello";
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(url);
System.out.println("About to execute");
HttpResponse response = client.execute(request);
String helpedResp=HttpUnpack.request(response); // Http Unpack is not part of
System.out.println(helpedResp); // of HttpClient library
txtresp =(helpedResp);
System.out.println(response);
}catch(Exception ex){
    ex.printStackTrace();
}
return txtresp;
}
}

And the android class.

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Settings extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dbtest);
        Button mybutton = (Button) findViewById(R.id.buttonpress);
        mybutton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                TextView txtresp = (TextView)findViewById(R.id.servlet_response);
            String[] args = null;
            try {
                String x = androidconnectors.main(args);
                txtresp.setText(x);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                txtresp.setText("Failed");
            }

            }
        });
}
}
  • 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-05-28T20:34:29+00:00Added an answer on May 28, 2026 at 8:34 pm

    It would seem, based on a quick google search, that since Honeycomb you are not supposed to execute network access on your app’s main thread (to improve responsiveness).

    Create another thread for network access as suggested here.

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

Sidebar

Related Questions

I'm currently building my first app for Android. What I'm having trouble with is
I'm building an android app and I'm currently having trouble retrieving a bitmap from
I'm having trouble with a web service deployed on Tomcat. During peak traffic times
I have currently switched to using PDO but am having trouble in handling the
I'm currently having trouble creating an image from a binary string of data in
I am currently having trouble finding the index/location of a button in a table
I'm currently having trouble with a destructor of a class which contains a vector
The part of the application that I am currently having trouble getting to work
I'm currently having some trouble with getting certain input fields passed along when a
I'm having trouble editing an XML file. I'm currently trying to use Nokogiri ,

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.