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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:15:18+00:00 2026-06-15T00:15:18+00:00

My activity gets force closed when there is no internet connection.How do I prevent

  • 0

My activity gets force closed when there is no internet connection.How do I prevent this? I am thinking of showing a message to the user that there is no internet connection if it is not connected.In which all ways should I change the code? Thanks in advance

public class Myclass extends BaseActivity {

private String[] imageUrls;

private DisplayImageOptions options;

private InputStream is;

private String result;

@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.ac_image_grid);


    imageUrls = new String[1000];

    try{

        HttpClient httpclient = new DefaultHttpClient();

        HttpPost httppost = new HttpPost(D.url+"gal.php");

        List nameValuepairss = new ArrayList(1);
        // adding attributes to List 
        nameValuepairss.add(new BasicNameValuePair("type","gal_living"));

       httppost.setEntity(new UrlEncodedFormEntity(nameValuepairss));
        HttpResponse response = httpclient.execute(httppost); 
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
        Log.e("log_tag", "connection success username");

}catch(Exception e){
        Log.e("log_tag", "Error in http connection "+e.toString());
        Toast.makeText(getApplicationContext(), "fail1", Toast.LENGTH_SHORT).show();

}
//convert response to string
try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");

        }
        is.close();

        result=sb.toString();
}catch(Exception e){

       Log.e("log_tag", "Error converting result :::"+e.toString());
   // Toast.makeText(getApplicationContext(), "fail1", Toast.LENGTH_SHORT).show();

}
try{


    JSONArray jArray = new JSONArray(result);
    int arrayLength=jArray.length();
    final String url[]=new String[arrayLength];

    for(int i=0;i<arrayLength;i++){
           JSONObject json_data = jArray.getJSONObject(i);



          url[i]=json_data.getString("url");
    }
    List<String> urls = new ArrayList<String>();
    urls.addAll(Arrays.asList(url));
    imageUrls = urls.toArray(new String[0]);

} catch(JSONException e){
    Log.e("log_tag", "Error parsing data:::::again? "+e.toString());
    }   options = new DisplayImageOptions.Builder()
        .showStubImage(R.drawable.stub_image)
        .showImageForEmptyUri(R.drawable.image_for_empty_url)
        .cacheInMemory()
        .cacheOnDisc()
        .build();
    GridView gridView = (GridView) findViewById(R.id.gridview);
    gridView.setAdapter(new ImageAdapter());
    gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            startImageGalleryActivity(position);
        }
    });
}

private void startImageGalleryActivity(int position) {
    Intent intent = new Intent(this, ImagePagerActivity.class);
    intent.putExtra(Extra.IMAGES, imageUrls);
    intent.putExtra(Extra.IMAGE_POSITION, position);
    startActivity(intent);
}

public class ImageAdapter extends BaseAdapter {
    @Override
    public int getCount() {
        return imageUrls.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ImageView imageView;
        if (convertView == null) {
            imageView = (ImageView) getLayoutInflater().inflate(R.layout.item_grid_image, parent, false);
        } else {
            imageView = (ImageView) convertView;
        }

        imageLoader.displayImage(imageUrls[position], imageView, options, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(Bitmap loadedImage) {
                Animation anim = AnimationUtils.loadAnimation(LivingGrid.this, R.anim.fade_in);
                imageView.setAnimation(anim);
                anim.start();
            }
        });

        return imageView;
    }}}
  • 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-15T00:15:20+00:00Added an answer on June 15, 2026 at 12:15 am

    I think you can just put a return in the first catch clause. Because although you have caught Http Connection exception, the below code would still be executed. At that time, “is” is null, so reading from “is” would cause crash.

    try{
    
        HttpClient httpclient = new DefaultHttpClient();
    
        HttpPost httppost = new HttpPost(D.url+"gal.php");
    
        List nameValuepairss = new ArrayList(1);
        // adding attributes to List 
        nameValuepairss.add(new BasicNameValuePair("type","gal_living"));
    
       httppost.setEntity(new UrlEncodedFormEntity(nameValuepairss));
        HttpResponse response = httpclient.execute(httppost); 
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
        Log.e("log_tag", "connection success username");
    
    }catch(Exception e){
        Log.e("log_tag", "Error in http connection "+e.toString());
        Toast.makeText(getApplicationContext(), "fail1", Toast.LENGTH_SHORT).show();
    
        return; // ADD return HERE!!!!!!!!!!!!
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have an Activity that when it gets displayed a Notification will also
I'm trying to create a searchable activity that gets its results from the google
I am facing this problem that buttons in android are not clickable when given
I have activity indicator in alert view that I use until my app gets
Is there a way to notify an activity/service of a force-close request right before
I have a MYSQL 'activity' table where all activity by a user gets logged.
I'm using TabActivity with Custom Title. TabControl.xml contains android:focusable=true android:focusableInTouchMode=true When activity gets launched,
Activity GameActivity : The page that's opened when the application is first opened. Activity
This activity is used for login. It post the username and password to a
My Activity creates an AsyncTask that loads data from a file and updates the

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.