-1

我正在尝试在异步任务之外使用 json 对象,但我的班级无法识别它。

如何将 JSONObject 传递到 Async 任务的外部?

我唯一能找到的是试图用来onProgressUpdate()传递对象,但我尝试实现并且对象会出现空白:(。

任何帮助将不胜感激,谢谢。

这是我的课:

public class NewHomepage extends Activity {

    public static String url = "http://www.alkouri.com/android/SQL.php?username=";
    public static String usernamefromlogin;
    public static TextView errorchecking; 
    public static JSONArray user = null;

    //JSON Node Names 
    public String TAG_USER = "users";
    public String TAG_FIRST = "first";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reshomepage);
        //get data from previous screen
          Intent intent = getIntent();
          getIntent().getExtras();

          //convert intent (intent) to string called "usernamefromlogin"     
         //error checking in log cat to see value of "usernamefromlogin"      
          usernamefromlogin = intent.getExtras().getString("username2"); 
           Log.d("log usernamefromlogin", usernamefromlogin);

          //take the string "url" and add string "usernamefromlogin" after it       
         //error checking in log cat to see value of url5
          String url5 = url.concat(usernamefromlogin);                           
            Log.d("log url5", url5);

            //start asynch task
         class PostTask2 extends AsyncTask<String, String, String> {
               @Override
               protected void onPreExecute() {
                  super.onPreExecute();

               }//end PreExecute

               @Override
               protected String doInBackground(String... params) {
                //pass url from outside class to inside this class   
                String url5 = params[0];

                //Creating new JSON Parser
                JSONParser jParser = new JSONParser();

                // Getting JSON from URL
                JSONObject json = jParser.getJSONFromUrl(url5);

                return null;

               }//end doInBackground

             protected void onProgressUpdate(String... values) {
                  super.onProgressUpdate(values);
              }//end onProgressUpdate

               @Override
               protected void onPostExecute(String result) {
               super.onPostExecute(result);

               }//end onPostExecute
               }//end Async task

        //execute the Async task
        new PostTask2().execute(url5);

    }//end oncreate

    @Override
    public void onBackPressed() {
        // do nothing on back press
    }
 }//end class
4

3 回答 3

0

您需要将 AsyncTask calss 放在 onCreate 方法之外,并从 onCreate 方法内部调用它。

PS但是,正如评论中一样,发布您的 json 响应并告诉我们您想从响应中解析什么。最重要的是,您需要使用 HTTP 或 Rest Client 来查询服务器并获得响应。

编辑

要了解如何向服务器发出请求,请阅读本教程,或者您也可以阅读本教程

于 2014-01-07T04:13:58.737 回答
0

您可以JSONObjectonPostExecute如下方式返回:

  class PostTask2 extends AsyncTask<String, String, JSONObject> {

          ..............................

           @Override
           protected String doInBackground(String... params) {
            //pass url from outside class to inside this class   
            String url5 = params[0];

            //Creating new JSON Parser
            JSONParser jParser = new JSONParser();

            // Getting JSON from URL
            JSONObject json = jParser.getJSONFromUrl(url5);

            return json;

           }//end doInBackground

         protected void onProgressUpdate(String... values) {
              super.onProgressUpdate(values);
          }//end onProgressUpdate

           @Override
           protected void onPostExecute(JSONObject result) {
           super.onPostExecute(result);

           }//end onPostExecute
           }//end Async task
于 2014-01-07T04:46:03.540 回答
0

您必须将您的 asynctask 从您的 onCreate 方法中提取出来,并将其作为嵌套类插入到 NewHomepage 中。

现在,您可以从 doInBackground() 返回一个 JSONObject,如下面的代码所示。注意 AsncTask。

很高兴学习一些关于活动生命周期和内存泄漏的东西(艰难的方式)(我是认真的,试试吧,只是为了练习)。但是,对于真正的应用程序,我强烈推荐像Volley这样的 Rest 客户端库。

    public class NewHomepage extends Activity {

        // ... your activity code


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            new PostTask2().execute(url5);
        }

        class PostTask2 extends AsyncTask<String, String, JSONObject> {

                    @Override
                    protected void onPreExecute() {
                        super.onPreExecute();

                    }//end PreExecute

                    @Override
                    protected String doInBackground(String... params) {
                        //pass url from outside class to inside this class
                        String url5 = params[0];

                        //Creating new JSON Parser
                        JSONParser jParser = new JSONParser();

                        // Getting JSON from URL
                        JSONObject json = jParser.getJSONFromUrl(url5);

                        return json;

                    }//end doInBackground

                    protected void onProgressUpdate(String... values) {
                        super.onProgressUpdate(values);
                    }//end onProgressUpdate

                    @Override
                    protected void onPostExecute(JSONObject result) {
                        // HERE, this is running on the main thread.
                        // You can do whatever you need to do with your JSONObject.
                        // Maybe call setData(result) but be careful, the user might have left this activty already!
                        // And the View might have been already destroyed, so watchout for null-pointer-exceptions
                    }//end onPostExecute
                }
    }
于 2014-01-07T04:32:39.800 回答