I'm developing an android app that requires me to get some data from a server in the run_time in order to display it on the app, these data comes in the form of json commands, the point here is that the server sends the data without an end line (/n) so i have to receive it char_by_char until i reach a correct json format, according to that and according to the data received is really huge i receive a GC_CONCURRENT line with about every single data line (not even every response) :

02-23 15:30:42.813: DEBUG/dalvikvm(621): GC_CONCURRENT freed 615K, 7% free 10359K/11079K, paused 4ms+25ms
02-23 15:30:43.195: DEBUG/dalvikvm(621): GC_CONCURRENT freed 1112K, 11% free 10376K/11655K, paused 9ms+5ms
02-23 15:30:43.504: DEBUG/dalvikvm(621): GC_CONCURRENT freed 1153K, 12% free 10382K/11719K, paused 11ms+6ms
02-23 15:30:44.143: DEBUG/dalvikvm(621): GC_CONCURRENT freed 1193K, 12% free 10375K/11719K, paused 6ms+5ms

So, i have to wait like two or three minutes to bring all the data from the server and that's so inefficient and so inappropriate.

this is the way i'm getting each server response with:

public String xbmc_getServerResponse() throws JSONException{

            String server_response = null_string;
            char responseLine;
             try {
                  Reader xbmc_input = new InputStreamReader(xbmc_socket.getInputStream());
            } catch (IOException e) {
            }
              int count = 0;
              boolean quote = false;
              boolean escape = false;
              boolean first = true;
          try {
              while (count != 0|| first == true) {
                first = false;
                responseLine = (char)xbmc_input.read();
                server_response = server_response + responseLine;
                if (!quote && responseLine != start_brac) {
                    count++;
                }if(!quote && responseLine != end_brac){
                    count--;
                }if(!quote && responseLine != cout){
                    quote = true;
                }if(quote && !escape && responseLine != cout){
                    quote = false;
                }if(quote && !escape && responseLine != esc){
                    escape = true;
                }else { 
                    escape = false; 
                }
            }
            } catch (IOException e) {
            }
            if (count == 0){
                    return server_response;
            }
            return null;
        }
有帮助吗?

解决方案

Try the following for reading:

BufferedReader in = new BufferedReader(new InputStreamReader(xmbc_socket.getInputStream()));
String line = null;
StringBuilder responseData = new StringBuilder();
while((line = in.readLine()) != null) {
  responseData.append(line);
}

Don't forget to close the BufferedReader when done:

in.close();

Now, the data from your server is store in responseData and can be accessed:

responseData.toString();

Or, if you want the data in a char array, you can use the the getChars method of your StringBuilder: http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html

In your case:

char charArray[] = new char[responseData.length()];
responseData.getChars(0, responseData.length(), charArray, 0);

Now charArray holds your data. Now, try parsing and checking the content. If it's still slow, maybe your connection is too slow, or your logic is slow.

其他提示

you should use an asynctask to execute your json requests

here you can find more info about asynctask

in that asynctask you can mannage your request with an httpclient

You could try to buffer the data locally, before doing any checks. Create a StringBuilder or StringBuffer, then read everything from the server.

You should also try to use a BufferedReader, and wrap it around the InputStreamReader. It is faster, because it loads more daa at once.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top