1
votes

Data Are show in the logcat but not show in the text view.. what can i do

This is my Log

12-29 11:05:13.908: D/gralloc_goldfish(772): Emulator without GPU emulation detected.
12-29 11:05:24.649: D/dalvikvm(772): GC_FOR_ALLOC freed 195K, 9% free 7439K/8135K, paused 35ms, total 38ms
12-29 11:05:26.458: D/dalvikvm(772): GC_CONCURRENT freed 229K, 10% free 7628K/8455K, paused 26ms+99ms, total 185ms
12-29 11:05:27.908: D/Create Response(772): {"message":"Login successfully.","success":1}
12-29 11:05:28.598: I/Choreographer(772): Skipped 52 frames!  The application may be doing too much work on its main thread.
12-29 11:05:29.848: D/Create Response(772): {"3":"[email protected]","2":"myname","1":"msnmsn","0":"8527801400","mobile_number":"8527801400","hint":"myname","email_id":"[email protected]","password":"msnmsn"}
12-29 11:05:29.848: I/JSON parse(772): myname
12-29 11:05:29.878: W/System.err(772): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
12-29 11:05:29.878: W/System.err(772):  at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4609)
12-29 11:05:29.878: W/System.err(772):  at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:835)
12-29 11:05:29.878: W/System.err(772):  at android.view.View.requestLayout(View.java:15129)
12-29 11:05:29.878: W/System.err(772):  at android.view.View.requestLayout(View.java:15129)
12-29 11:05:29.878: W/System.err(772):  at android.view.View.requestLayout(View.java:15129)
12-29 11:05:29.888: W/System.err(772):  at android.view.View.requestLayout(View.java:15129)
12-29 11:05:29.888: W/System.err(772):  at android.view.View.requestLayout(View.java:15129)
12-29 11:05:29.888: W/System.err(772):  at android.widget.TextView.checkForRelayout(TextView.java:6303)
12-29 11:05:29.888: W/System.err(772):  at android.widget.TextView.setText(TextView.java:3547)
12-29 11:05:29.888: W/System.err(772):  at android.widget.TextView.setText(TextView.java:3405)
12-29 11:05:29.888: W/System.err(772):  at android.widget.TextView.setText(TextView.java:3380)
12-29 11:05:29.898: W/System.err(772):  at com.example.phonebook.Welcome$loginAccess.doInBackground(Welcome.java:93)
12-29 11:05:29.898: W/System.err(772):  at com.example.phonebook.Welcome$loginAccess.doInBackground(Welcome.java:1)
12-29 11:05:29.898: W/System.err(772):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-29 11:05:29.898: W/System.err(772):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-29 11:05:29.898: W/System.err(772):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-29 11:05:29.898: W/System.err(772):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-29 11:05:29.908: W/System.err(772):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-29 11:05:29.908: W/System.err(772):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-29 11:05:29.908: W/System.err(772):  at java.lang.Thread.run(Thread.java:856)

