I am trying to fill a spinner with mysql data using php web services that return a JSONArray, the problem is that it is giving me back the error that says the title.
private class consultarDatos extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
// params comes from the execute() call: params[0] is the url.
try {
return downloadUrl(urls[0]);
} catch (IOException e) {
return "Unable to retrieve web page. URL may be invalid.";
}
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
JSONArray ja = null;
try {
ja = new JSONArray(result);
IdProvincia = ja.getInt(0);
} catch (JSONException e){
e.printStackTrace();
}
I have tried the php file and it returns the JSONArray well, the app works well in the emulator but not in the physical phones.
This is the exception it throws.
org.json.JSONException: Value ���������������������������
at org.json.JSON.typeMismatch(JSON.java:111)
at org.json.JSONArray.<init>(JSONArray.java:96)
at org.json.JSONArray.<init>(JSONArray.java:108)
Here is the DownloadUrl method:
private String downloadUrl(String myurl) throws IOException {
InputStream is = null;
// Aqui es importante reemplazar los espacios por %20
myurl = myurl.replace(" ", "%20");
// Only display the first 500 characters of the retrieved
// web page content.
int len = 5000;
try {
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
int response = conn.getResponseCode();
Log.d("Respuesta", "The response is: " + response);
is = conn.getInputStream();
// Convert the InputStream into a string
String contentAsString = readIt(is, len);
return contentAsString;
// Makes sure that the InputStream is closed after the app is
// finished using it.
} finally {
if (is != null) {
is.close();
}
}
}
public String readIt(InputStream stream, int len) throws IOException,
UnsupportedEncodingException {
Reader reader = null;
reader = new InputStreamReader(stream, "UTF-8");
char[] buffer = new char[len];
reader.read(buffer);
return new String(buffer);
}