0

I'm processing some data in the background using runOnUiThread but the value for variable (ishr) which is being used in processing is getting null at the end.

I tried to debug it, every thing works fine and value is present in the runOnUiThread block but it is null when it comes out of it, is there a way to get the values in mainUI ?

public String ishra="";
TextView ish = (TextView) findViewById(R.id.ish);
runOnUiThread(new Runnable(){
            @Override
            public void run(){
                processdata.in.background(result);
            }
        });

ish.setText(ishra);


process.data.in.background(String match)
{
    if (match=="True"){
        getdatafromhttp();
        processdata(resultfromhttp);
    }
}

private void processdata(String data)
{ 
    try
    { 
        JSONObject json = new JSONObject(data);
        JSONObject a = json.getJSONObject("data"); 
        ishra = a.getString("Surprise");
    }
    catch (JSONException e)
    {
        Log.wtf("log", e);
    }
}
Tigger
  • 8,980
  • 5
  • 36
  • 40
reader
  • 37
  • 1
  • 7
  • 1
    `runOnUiThread` is not background process – Vygintas B Nov 23 '16 at 07:37
  • What does `processdata.in.background...` do? Where is that code? – Tigger Nov 23 '16 at 07:39
  • You appear to have a Context (since you're calling findViewById) so there should be no need to call this as runOnUiThread, you can just invoke `processdata.in.background` directly. – scorpiodawg Nov 23 '16 at 07:40
  • processdata.in.background() is parsing JSON data as i was getting error when i put the code directly, after searching i found this [link](http://stackoverflow.com/questions/6343166/how-to-fix-android-os-networkonmainthreadexception) where they suggested many ways to avoid the error i used this one and there was no error .the problem is the value of ishra is getting null when it comes out of the function runOnUithread – reader Nov 23 '16 at 07:54
  • @Tigger here is the code for the function processdata.in.backgroundconnect.url.and.get.data(url) ; connect.url.and.get.data(url) ; private void processdata(String data) { try { JSONObject json = new JSONObject(data); JSONObject a = json.getJSONObject("data"); ishra = a.getString("Surprise"); } catch (JSONException e) { Log.wtf("log", e); } } on debugi found the value for ishra to be some string but wehn it goes back to mainUI it is getting null – reader Nov 23 '16 at 08:04
  • @Tigger apologies i am new to this forum. i have added the other imp functions to the script – reader Nov 23 '16 at 08:51
  • Try AsyncTask its much easier and convenient . – Rishabh Maurya Nov 23 '16 at 08:59

1 Answers1

0

Personally I would re-write the code to use AsyncTask or make the following changes:

// ishra is not required any more
//public String ishra="";
TextView ish = (TextView) findViewById(R.id.ish);
runOnUiThread(new Runnable(){
        @Override
        public void run(){
            // Set the text here
            ish.setText(processdata.in.background(result));
        }
    });

// Not required any more
//ish.setText(ishra);

// Return a String 
private String process.data.in.background(String match)
{
    private String result = "";
    if (match=="True"){
        // What does getdatafromhttp() do? It may need to be changed.
        getdatafromhttp();
        result = processdata(resultfromhttp);
    }
    return result;
}

private String processdata(String data)
{
    String result = "";
    try
    { 
        JSONObject json = new JSONObject(data);
        JSONObject a = json.getJSONObject("data"); 
        // Get the result
        result = a.getString("Surprise");
    }
    catch (JSONException e)
    {
        Log.wtf("log", e);
    }
    // Return the result String
    return result;
}

The reason why your code is not working is because you are calling ish.setText(ishra); outside of the runOnUiThread() method. This means the String assigned to ishra within the runOnUiThread() is updated after you have called the setText() method.

You could shift ish.setText(ishra); like so as well:

public void run(){
    processdata.in.background(result);
    ish.setText(ishra);
}

And if you code is solid, that should work too.

Tigger
  • 8,980
  • 5
  • 36
  • 40
  • thanks, yours is a good solution. before trying urs i was searching for other solution i found ion[link](https://github.com/koush/ion) by kaushikdutta which is simple solution thanks again for the the reply – reader Nov 26 '16 at 09:00