如何从asynctask中的JSON查询返回多个字符串?

时间:2014-03-10 17:00:51

标签: java android json android-asynctask

我目前正在处理一个群组项目,我们正在尝试从asynctask中的JSON查询中获取字符串值,以便通过意图将它们发送到第二个活动。我们遇到的麻烦是,当字符串从asynctask中取出时,它变为null。怎么解决这个问题?

public class connectTask extends AsyncTask<String,String,TCPClient> {

    String FilmId, Name, Certificate, Duration, Director, Description, ReleaseDate, Cast; 
    @Override
    protected TCPClient doInBackground(String... message) {

        //we create a TCPClient object and
        mTcpClient = new TCPClient(new TCPClient.OnMessageReceived() {
            @Override
            //here the messageReceived method is implemented
            public void messageReceived(String message) {
                //this method calls the onProgressUpdate


                Log.e("TCP Client", message);




                try 
                {
                    JSONObject json = new JSONObject(message);
                    FilmId = (String) json.get("FilmId");
                    Name = (String) json.get("Name");
                    Certificate = (String) json.get("Certificate");
                    Duration =(String) json.get("Duration");
                    Director = (String) json.get("Director");
                    Description = (String) json.get("Description");
                    ReleaseDate = (String) json.get("ReleaseDate");
                    Cast = (String) json.get("Cast");
                    Log.e("FilmId: ", FilmId);
                    Log.e("Name: ", Name);
                    Log.e("Cert: ", Certificate);
                    Log.e("Duration: ", Duration);
                    Log.e("Director: ", Director);
                    Log.e("Description: ", Description);
                    Log.e("ReleaseDate: ", ReleaseDate);
                    Log.e("Cast: ", Cast);



                } 
                catch (JSONException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }



            }
        });


        mTcpClient.run("GTF 02");
        System.out.println(JSON[1]);    



        return null;
       }
    protected void onPostExecute()
    {
    JSON[0] = FilmId;
    JSON[1] = Name;



    }
}


public void mLoadFilmInfo(View view) 
{

    //FilmID = "GTF "+ ButtonName;
    new connectTask().execute();

    Intent FilmInfo = new Intent(this, FilmInfo.class);
    FilmInfo.putExtra("NamePass", JSON[1]);



    startActivity(FilmInfo);

 }

这是asynctask的代码和传递给第二个活动。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:3)

AsyncTask与您的代码并行运行。执行完成后,它不会立即完成。 execute()下面的所有代码都需要在你的onPostExecute中。

答案 1 :(得分:1)

尝试这样的事情,异步线程是异步的,你无法确定它何时结束:

public class connectTask extends AsyncTask<String,String,String[]> {

    String FilmId, Name, Certificate, Duration, Director, Description, ReleaseDate, Cast; 
    @Override
    protected String[] doInBackground(String... message) {
    String[] jsonArr = new String[9];
        //we create a TCPClient object and
        mTcpClient = new TCPClient(new TCPClient.OnMessageReceived() {
            @Override
            //here the messageReceived method is implemented
            public void messageReceived(String message) {
                //this method calls the onProgressUpdate


                Log.e("TCP Client", message);




                try 
                {
                    JSONObject json = new JSONObject(message);
                    FilmId = (String) json.get("FilmId");
                    Name = (String) json.get("Name");
                    Certificate = (String) json.get("Certificate");
                    Duration =(String) json.get("Duration");
                    Director = (String) json.get("Director");
                    Description = (String) json.get("Description");
                    ReleaseDate = (String) json.get("ReleaseDate");
                    Cast = (String) json.get("Cast");
                    Log.e("FilmId: ", FilmId);
                    Log.e("Name: ", Name);
                    Log.e("Cert: ", Certificate);
                    Log.e("Duration: ", Duration);
                    Log.e("Director: ", Director);
                    Log.e("Description: ", Description);
                    Log.e("ReleaseDate: ", ReleaseDate);
                    Log.e("Cast: ", Cast);

    jsonArr [0] = FilmId;
jsonArr [1] = Name;

                } 
                catch (JSONException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }



            }
        });


        mTcpClient.run("GTF 02");
        System.out.println(jsonArr [1]);    



        return jsonArr ;
       }
    protected void onPostExecute(String[] JSON)
    {

    Intent FilmInfo = new Intent(this, FilmInfo.class);
    FilmInfo.putExtra("NamePass", JSON[1]);



    startActivity(FilmInfo);

    }
}


public void mLoadFilmInfo(View view) 
{

    //FilmID = "GTF "+ ButtonName;
    new connectTask().execute();


 }