在我的 android 项目中有一个小错误我无法从 AsyncTask 子类访问 Super 类字符串变量我收到以下错误
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);
}
});
}
}
}
错误是
JSONObject json = jParser.getJSONFromUrl(url);
我无法从 onCreate() 方法调用 url 字符串 任何人都可以修复此错误吗?