数据在logcat中显示但不在文本视图中显示

时间:2014-12-29 05:43:04

标签: android json

数据显示在logcat中但不显示在文本视图中..我该怎么办

这是我的日志

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":"jmi.mohsin@gmail.com","2":"myname","1":"msnmsn","0":"8527801400","mobile_number":"8527801400","hint":"myname","email_id":"jmi.mohsin@gmail.com","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)

这是我的欢迎活动


    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 个答案:

答案 0 :(得分:1)

jsonParsed.setText( OutputData );
您只需在onPostExecute行之后的hint_data.setText( OutputData );方法中加上上一行,您就会得到完美的输出 并从jsonParsed.setText( OutputData );方法中删除doInBackground行。

答案 1 :(得分:1)

修改您的异步任务,如下所示: 执行asynctask的后台方法在NON UI Thread中运行,所以我们不能修改doInbackgroud中的ui元素,所以我们必须返回结果,以便结果将postExecute方法作为参数出现,然后我们就可以修改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

答案 2 :(得分:0)

从doInBackground方法

中删除以下行
 jsonParsed.setText( OutputData );

您无法从外部主线接触视图。