Furqan Ahmed
Furqan Ahmed

Reputation: 159

using variables in threading

    new Thread(new Runnable() {
        public void run() {
            String response = null;
            //String res = null;
            try {
                response = CustomHttpClient.executeHttpPost("http://abc.org/fypcps/furqan.php");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                //Toast.makeText(context, e.toString(), 0).show();
                //Log.i("furqan", "ya ALLAH madad");
            }  //Enetr Your remote PHP,ASP, Servlet file link  
            String res = response.toString();  
            // res = res.trim();  
            //res= res.replaceAll("\\s+","");  
            //error.setText(res);  
            //if(res.equals("1"))  
                //Log.i("furqan1", res);
            //else  
                //Log.i("furqan2", "no string is captured");
            response1 = res;
        }
      }).start();

here in my code response1 is a variable that i have declared above the thread and i want to assign a string "res" to "response1" but it is giving me enclosed type error.can any body help me out with this problem.

Upvotes: 1

Views: 4446

Answers (3)

Anis H
Anis H

Reputation: 1150

Another method is to declare your String variable outside of the Thread as a final Array, so you can assign the value you want inside the thread.

  final String [] answer = new String[1];      
  answer[0]="init value";
  System.out.println(answer[0]); // return "init value"
  new Thread(new Runnable() {
      public void run() {

          answer[0]="value assigned from the thread";
      }
    }).start();

  try {
    Thread.currentThread().sleep(1000);
  } catch (InterruptedException e) {
    e.printStackTrace();
  }
  System.out.println(answer[0]); //return "value assigned from the thread"

Upvotes: 4

Thomas Antony
Thomas Antony

Reputation: 584

I think you can sent the value of an instance variable from within a thread. Something like

package pro.Project;

import android.app.Activity;
import android.os.Bundle;

public class ProjectActivity extends Activity {
    /** Called when the activity is first created. */
    private String foo = "";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String bar = ""; // Non-final local variable

        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                foo ="bar"; // This would work
                bar ="baz"; // This WOULDN'T. As 'bar' is a non-final local variable
            }
        });
        t.start();
    }
}

Upvotes: 0

Tudor
Tudor

Reputation: 62439

You cannot assign something to a variable from inside an anonymous class because java requires variables to be final if used in this scenario (to prevent unwanted side-effects).

You could try to solve this by creating a wrapper for your variable:

class StringWrapper {
    public String innerString;
}

final StringWrapper wrapper = new StringWrapper();

new Thread(new Runnable() {
    public void run() {
         ...
         wrapper.innerString = res;
    }
});

Upvotes: 0

Related Questions