onPostExecute(String)方法永远不会在本地使用--AsyncTask

时间:2014-02-17 21:11:10

标签: android android-asynctask override

我正在开发一个小程序,只需从服务器检索一些JSON,解析它,然后将值返回到屏幕。我在AsyncTask中执行此操作,但是onPostExecute(String result)会发出以下警告: MainActivity.RESTfulGET类型中的onPostExecute(String)方法从不在本地使用

我不知道为什么我收到这个警告,而且我已经通过谷歌的许多不同来源试图找到这个问题的答案,遗憾的是我一直找不到任何解决方案。

这是我的代码(请注意,解析器尚未完成,但我不需要任何帮助):

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    private RESTfulGET mTask;

    Button bParseJSON;

    TextView tvJSON;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            Class.forName("android.os.AsyncTask");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        bParseJSON = (Button) findViewById(R.id.bParseJSON);

        tvJSON = (TextView) findViewById(R.id.tvJSON);

        bParseJSON.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mTask = (RESTfulGET) new RESTfulGET().execute("http://www.seasite.niu.edu/cs428_628/Assignments/A2_Client_list_json.txt");
                mTask.getStatus();
            }
        });

    }

    private class RESTfulGET extends AsyncTask <String, Integer, String> {
        HttpClient  httpClient;
        HttpGet     httpGet;

        @Override
        protected String doInBackground(String... urls) {
            String returnValue = null;

            HttpResponse serverResponse;

            String url  = urls[0];
            httpClient  = new DefaultHttpClient();  // Optional args to alter default settings
            httpGet     = new HttpGet(url);         // or the passed-in URI argument string

            try {
                serverResponse = httpClient.execute(httpGet);
                if(serverResponse != null &&
                    serverResponse.getStatusLine().getStatusCode() == 200) {
                    // Success, do something
                    HttpEntity responseEntity = serverResponse.getEntity();

                    InputStream is = null;

                    try {
                        is = responseEntity.getContent();
                        BufferedReader readBuf = new BufferedReader(new InputStreamReader(is));
                        StringBuilder sb = new StringBuilder();

                        char[] buf = new char[1024]; // an initial size
                        int bytesRead;

                        // this could block; thus it is done in a background thread
                        while((bytesRead = readBuf.read(buf, 0, 1024)) != -1) {
                            sb.append(buf, 0, bytesRead);
                        }

                        returnValue = sb.toString();
                    }
                    finally {
                        // do nothing
                    }
                }
            } catch(IOException e) {

            } // try

            return returnValue;
        } // doInBackground

        @Override
        protected void onProgressUpdate(Integer... progress) {

        }

        protected void onPostExcute(String result) {
            // back on main thread, get the result passed by doInBackground() and cast it in preparation to parsing
            if(result != null) {
                try {
                    JSONObject json = new JSONObject(result);
                    JSONArray jArray = ((JSONObject) json).getJSONArray("clients");

                    for(int i = 0; i < jArray.length(); i++) {
                        JSONObject client_json = jArray.getJSONObject(i);
                        String name         = client_json.getString("name");
                        String profession   = client_json.getString("profession");
                        String dob          = client_json.getString("dob");
                        Log.d("RESULT", "Name: " + name);
                        Log.d("RESULT", "Profession: " + profession);
                        Log.d("RESULT", "DOB: " + dob);
                    } // for
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } // if
        } // onPostExecute

    } // RESTfulGET
}

1 个答案:

答案 0 :(得分:6)

这里有一个错字:

  protected void onPostExcute(String result) {

应该是:

  @Override
  protected void onPostExecute(String result) {

所以你最后编写了自己的函数,因为onPostExcute中输入错误(e丢失了)并且没有警告你没有覆盖任何方法,因为你也没有{{1注释

相关问题