2016-04-30 7 views
-1

Hier ist mein Code zum Herunterladen von Informationen von einem Google-Blatt zu meiner App. mein Android Studio scheint diese Klasse nicht zu finden. Was kann ich bei diesem Problem tun? ich bin verloren. danke. nahm ich diesen Code aus diesem Tutorial: http://www.telerik.com/blogs/google-spreadsheet-as-data-source-androidkann Symbol 'AsyncResult' nicht lösen

package yrapps.szone; 
 
import android.os.AsyncTask; 
 

 
import org.json.JSONException; 
 
import org.json.JSONObject; 
 

 
import java.io.BufferedReader; 
 
import java.io.IOException; 
 
import java.io.InputStream; 
 
import java.io.InputStreamReader; 
 
import java.net.HttpURLConnection; 
 
import java.net.URL; 
 

 
public class DownloadWebpageTask extends AsyncTask<String, Void, String> { 
 
    AsyncResult callback; 
 

 
    public DownloadWebpageTask(AsyncResult callback) { 
 
     this.callback = callback; 
 
    } 
 

 
    @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 download the requested page."; 
 
     } 
 
    } 
 

 
    // onPostExecute displays the results of the AsyncTask. 
 
    @Override 
 
    protected void onPostExecute(String result) { 
 
     // remove the unnecessary parts from the response and construct a JSON 
 
     int start = result.indexOf("{", result.indexOf("{") + 1); 
 
     int end = result.lastIndexOf("}"); 
 
     String jsonResponse = result.substring(start, end); 
 
     try { 
 
      JSONObject table = new JSONObject(jsonResponse); 
 
      callback.onResult(table); 
 
     } catch (JSONException e) { 
 
      e.printStackTrace(); 
 
     } 
 
    } 
 

 
    private String downloadUrl(String urlString) throws IOException { 
 
     InputStream is = null; 
 

 
     try { 
 
      URL url = new URL(urlString); 
 
      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 responseCode = conn.getResponseCode(); 
 
      is = conn.getInputStream(); 
 

 
      String contentAsString = convertStreamToString(is); 
 
      return contentAsString; 
 
     } finally { 
 
      if (is != null) { 
 
       is.close(); 
 
      } 
 
     } 
 
    } 
 

 
    private String convertStreamToString(InputStream is) { 
 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
 
     StringBuilder sb = new StringBuilder(); 
 

 
     String line = null; 
 
     try { 
 
      while ((line = reader.readLine()) != null) { 
 
       sb.append(line + "\n"); 
 
      } 
 
     } catch (IOException e) { 
 
      e.printStackTrace(); 
 
     } finally { 
 
      try { 
 
       is.close(); 
 
      } catch (IOException e) { 
 
       e.printStackTrace(); 
 
      } 
 
     } 
 
     return sb.toString(); 
 
    } 
 
}

+0

Sie können jederzeit einen einfachen Handler verwenden, anstatt –

Antwort

1

Sie können das Symbol nicht lösen, weil Sie nicht die Klasse für AsyncResult auf Importe aufgenommen haben. AsyncResult ist kein standardmäßiger Android-Code. Wenn Sie zum GitHub-Repository gehen, das in Ihrem Link erwähnt wird, sehe ich, dass es sich um eine benutzerdefinierte Klasse handelt, die Teil des gleichen Pakets wie DownloadWebpageTask ist. Deshalb bekommt er es kostenlos. Aber Sie müssen AsyncResult selbst erstellen.

Hier ist seine Quelle: https://github.com/telerik/Android-samples/blob/master/Blogs/Json-Reader/app/src/main/java/com/example/progress/json_reader/AsyncResult.java

+0

danke danke danke. –