简体   繁体   English

Asynctask:Json返回移动数据

[英]Asynctask: Json return with Mobile data

I have a problem with mobile data using Asynctask . 我对使用Asynctask的移动数据有问题。 If i use Wifi , the problem doesn't appear. 如果我使用Wifi ,则不会出现问题。 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: 在此错误中,响应不完整(我尝试将“ len”更改为“ 5000000”),但错误相同:

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: 这里有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. 谢谢。

Your readIt method is bugged. 您的readIt方法有问题。 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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM