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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:46:21+00:00 2026-06-13T09:46:21+00:00

This is my first android app. I am posting the code I am using

  • 0

This is my first android app. I am posting the code I am using for HTTP connection(not mine, this is from one tutorial site). Problem is when I push the button for downloading text, it shows the progress dialog and the app force closes. I am posting the code.

package com.example.httpconnectsample;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
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.ImageView;
import android.widget.TextView;

public class Httpconnectionsample extends Activity {

private Button getImageButton;
private Button getTextButton;
private ProgressDialog progressDialog;  
private Bitmap bitmap = null;
private String text = null;

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

    getImageButton = (Button)findViewById(R.id.Button01);
    getTextButton = (Button)findViewById(R.id.Button02);

    getImageButton.setOnClickListener( new OnClickListener() {

        @Override
        public void onClick(View v) {
            downloadImage("http://www.android.com/media/wallpaper    /gif/android_logo.gif");

        }
    });

    getTextButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            downloadText("http://xyz.com")
        }
    });
}


private void downloadImage(String urlStr) {
    progressDialog = ProgressDialog.show(this, "", "Fetching Image...");
    final String url = urlStr;

    new Thread() {
        public void run() {
            InputStream in = null;
            Message msg = Message.obtain();
            msg.what = 1;
            try {
                in = openHttpConnection(url);
                bitmap = BitmapFactory.decodeStream(in);
                Bundle b = new Bundle();
                b.putParcelable("bitmap", bitmap);
                msg.setData(b);
                in.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            messageHandler.sendMessage(msg);    

        }
    }.start();

}

private void downloadText(String urlStr) {
    progressDialog = ProgressDialog.show(this, "", "Fetching Text...");
    final String url = urlStr;

    new Thread () {
        public void run() {
            int BUFFER_SIZE = 2000;
            InputStream in = null;
            Message msg = Message.obtain();
            msg.what=2;
            try {
                in = openHttpConnection(url);

                InputStreamReader isr = new InputStreamReader(in);
                int charRead;
                  text = "";
                  char[] inputBuffer = new char[BUFFER_SIZE];

                      while ((charRead = isr.read(inputBuffer))>0)
                      {                    
                          //---convert the chars to a String---
                          String readString = 
                              String.copyValueOf(inputBuffer, 0, charRead);                    
                          text += readString;
                          inputBuffer = new char[BUFFER_SIZE];
                      }
                     Bundle b = new Bundle();
                        b.putString("text", text);
                        msg.setData(b);
                      in.close();

            }catch (IOException e) {
                e.printStackTrace();
            }
            messageHandler.sendMessage(msg);
        }
    }.start();

}

private InputStream openHttpConnection(String urlStr) {
    InputStream in = null;
    int resCode = -1;

    try {
        URL url = new URL(urlStr);
        URLConnection urlConn = url.openConnection();

        if (!(urlConn instanceof HttpURLConnection)) {
            throw new IOException ("URL is not an Http URL");
        }

        HttpURLConnection httpConn = (HttpURLConnection)urlConn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect(); 

        resCode = httpConn.getResponseCode();     


        if (resCode == HttpURLConnection.HTTP_OK) {
            in = httpConn.getInputStream();                                 
        }    

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return in;
}

private Handler messageHandler = new Handler() {

    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what) {
                /*case 1:
            ImageView img = (ImageView) findViewById(R.id.imageview01);
            img.setImageBitmap((Bitmap)(msg.getData().getParcelable("bitmap")));
            break; */
        case 2:
            TextView text = (TextView) findViewById(R.id.textview01);
            text.setText(msg.getData().getString("text"));
            break;
        }
        progressDialog.dismiss();
    }
};
}

following is my XML

   <?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<Button
    android:id="@+id/Button02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="24dp"
    android:text="Button" />

<Button
    android:id="@+id/Button01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="56dp"
    android:text="Button" />

<TextView
    android:id="@+id/textview01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/Button02"
    android:layout_marginTop="39dp"

    tools:context=".Httpconnectionsample" />



</RelativeLayout>
</ScrollView>

