2017-01-09 112 views
0

我在使用Ion從我的API調用接收數據時遇到問題。我正在通過Udacity tutorial工作,它使用一堆樣板代碼從API接收數據。我想走一條不同的路線 - 主要是學習 - 並使用Ion來提出我的網絡請求。產生空離子結果

我所做的事情:

  1. 改變的離子呼叫
  2. 驗證API調用的互聯網瀏覽器的URL
  3. 放置離子結果內的上下文參數if語句,以防止應用程序從如果崩潰的結果是空
  4. 新增setLogging離子請求
  5. 添加的網絡權限在AndroidManifest

不幸的是,我仍然沒有收到任何Android監視器。

package com.example.android.sunshine.app; 

import android.app.Fragment; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

import com.koushikdutta.async.future.FutureCallback; 
import com.koushikdutta.ion.Ion; 

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

import java.util.ArrayList; 

/** 
* Created by Chasen on 1/7/2017. 
*/ 

public class ForecastFragment extends Fragment { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setHasOptionsMenu(true); 
    } 

    private ArrayAdapter<String> forecastAdapter; 
    public static final String LOG_TAG = ForecastFragment.class.getSimpleName(); 
    public static final String baseURL = "api.openweathermap.org/data/2.5/weather?q=London"; 
    public static final String api_key = "&APPID=" + BuildConfig.OPEN_WEATHER_API_KEY; 

    public ForecastFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_main, container, false); 

     ListView listView = (ListView) rootView.findViewById(R.id.list_view_forecast); 

     ArrayList<String> forecastEntries = new ArrayList<String>(); 
     forecastEntries.add("Today - Sunny - 88/63"); 
     forecastEntries.add("Tomorrow - Foggy - 70/46"); 
     forecastEntries.add("Wed - Cloudy - 72/63"); 
     forecastEntries.add("Thurs - Rainy - 64/52"); 
     forecastEntries.add("Fri - Foggy - 70/46"); 
     forecastEntries.add("Sat - Sunny - 76/68"); 

     forecastAdapter = new ArrayAdapter<String>(rootView.getContext() 
       ,R.layout.list_item_forecast 
       ,R.id.list_item_forecast_textview 
       ,forecastEntries); 

     /* 
     {"coord":{"lon":-0.13,"lat":51.51} 
     ,"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10d"}] 
     ,"base":"stations" 
     ,"main":{"temp":281.82,"pressure":1012,"humidity":93,"temp_min":281.15,"temp_max":282.15} 
     ,"visibility":10000 
     ,"wind":{"speed":7.7,"deg":190} 
     ,"clouds":{"all":75},"dt":1483969800 
     ,"sys":{"type":1,"id":5187 
     ,"message":0.2086 
     ,"country":"GB","sunrise":1483948990,"sunset":1483978386},"id":2643743,"name":"London","cod":200} 

     */ 

     listView.setAdapter(forecastAdapter); 
     return rootView; 
    } 

    private static void logResult(String result) { 
     try { 
      JSONObject json = new JSONObject(result); 
      Log.v(LOG_TAG, json.toString()); 
     } 
     catch (JSONException e) { 
      Log.wtf(LOG_TAG, e); 
     } 

    } 

    @Override 
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
     inflater.inflate(R.menu.forecastfragment, menu); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 

     int id = item.getItemId(); 
     if(id == R.menu.forecastfragment) { 
      Ion.with(this) 
        .load(baseURL.concat(api_key)) 
        .setLogging(LOG_TAG, Log.DEBUG) 
        .asString() 
        .setCallback(new FutureCallback<String>() { 
         @Override 
         public void onCompleted(Exception e, String result) { 
          if(result != null) { 
           Log.v(LOG_TAG, result); 
          } 
          //logResult(result); 
         } 
        }); 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

回答

0

需要將http://添加到API密鑰。