Las conexiones desde Android hay que hacerlas con un método llamado AsycTask, aquí te dejo un ejemplo mio con la meteorología.
MainActivity:
public class MainActivity extends AppCompatActivity {
private final static String url = "http://api.openweathermap.org/data/2.5/weather?q=hanoi&mode=xml";
private TextView location, country, temperature, humidity, pressure;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
location = (TextView) findViewById(R.id.location);
country = (TextView) findViewById(R.id.country);
temperature = (TextView) findViewById(R.id.temperature);
humidity = (TextView) findViewById(R.id.humidity);
pressure = (TextView) findViewById(R.id.pressure);
new GetWeatherDataTask(this, url).execute();
}
public void callBackData(String[] result) {
temperature.setText((Float.parseFloat(result[2]) - 273) + " degree Celcius" );
humidity.setText(result[0] + " %");
pressure.setText(result[1] + " hPa");
country.setText(result[4]);
location.setText(result[3]);
}
}
GetWeatherDataTask (el AsynxTask):
public class GetWeatherDataTask extends AsyncTask<String[], Void, String[]> {
private MainActivity activity;
private String url;
private XmlPullParserFactory xmlFactoryObject;
private ProgressDialog pDialog;
public GetWeatherDataTask(MainActivity activity, String url) {
this.activity = activity;
this.url = url;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(activity);
pDialog.setTitle("Get Weather Information from XML");
pDialog.setMessage("Loading...");
pDialog.show();
}
@Override
protected String[] doInBackground(String[]... params) {
try {
URL url = new URL(this.url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(10000 /* milliseconds */);
connection.setConnectTimeout(15000 /* milliseconds */);
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.connect();
InputStream stream = connection.getInputStream();
xmlFactoryObject = XmlPullParserFactory.newInstance();
XmlPullParser myParser = xmlFactoryObject.newPullParser();
myParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
myParser.setInput(stream, null);
String[] result = parseXML(myParser);
stream.close();
return result;
} catch (Exception e) {
e.printStackTrace();
Log.e("AsyncTask", "exception");
return null;
}
}
public String[] parseXML(XmlPullParser myParser) {
int event;
String text = null;
String[] result = new String[5];
try {
event = myParser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
String name = myParser.getName();
switch (event) {
case XmlPullParser.START_TAG:
break;
case XmlPullParser.TEXT:
text = myParser.getText();
break;
case XmlPullParser.END_TAG:
//get country name
if (name.equals("country")) {
result[4] = text;
} else if (name.equals("humidity")) { //get humidity
result[0] = myParser.getAttributeValue(null, "value");
} else if (name.equals("pressure")) { //get pressure
result[1] = myParser.getAttributeValue(null, "value");
} else if (name.equals("temperature")) { //get temperature
result[2] = myParser.getAttributeValue(null, "value");
} else if (name.equals("coord")) { //get location
result[3] = "(" + myParser.getAttributeValue(null, "lat") + " , "
+ myParser.getAttributeValue(null, "lon") + ")";
}
break;
}
event = myParser.next();
}
return result;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(String[] result) {
//call back data to main thread
pDialog.dismiss();
activity.callBackData(result);
}
}
Espero que esto te ayude!
http://dcdatabase.me/characters.json
no funciona. Lo primero sería usar un URL que responda. Yo usaríaVolley
para la petición.