我正在尝试制作一个小型应用程序,该应用程序可以从依赖于用户输入的服务器中检索一些 JSON。
到目前为止,我有以下工作要做:
- 用户输入搜索词
 - 应用查找搜索结果
 
我刚才遇到的问题是在检索到结果后解析我的主类中的数据。
我的代码:下面是重要的代码。我没有包含解析或检索数据的方法,因为它们确实有效。
基本上,一旦数据完成检索,我需要一种方法向调用我的 ResultsGetter.getToday() 方法的方法发送“完成”信号或其他东西,因为当我尝试按原样访问数据时,它都是空的数据还没有完成。
主要活动
public class MainActivity extends Activity implements android.view.View.OnClickListener{
    ResultsGetter results;
        HttpClient client;
    EditText location;
    Button search;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        client = new DefaultHttpClient();
        location = (EditText) findViewById(R.id.etLocation);
        search = (Button) findViewById(R.id.btSearch);
        search.setOnClickListener(this);
        search= new ResultsGetter(
                this,
                0,
                "MY DATA URL",
                client);
    }
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
    results.getToday(location.getText().toString());
    }
}
我的 ResultsGetter 类
public class ResultsGetter {
public ResultsGetter(Context c, int f, String url, HttpClient client) {
        this.context = c;
        this.term = f;
        this.url = url;
        this.client = client;
    }
public void getToday(String location) {
        getData = new Reader();
        builtUrl = url + "?length=today&city=" + location;
        getData.execute();
}
public class Reader extends AsyncTask<String, Integer, JSONObject> {
        @Override
        protected JSONObject doInBackground(String... params) {
            // TODO Auto-generated method stub
            try {
                alldata = getData();
                return alldata;
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                Log.e("Error", "CLIENT Exception " + e.toString());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                Log.e("Error", "IOException " + e.toString());
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                Log.e("Error", "JSON Exception " + e.toString());
            }
            return null;
        }
        @Override
        protected void onPostExecute(JSONObject result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            Log.e("Success", "JSON retrieved");
            parseData();
        }
    }
}