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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:46:35+00:00 2026-06-05T22:46:35+00:00

I’m trying to use a simple HTTP get to load a string from a

  • 0

I’m trying to use a simple HTTP get to load a string from a webpage. However, when I run the app in the emulator, I get a strict policy violation, and nothing is displayed. The policy violation is listed as “policy=31 violation=4”. If I add a permitNetwork() into the ThreadPolicy initialization, the LogCat tells me that google.com doesn’t resolve to an address. Clearly, I’m missing something, but I was under the impression that this is how I should be handling network operations.

EDIT: I’ve altered my HttpExampleActivity.java, I now have this:

package com.android.httpexample;

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.os.StrictMode.ThreadPolicy.Builder;
import android.widget.TextView;

public class HttpExampleActivity extends Activity {
    TextView httpStuff;

     @Override
        public void onCreate(Bundle savedInstanceState) {
         StrictMode.ThreadPolicy policy = new Builder().detectAll().penaltyLog().build(); 
         StrictMode.setThreadPolicy(policy);
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            new Thread(new Runnable() { 
                public void run() {

                    httpStuff = (TextView) findViewById(R.id.tvHttp);
                    httpStuff.post(new Runnable(){
                        public void run(){

                    GetProcedure test = new GetProcedure();
                    String returned;
                    try{
                        returned = test.getInternetData();
                        httpStuff.setText(returned);
                    } catch(Exception e){
                        e.printStackTrace();
                    }
                        }
                    });
                }
            }).start();


     }
}

In my GetProcedure.java, I have this:

package com.android.httpexample;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;

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 GetProcedure {
    public String getInternetData() throws Exception {
        BufferedReader in = null;
        String data = null;
        try{
            HttpClient client = new DefaultHttpClient();
            URI website = new URI("http://google.com");
            HttpGet request = new HttpGet();
            request.setURI(website);
            HttpResponse response = client.execute(request);
            in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String l = "";
            String nl = System.getProperty("line.separator");
            while((l = in.readLine()) != null)
            {
                sb.append(l + nl);
            }
            in.close();
            data = sb.toString();
            return data;
        }finally{
            if(in != null){
                try{
                    in.close();
                    return data;
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
}
  • 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-05T22:46:36+00:00Added an answer on June 5, 2026 at 10:46 pm

    You need to use a seperate Thread that executes the network activities. Android doesn’t allow networking in the UI-thread since ICS and did raiss a force close after approx. 5 seconds before.

    A good way to solve your task would be an AsyncTask that handles everying or a Thread with a listener for your Activity.

    Check the Painless Threading Topic for more advice.

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

Sidebar

Related Questions

I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
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'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.