在不使用listview的情况下将JSON值输出到文本视图

时间:2014-09-08 16:59:05

标签: android json listview

我在这里有这个代码,而且工作非常好。

我尝试将JSON的结果显示到textView中,但每次我这样做,应用程序崩溃,我搜索了很多,

大多数解决方案是关于使用ListView BUT的值我只需要一个结果,这就是我从JSON中检索的内容

public class ProfileActivity extends Activity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_profile_activity);
        // we will using AsyncTask during parsing 
        new AsyncTaskParseJson().execute();
    }

 // you can make this class as another java file so it will be separated from your main activity.
    public class AsyncTaskParseJson extends AsyncTask<String, String, String> {

        final String TAG = "AsyncTaskParseJson.java";

        // set your json string url here
        String yourJsonStringUrl = "http://example.com";

        // contacts JSONArray
        JSONArray dataJsonArr = null;

        @Override
        protected void onPreExecute() {}

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

            try {

                // instantiate our json parser
                JSONParser jParser = new JSONParser();

                // get json string from url
                JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);

                // get the array of users
                dataJsonArr = json.getJSONArray("Users");

                // loop through all users
                for (int i = 0; i < dataJsonArr.length(); i++) {

                    JSONObject c = dataJsonArr.getJSONObject(i);


                    String username = c.getString("UserName");

                    // show the values in our logcat
                    Log.e(TAG, "Username: " +  username);

                    TextView lblName = (TextView) findViewById(R.id.name);
                    lblName.setText(username);

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String strFromDoInBg) {}
    }
}

Json值

{ "Users": [ { "fullName    ":"", "UserName ":"admin", "UserRegistered  ":"2014-09-07 15:42:39", "EmailAddress  ":"usf.me2000@gmail.com", "ProfilePicture ":"http://static.adzerk.net/Advertisers/8f5b41b43c804b25b9bed0806b82ac69.gif", "Facebook  ":"http://facebook.com", "Twitter   ":"http://twitter.com", "LinkedIn   ":"http://plus.google.com", "GooglePlus ":"http://linkedin.com", "ProfileHeader ":"User Extra Field", "TotalPosts   ":"10" } ] }

LogCat错误

09-08 17:19:56.426: E/AsyncTaskParseJson.java(6729): Username: admin // This to validate the getting value is correct


09-08 17:19:56.430: W/dalvikvm(6729): threadid=12: thread exiting with uncaught exception (group=0xa624a288)
09-08 17:19:56.430: E/AndroidRuntime(6729): FATAL EXCEPTION: AsyncTask #1
09-08 17:19:56.430: E/AndroidRuntime(6729): java.lang.RuntimeException: An error occured while executing doInBackground()
09-08 17:19:56.430: E/AndroidRuntime(6729):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
09-08 17:19:56.430: E/AndroidRuntime(6729):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-08 17:19:56.430: E/AndroidRuntime(6729):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-08 17:19:56.430: E/AndroidRuntime(6729):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-08 17:19:56.430: E/AndroidRuntime(6729):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-08 17:19:56.430: E/AndroidRuntime(6729):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
09-08 17:19:56.430: E/AndroidRuntime(6729):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-08 17:19:56.430: E/AndroidRuntime(6729):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-08 17:19:56.430: E/AndroidRuntime(6729):     at java.lang.Thread.run(Thread.java:856)
09-08 17:19:56.430: E/AndroidRuntime(6729): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
09-08 17:19:56.430: E/AndroidRuntime(6729):     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4607)
09-08 17:19:56.430: E/AndroidRuntime(6729):     at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:867)
09-08 17:19:56.430: E/AndroidRuntime(6729):     at android.view.ViewGroup.invalidateChild(ViewGroup.java:4066)
09-08 17:19:56.430: E/AndroidRuntime(6729):     at android.view.View.invalidate(View.java:10250)
09-08 17:19:56.430: E/AndroidRuntime(6729):     at android.view.View.invalidate(View.java:10205)
09-08 17:19:56.430: E/AndroidRuntime(6729):     at android.widget.TextView.checkForRelayout(TextView.java:6296)
09-08 17:19:56.430: E/AndroidRuntime(6729):     at android.widget.TextView.setText(TextView.java:3547)
09-08 17:19:56.430: E/AndroidRuntime(6729):     at android.widget.TextView.setText(TextView.java:3405)
09-08 17:19:56.430: E/AndroidRuntime(6729):     at android.widget.TextView.setText(TextView.java:3380)
09-08 17:19:56.430: E/AndroidRuntime(6729):     at com.twaa9l.photosee.ProfileActivity$AsyncTaskParseJson.doInBackground(ProfileActivity.java:67)
09-08 17:19:56.430: E/AndroidRuntime(6729):     at com.twaa9l.photosee.ProfileActivity$AsyncTaskParseJson.doInBackground(ProfileActivity.java:1)
09-08 17:19:56.430: E/AndroidRuntime(6729):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-08 17:19:56.430: E/AndroidRuntime(6729):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-08 17:19:56.430: E/AndroidRuntime(6729):     ... 5 more

2 个答案:

答案 0 :(得分:0)

您正在尝试getString&#34;名字&#34;和&#34;姓氏&#34;,但您的json数据没有这些密钥。

答案 1 :(得分:0)

你的LogCat说明了一切。您无法更新doInBackground中的textview。在onPostExecute中进行更新