我有一个可以调用的按钮CheckNetworkConnectivity
。如果 WiFi 不可用,它会显示带有一些文本的 toast(网络不可用),这很好。但是当 WiFi 连接可用Activity
时,访问 URL 时会崩溃。
我的问题是有时 WiFi 网络可用,但您必须登录才能访问服务器。它在您的手机中显示为 WiFi 连接。
ExampleActivity.java:
public class ExampleActivity extends ListActivity {
// url to make request
private static String url = "http://10.0.2.2/test/index.php";
// JSON Node usds
private static final String TAG_CONTACTS = "contacts";
private static final String TAG_ID = "id";
private static final String TAG_CURRENCY = "currency";
private static final String TAG_BUY = "buy";
private static final String TAG_SALE = "sale";
private static final String TAG_UPDATE = "update";
// contacts JSONArray
JSONArray contacts = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.example_layout);
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
contacts = json.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for(int i = 0; i < contacts.length(); i++){
JSONObject c = contacts.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String currency = c.getString(TAG_CURRENCY);
String buy = c.getString(TAG_BUY);
String sale = c.getString(TAG_SALE);
String update = c.getString(TAG_UPDATE);
// 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_CURRENCY, currency);
map.put(TAG_BUY, buy);
map.put(TAG_SALE, sale);
map.put(TAG_UPDATE, update);
// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(this, contactList,
R.layout.list_item_exchange,
new String[] { TAG_CURRENCY, TAG_BUY, TAG_SALE, TAG_UPDATE }, new int[] {
R.id.currency, R.id.buy, R.id.sale, R.id.update });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
}
}