following is the Log:-

   10-26 15:22:43.660: D/ddm-heap(23866): Got feature list request
   10-26 15:22:48.930: W/System.err(23866): java.net.SocketException: Permission denied     (maybe missing INTERNET permission)
   10-26 15:22:48.940: W/System.err(23866):     at    org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocketImpl(Native Method)
   10-26 15:22:48.945: W/System.err(23866):     at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocket(OSNetworkSystem.java:187)
   10-26 15:22:48.945: W/System.err(23866):     at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:266)
   10-26 15:22:48.950: W/System.err(23866):     at  java.net.Socket.checkClosedAndCreate(Socket.java:889)
   10-26 15:22:48.950: W/System.err(23866):     at java.net.Socket.connect(Socket.java:1036)
   10-26 15:22:48.950: W/System.err(23866):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>  (HttpConnection.java:62)
  10-26 15:22:48.950: W/System.err(23866):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionManager$ConnectionPool.getHttpConnection(HttpConnectionManager.java:145)
 10-26 15:22:48.950: W/System.err(23866):   at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionManager.getConnection(HttpConnectionManager.java:67)
 10-26 15:22:48.960: W/System.err(23866):   at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getHTTPConnection(HttpURLConnection.java:821)
 10-26 15:22:48.960: W/System.err(23866):   at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:807)
 10-26 15:22:48.965: W/System.err(23866):   at com.example.httpconnectsample.Httpconnectionsample.openHttpConnection(Httpconnectionsample.java:142)
 10-26 15:22:48.965: W/System.err(23866):   at com.example.httpconnectsample.Httpconnectionsample.access$3(Httpconnectionsample.java:126)
 10-26 15:22:48.965: W/System.err(23866):   at com.example.httpconnectsample.Httpconnectionsample$5.run(Httpconnectionsample.java:97)
 10-26 15:22:48.965: W/dalvikvm(23866): threadid=15: thread exiting with uncaught exception (group=0x4001b180)
10-26 15:22:48.995: E/AndroidRuntime(23866): Uncaught handler: thread Thread-8 exiting due to uncaught exception
10-26 15:22:49.090: E/AndroidRuntime(23866): java.lang.NullPointerException
10-26 15:22:49.090: E/AndroidRuntime(23866):    at java.io.Reader.<init>(Reader.java:65)
 10-26 15:22:49.090: E/AndroidRuntime(23866):   at java.io.InputStreamReader.<init>(InputStreamReader.java:65)
10-26 15:22:49.090: E/AndroidRuntime(23866):    at com.example.httpconnectsample.Httpconnectionsample$5.run(Httpconnectionsample.java:99)
10-26 15:22:49.100: I/dalvikvm(23866): threadid=7: reacting to signal 3
10-26 15:22:49.115: I/dalvikvm(23866): Wrote stack trace to '/data/anr/traces.txt'

I tried searching for a fix but could not understand why this is happening. I just need to download text so at present I am ignoring download image part of this code. Also, I added
uses-permission android:name=”android.permission.INTERNET”
just above in manifest but still it is showing permission denied.
I’d be grateful for any help.

  • 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-13T09:46:21+00:00Added an answer on June 13, 2026 at 9:46 am

    Add the INTERNET permission to your manifest file.

    You have to add this line:

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    

    outside the application tag in your AndroidManifest.xml as shown below

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.demo"
    android:versionCode="1"
    android:versionName="1.0" >
    
    <uses-sdk android:minSdkVersion="15" /> 
    
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />   
    
    <application>
        //your stuffs
    </application>
    
    </manifest>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've followed this tutorial: http://anandhansubbiah.com/blog/writing-your-first-android-application/ , but no matter what I do, and what
This is my first android app and I'm a little disapponted because I'm not
This is my first android app. The code that is heavily tied to hardware
Bear with me, as this is my first Android app. :) Essentially, I would
How to create in-app step by step instructions? Like this ( first boot android
Up front: This is my first attempt at an Android app. I'm in that
This is my first Android app. I need to email what I have so
I have published my first android app and its quite different from iPhone. On
I've written my first Android app (Sencha Touch + Phonegap using Eclipse on PC).
This is my first question posting here. I'm novice to Flash builder,using Flash Builder

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.