1

In my android project there is small error i can't access the Super class string variable from the AsyncTask sub class i got the following error

public class XyzActivity extends ListActivity {

    // JSON Node names
                private static final String TAG_CONTACTS = "results";
                private static final String TAG_ID = "id";
                private static final String TAG_NAME = "name";
                private static final String TAG_GENDER = "rating";
                private static final String TAG_ADDRESS = "formatted_address";
                private static final String TAG_REFERENCE = "reference";


                private static final String TAG_LOCATION = "location";
                private static final String TAG_TYPE="type";


    // contacts JSONArray
    JSONArray results = null;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
           String location = extras.getString("TAG_LOCATION");

        // url to make request
        String url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=restaurents+in+"+location+"&sensor=true&key=AIzaSyD38pak_katUfSR92WE2_O2b9y0JSp5htA";
        }

        LoadData ld = new LoadData();
        ld.onPreExecute();
    new LoadData().execute();

    }

class LoadData extends AsyncTask<String, String, String>
    { 
    ProgressDialog pDialog;

protected void onPreExecute() {

pDialog = new ProgressDialog(Xyz.this); 
pDialog.setMessage("Populating list please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
}

    // Hashmap for ListView
        ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
    protected String doInBackground(String... args) {


        // Creating JSON Parser instance
        JSONParser jParser = new JSONParser();
        // getting JSON string from URL
        JSONObject json = jParser.getJSONFromUrl(url);

        try {
            // Getting Array of Results
            results = json.getJSONArray(TAG_CONTACTS);

            // looping through All Contacts
            for(int i = 0; i < results.length(); i++){
                JSONObject c = results.getJSONObject(i);

                // Storing each json item in variable
                String id = c.getString(TAG_ID);
                String name = c.getString(TAG_NAME);
                String formatted_address = c.getString(TAG_ADDRESS);
                String rating = c.getString(TAG_GENDER);

                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                map.put(TAG_ID, id);
                map.put(TAG_NAME, name);
                map.put(TAG_ADDRESS, formatted_address);
                map.put(TAG_GENDER, rating);

                // adding HashList to ArrayList
                contactList.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
            }    
        protected void onPostExecute(String file_url) {

        this.pDialog.cancel();
                // dismiss the dialog after getting all products
                    /**
         * Updating parsed JSON data into ListView
         * */
        ListAdapter adapter = new SimpleAdapter(Xyz.this, contactList,
                R.layout.listview_item_row,
                new String[] { TAG_NAME, TAG_ADDRESS, TAG_GENDER,}, new int[] {
                        R.id.txtTitle, R.id.txtTitle1, R.id.txtTitle3 });

        setListAdapter(adapter);

        // selecting single ListView item
        ListView lv = getListView();

        // Launching new screen on Selecting Single ListItem
        lv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                // Starting new intent
                Intent in = new Intent(getApplicationContext(), ProfileViewActivity.class);

            }
        });

        }

}
}

And the error is

JSONObject json = jParser.getJSONFromUrl(url);

i can't call the url string from the onCreate() method can anyone fix this error?

4

2 に答える 2

3

...のに文字列を定義します。onCreate

public class XyzActivity extends ListActivity {

// JSON Node names
            private static final String TAG_CONTACTS = "results";
            private static final String TAG_ID = "id";
            private static final String TAG_NAME = "name";
            private static final String TAG_GENDER = "rating";
            private static final String TAG_ADDRESS = "formatted_address";
            private static final String TAG_REFERENCE = "reference";


            private static final String TAG_LOCATION = "location";
            private static final String TAG_TYPE="type";


// contacts JSONArray
JSONArray results = null;

private String url = "";


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
       String location = extras.getString("TAG_LOCATION");

    // url to make request
    url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=restaurents+in+"+location+"&sensor=true&key=AIzaSyD38pak_katUfSR92WE2_O2b9y0JSp5htA";
    }

    LoadData ld = new LoadData();

new LoadData().execute();

}

class LoadData extends AsyncTask<String, String, String>
{ 
ProgressDialog pDialog;

protected void onPreExecute() {

pDialog = new ProgressDialog(Xyz.this); 
pDialog.setMessage("Populating list please wait...");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(false);
    pDialog.show();
}

// Hashmap for ListView
    ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
protected String doInBackground(String... args) {


    // Creating JSON Parser instance
    JSONParser jParser = new JSONParser();
    // getting JSON string from URL
    JSONObject json = jParser.getJSONFromUrl(url);

    try {
        // Getting Array of Results
        results = json.getJSONArray(TAG_CONTACTS);

        // looping through All Contacts
        for(int i = 0; i < results.length(); i++){
            JSONObject c = results.getJSONObject(i);

            // Storing each json item in variable
            String id = c.getString(TAG_ID);
            String name = c.getString(TAG_NAME);
            String formatted_address = c.getString(TAG_ADDRESS);
            String rating = c.getString(TAG_GENDER);

            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();

            // adding each child node to HashMap key => value
            map.put(TAG_ID, id);
            map.put(TAG_NAME, name);
            map.put(TAG_ADDRESS, formatted_address);
            map.put(TAG_GENDER, rating);

            // adding HashList to ArrayList
            contactList.add(map);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
        }    
    protected void onPostExecute(String file_url) {

    this.pDialog.cancel();
            // dismiss the dialog after getting all products
                /**
     * Updating parsed JSON data into ListView
     * */
    ListAdapter adapter = new SimpleAdapter(Xyz.this, contactList,
            R.layout.listview_item_row,
            new String[] { TAG_NAME, TAG_ADDRESS, TAG_GENDER,}, new int[] {
                    R.id.txtTitle, R.id.txtTitle1, R.id.txtTitle3 });

    setListAdapter(adapter);

    // selecting single ListView item
    ListView lv = getListView();

    // Launching new screen on Selecting Single ListItem
    lv.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            // Starting new intent
            Intent in = new Intent(getApplicationContext(), ProfileViewActivity.class);

        }
    });

    }

}
}
于 2012-09-24T11:58:58.100 に答える
2

urlメソッドのローカル変数ですonCreate。この状態でコンパイルすることさえできません。(未定義の可能性があり、AsyncTask がまだ実行されていることは言うまでもありません)。

URL をパラメーターとして execute メソッドに渡す必要があります。

new LoadData().execute(url);

次に、AsyncTask で取得できます。

String url = args[0];

また、AsyncTask から null を返すことはあまり役に立ちません。フェッチした要素の配列を返すことをお勧めします。

于 2012-09-24T12:31:06.073 に答える