Android php+mysql+json error implementing AsyncTask



我是Android新手,我需要一点帮助。我有一个来自远程 mysql 上的一个表的女巫值,它在 2.3 上工作正常,但在 4.x 上我启动它时出现错误。我在某处读过,因为我没有使用 AsyncTask。我试图在现有的工作代码上实现它,但有一个错误,所以请帮助,因为我尽了一切努力让它工作但没有成功。

这是适用于 2.3 的现有代码

package com.stole.fetchtest;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.net.ParseException;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Toast;
public class FetchData extends ListActivity {
    @SuppressWarnings("unchecked")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String result = null;
        InputStream is = null;
        StringBuilder sb = null;
        ArrayList<?> nameValuePairs = new ArrayList<Object>();
        List<String> r = new ArrayList<String>();
        try {

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(
                    "http://www.url.com/fetch.php");
            httppost.setEntity(new UrlEncodedFormEntity(
                    (List<? extends NameValuePair>) nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
        } catch (Exception e) {
            Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
                    .show();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "UTF-8"));
            sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "n");
            }
            is.close();
            result = sb.toString();
        } catch (Exception e) {
            Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
                    .show();
        }
        try {
            JSONArray jArray = new JSONArray(result);
            JSONObject json_data = null;
            for (int i = 0; i < jArray.length(); i++) {
                json_data = jArray.getJSONObject(i);
                r.add(json_data.getString("names"));
            }
            setListAdapter(new ArrayAdapter<String>(this, R.layout.names_row, r));
        } catch (JSONException e1) {
            Toast.makeText(getBaseContext(), e1.toString(), Toast.LENGTH_LONG)
                    .show();
        } catch (ParseException e1) {
            Toast.makeText(getBaseContext(), e1.toString(), Toast.LENGTH_LONG)
                    .show();
        }
    }}

然后我尝试添加AsyncTask,根据网上找到的一些教程,它看起来像这样:

package com.stole.fetchtest;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Toast;
public class FetchData extends ListActivity {
    String result = null;
    InputStream is = null;
    StringBuilder sb = null;
    ArrayList<?> nameValuePairs = new ArrayList<Object>();
    List<String> r = new ArrayList<String>();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new task().execute();
    }
    public class task extends AsyncTask<String, String, Void> {
        @SuppressWarnings("unchecked")
        @Override
        protected Void doInBackground(String... params) {
            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://www.url.com/fetch.php");
                httppost.setEntity(new UrlEncodedFormEntity(
                        (List<? extends NameValuePair>) nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
            } catch (Exception e) {
                Toast.makeText(getBaseContext(), e.toString(),
                        Toast.LENGTH_LONG).show();
            }
            try {
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(is, "iso-8859-1"));
                sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "n");
                }
                is.close();
                result = sb.toString();
            } catch (Exception e) {
                Toast.makeText(getBaseContext(), e.toString(),
                        Toast.LENGTH_LONG).show();
            }
            return null;
        }
        protected void onPostExecute(Void v) {
            try {
                JSONArray jArray = new JSONArray(result);
                JSONObject json_data = null;
                for (int i = 0; i < jArray.length(); i++) {
                    json_data = jArray.getJSONObject(i);
                    r.add(json_data.getString("naziv"));
                }
                setListAdapter(new ArrayAdapter(this, R.layout.names_row, r));
            } catch (JSONException e1) {
                Toast.makeText(getBaseContext(), e1.toString(),
                        Toast.LENGTH_LONG).show();
            } catch (ParseException e1) {
                Toast.makeText(getBaseContext(), e1.toString(),
                        Toast.LENGTH_LONG).show();
            }
        }
    }
}

我得到的错误是

setListAdapter(new ArrayAdapter(this, R.layout.names_row, r));

它说"构造函数 ArrayAdapter(FetchData.task, int, List) 是未定义的"谢谢!

它应该使用活动上下文而不是任务上下文。

setListAdapter(new ArrayAdapter(FetchData.this, R.layout.names_row, r));

最新更新