2016-04-30 82 views
-1

這裏是我的代碼,用於從谷歌表格下載信息到我的應用程序。 我的android studio似乎找不到這個類。 我能對這個問題做些什麼?我迷路了。 謝謝。 我把這個代碼從本教程: http://www.telerik.com/blogs/google-spreadsheet-as-data-source-android無法解析符號'AsyncResult'

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

你總是可以用一個簡單的處理程序,而不是 –

回答

1

無法解決的符號,因爲它沒有包括在您的進口AsyncResult類。 AsyncResult不是標準的Android代碼。如果你轉到鏈接中提到的GitHub倉庫,我會發現它是一個自定義類,它與DownloadWebpageTask是同一個包的一部分。這就是他爲什麼免費獲得它的原因。但是你需要自己創建AsyncResult。

這裏是他的來源:https://github.com/telerik/Android-samples/blob/master/Blogs/Json-Reader/app/src/main/java/com/example/progress/json_reader/AsyncResult.java

+0

謝謝謝謝謝謝。 –