Issue
I have a problem with mobile data using Asynctask. If i use Wifi
, the problem doesn't appear. Here the code:
@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 null;
}
}
// onPostExecute displays the results of the AsyncTask.
@Override
public void onPostExecute(String result) {
}
private String downloadUrl(String myurl) throws IOException {
Log.i("URL",""+myurl);
myurl = myurl.replace(" ","%20");
InputStream is = null;
int len = 5000000;
try {
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();// crea la conexion de tipo HttpURLConnection
conn.setReadTimeout(15000 /* milliseconds */);
conn.setConnectTimeout(20000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
int response = conn.getResponseCode();
Log.d("respuesta", "The response is: " + response);
is = conn.getInputStream();
String contentAsString = readIt(is, len);
return contentAsString;
} finally {
if (is != null) {
is.close();
}
}
}
public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
Reader reader = null;
String resultado="";
reader = new InputStreamReader(stream, "UTF-8");
char[] buffer = new char[len];
while(reader.read(buffer)!= -1){
resultado += new String (buffer);
}
System.out.println(resultado);
return resultado;
}
Here the error, the response is incomplete (i tried to change "len" to "5000000") but the error is the same:
4-07 11:27:33.737 7556-7556/com.example.alex.version2 E/AndroidRuntime:
FATAL EXCEPTION: main
Process: com.example.alex.version2, PID: 7556
04-07 11:27:33.738 7556-7556/com.example.alex.version2 E/AndroidRuntime:
java.lang.NumberFormatException: For input string: "-7.28610������������������������������.....
Here with wifi:
04-07 11:51:20.547 12538-12565/com.example.alex.version2 D/respuesta: The
response is: 200
04-07 11:51:20.611 12538-12565/com.example.alex.version2 I/System.out:
[{"latitud":"43.0037506","longitud":"-7.5530884"},
{"latitud":"43.0451345","longitud":"-7.5696921"},
{"latitud":"43.4696742","longitud":"-7.2975061"},
{"latitud":"43.5041140","longitud":"-7.2437110"},
{"latitud":"43.5273794","longitud":"-7.4154747"},
{"latitud":"43.5273920","longitud":"-7.4165190"},..................
Mobile data cut my results. Some ideas? Thank you.
Solution
Your readIt
method is bugged. Try this instead:
public String readIt(InputStream stream) throws IOException, UnsupportedEncodingException {
Reader reader = new InputStreamReader(new BufferedInputStream(stream), "UTF-8");
char[] readBuffer = new char[4096];// 4KB
StringBuilder resultado = new StringBuilder();
int readLen;
while ((readLen = reader.read(readBuffer)) != -1) {
resultado.append(readBuffer, 0, readLen);
}
System.out.println(resultado);
return resultado.toString();
}
Answered By - Andrei Mărcuţ
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.