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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:58:55+00:00 2026-06-12T17:58:55+00:00

I want to display a progressdialog when a client sends request to the server..the

  • 0

I want to display a progressdialog when a client sends request to the server..the request should be done in background…but i am getting a force close when i use the AsyncTask..Please help ..Thank you

package com.example.client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    public static ProgressDialog Dialog;
    String s1;

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

        findViewById(R.id.button1).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // sendreq();
                new SendUserTask().execute();
            }
        });
    }

    public String sendreq() {
        try {
            // TODO Auto-generated method stub
            SocketAddress sockaddr = new InetSocketAddress("192.168.7.116",
                    9011);
            Socket serversocket = new Socket();
            serversocket.connect(sockaddr, 10000);
            serversocket.setSoTimeout(10000);

            DataOutputStream out = new DataOutputStream(
                    serversocket.getOutputStream());
            out.flush();

            DataInputStream in = new DataInputStream(
                    serversocket.getInputStream());
            out.flush();
            String msg = "";

            msg = "hi";
            out.writeBytes(msg);
            out.flush();
            byte[] message = new byte[100];
            in.read(message);
            s1 = new String(message);
            Toast.makeText(getApplicationContext(), s1, Toast.LENGTH_LONG)
                    .show();

            ((TextView) findViewById(R.id.textView1)).setText(s1);
            in.close();
            out.close();
            serversocket.close();
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), String.valueOf(e),
                    Toast.LENGTH_LONG).show();
        }
        return null;
    }

    private class SendUserTask extends AsyncTask<Void, Void, String> {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            Dialog = ProgressDialog.show(MainActivity.this, "",
                    "Logging In....", true);

            super.onPreExecute();
        }

        protected void onPostExecute(String s) {

            if (Dialog.isShowing())
                Dialog.dismiss();
        }

        @Override
        protected String doInBackground(Void... params) {
            // TODO Auto-generated method stub
            try {
                return sendreq();

            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), String.valueOf(e),
                        Toast.LENGTH_SHORT).show();
            }
            return null;
        }
    }

}
  • 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-12T17:58:56+00:00Added an answer on June 12, 2026 at 5:58 pm

    this ((TextView) findViewById(R.id.textView1)).setText(s1); is executed from the background thread. you cannot access UI elements outside the UI thread.

    You need to move that part to either onProgressUpdate or onPostExecute, or to run it in a runOnUiThread Runnable.

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

Sidebar

Related Questions

hey i want to display a loading progress using ProgressDialog on my aplication, but
I am getting Arabic text from server successfully. Retrieved text I want display in
Is it able to put views to the left, but off-screen? I want display
I want to display tabular type data, but it will not be coming from
I have AsyncTask extend class I want to display progress dialoge so I use
Normally book module automatically generated book navigation but i want display only 2 book
I want display data from database in Listbox...Here is my code, It is not
i want display 1 record from colums zodys , I'm programint in C# I
I have a simple quiz application and I want display a nice timer /
I have db with this table (TableToDo): http://goo.gl/NlTEk I want display all records in

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.