2

我是 Android 和 Java 的初学者。到目前为止还不错,但我偶然发现了一个我无法解决的问题。

我正在尝试在我的应用程序类中创建一个方法,该方法将使用传递给它的值对列表进行 http 调用。这是第一部分。这是在通过单击按钮激活的活动中。

  // Add your data to array
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("action", "2"));
        nameValuePairs.add(new BasicNameValuePair("cell", "2500270025"));
        nameValuePairs.add(new BasicNameValuePair("date", "blah"));
        nameValuePairs.add(new BasicNameValuePair("time", "AndDev is Cool!"));
        nameValuePairs.add(new BasicNameValuePair("reason", "2"));

        // need to call the request
        String result = ((RespondApp) getApplication()).makeHTTPCall(nameValuePairs);

一旦我进入应用程序,这里就是接收部分。

public String makeHTTPCall(List<NameValuePair> nameValuePairs) {
        // this will be used to make all http requests from the whole app       
        new postToHttp().execute(nameValuePairs);
        return null;        
    }

这是 AsyncTask 部分。

class postToHttp extends AsyncTask<List<NameValuePair>, Void, String> {

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            // I am sure I need something here just don't know what
        }

        @Override
        protected String doInBackground(List<NameValuePair>... params) {

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://www.evfd.ca/cell.php");
            try {

                httppost.setEntity(new UrlEncodedFormEntity(params[0]));
                Log.i("makeHttpCall", "done ecoding");
                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    InputStream instream = entity.getContent();
                String result = convertStreamToString(instream);
                Log.i("Read from server", result);
                return result;
                }

            } catch (ClientProtocolException e) {
                return null;
            } catch (IOException e) {
                return null;
            }
            return null;
        }

我试图让网络服务器发回的响应一直返回到调用此过程的活动页面。因此,理想情况下,我只想加载值对进行调用并返回 http 响应,然后继续我的快乐方式。

我怎样才能做到这一点?

4

1 回答 1

2

You can work with the String you downloaded on the main UI thread right from the AsyncTask itself. Simply override protected void onPostExecute(String result) in your AsyncTask class and do the work there. This function will get called with whatever value you return from doInBackground().

Basically, do whatever the next step is inside of onPostExecute(). See How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class? for a few ideas:

  • Nest the AsyncTask class as an inner class within your Activity so it can work with your Activity's variables, methods, etc.

  • Create an interface that your Activity implements. Basically, the task would call something like onHttpTaskComplete(String result) in the Activity.

于 2012-12-22T02:04:58.277 回答