This is my Welcome Activity


    package com.example.phonebook;

    import java.util.ArrayList;
    import java.util.List;

    import org.apache.http.NameValuePair;
    import org.apache.http.message.BasicNameValuePair;
    import org.json.JSONObject;



    import android.app.Activity;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.os.StrictMode;
    import android.util.Log;
    import android.widget.TextView;

    public class Welcome extends Activity {
    //  private ProgressDialog pDialog;

        TextView jsonParsed1,DataShow,hint_data;

        JSONParser jsonParser = new JSONParser();
        JSONParser jobj = new JSONParser();
        private static String url = "http://192.168.1.10/android_connect/login_setup/show_data.php";

        //private static final String about = "about"; 
        //  private static final String TAG_SUCCESS="success";
        //  private static final String hint="hint";

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
            .detectDiskReads().detectDiskWrites().detectNetwork()
            .penaltyLog().build());
            super.onCreate(savedInstanceState);
            setContentView(R.layout.welcome);

            jsonParsed1=(TextView)findViewById(R.id.jsonParsed);
            DataShow=(TextView)findViewById(R.id.show_all_data);
            hint_data=(TextView)findViewById(R.id.hint_datafor_value);

            String abt = getIntent().getStringExtra("mobile_number");
            //Intent i=getIntent();
            //String abt=i.getStringExtra("mobile_number");
            //System.out.println(abt);
            //jsonParsed1.setText(abt);

            new loginAccess().execute();
        }

    class loginAccess extends AsyncTask {

        TextView jsonParsed = (TextView) findViewById(R.id.jsonParsed);

        private String Content;
        String OutputData = "";
        JSONObject jsonResponse;



        @Override
        protected String doInBackground(String... arg0) {

            String abt = getIntent().getStringExtra("mobile_number");

            List params = new ArrayList();
            params.add(new BasicNameValuePair("mobile_number", abt));
            JSONObject json = jsonParser.makeHttpRequest(url,"POST", params);

                Content = json.toString();

                Log.d("Create Response", Content);
     /****************** Start Parse Response JSON Data *************/




                try {

                    /****** Creates a new JSONObject with name/value mappings from the JSON string. ********/
                    jsonResponse = new JSONObject(Content);

                    /******* Fetch node values **********/
                   String hint   = jsonResponse.optString("hint").toString();

                    OutputData += "Node : \n\n     "+ hint +"  ";

                    Log.i("JSON parse", hint);

                 /****************** End Parse Response JSON Data *************/     

                  //  jsonParsed.setText( OutputData );

                 } catch (Exception e) {

                     e.printStackTrace();
                 }


                return null;
        }

        protected void onPostExecute(Void unused) { 
             //      //JSONObject jsonResponse = null;
            //    
            //      /******* Fetch node values **********/
            //        String hint   = jsonResponse.optString("hint").toString();
            //        
            //        OutputData += "Node : \n\n     "+ hint +"  ";
            //        
            //        Log.i("JSON parse", hint);
            //    
            //     /****************** End Parse Response JSON Data *************/     
            //         
            //        hint_data.setText( OutputData );

               jsonParsed.setText( OutputData );

             }

        }
    }



3

3 Answers

1
votes

jsonParsed.setText( OutputData );
You just put above line in onPostExecute method after hint_data.setText( OutputData ); line and you will get the perfect output.
And also delete jsonParsed.setText( OutputData ); line from doInBackground method.

1
votes

Modify your async task like this: doing background method of asynctask runs in NON UI Thread, so we can not modify the ui elements from doInbackgroud so we have to return that result so that result will comes in postExecute method as parameter then we can modify the UI

    package com.example.phonebook;

    import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.widget.TextView;

import com.afbb.animationtest.R;

    public class Welcome extends Activity {
        TextView jsonParsed1,DataShow,hint_data;
        TextView jsonParsed ;
        JSONParser jsonParser = new JSONParser();
        JSONParser jobj = new JSONParser();
        private static String url = "http://192.168.1.10/android_connect/login_setup/show_data.php";
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
            .detectDiskReads().detectDiskWrites().detectNetwork()
            .penaltyLog().build());
            super.onCreate(savedInstanceState);
            setContentView(R.layout.welcome);
            jsonParsed1=(TextView)findViewById(R.id.jsonParsed);
            DataShow=(TextView)findViewById(R.id.show_all_data);
            hint_data=(TextView)findViewById(R.id.hint_datafor_value);
            jsonParsed = (TextView) findViewById(R.id.jsonParsed);
            String abt = getIntent().getStringExtra("mobile_number");
            new loginAccess().execute();
        }

    class loginAccess extends AsyncTask<String,Void,JSONObject> {
        private String Content;
        @Override
        protected JSONObject doInBackground(String... arg0) {
            String abt = getIntent().getStringExtra("mobile_number");
            List params = new ArrayList();
            params.add(new BasicNameValuePair("mobile_number", abt));
            JSONObject json = jsonParser.makeHttpRequest(url,"POST", params);
                Content = json.toString();
                Log.d("Create Response", Content);
     /****************** Start Parse Response JSON Data *************/
                try {
                    JSONObject jsonResponse = new JSONObject(Content);
                   String hint   = jsonResponse.optString("hint").toString();
                    return jsonResponse;
                 } catch (Exception e) {

                     e.printStackTrace();
                 }
                return null;
        }
        protected void onPostExecute(JSONObject result) {
            //Parse and update the UI
               jsonParsed.setText( OutputData );
             }
        }
    }



i
0
votes

Remove below line from doInBackground method

 jsonParsed.setText( OutputData );

You can not touch view from outside main thread.