2015-11-06 18 views
0

目標是使用Picasso從The Movie Database中顯示圖像。ListView的構建URL使用Picasso在Android App中

我正在從電影數據庫訪問json。 這是所有流行的電影

http://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=e2a8069c1e18c3b7545db574817b218e

這其中的作品的鏈接,

這是JSON是一個電影的URL星際 http://image.tmdb.org/t/p/w185//nBNZadXqJSdt05SHLqgT0HuC5Gm.jpg

我想,我需要這樣的東西,

url http://image.tmdb.org/t/p/w185/ 
getPoster_path() 

我很確定我是buil不正確的網址。我真的很感激,如果有人可以看看網址,看看我是否正確處理它。

處理JSON時返回這個樣子,

在電影類

public String getPoster_path() 
    { 
     return poster_path; 
    } 

    public void setPoster_path (String poster_path) 
    { 
     this.poster_path = poster_path; 
    } 

我建立這樣的這種聯繫,

接口(API)(後基本URL被調用的API密鑰) ,

public interface api { 

    @GET("/3/discover/movie?sort_by=popularity.desc&api_key=e2a8069c1e18c3b7545db574817b218e") 
    public void getData(Callback<List<Movie>> response); 

} 

適配器類(其中處理畢加索),

public class adapter extends ArrayAdapter<Movie> { 

    String url="http://image.tmdb.org/t/p/w185/"; 
    private Context context; 
    private List<Movie> movieList; 
    public adapter(Context context, int resource, List<Movie> objects) { 
     super(context, resource, objects); 
     this.context = context; 
     this.movieList = objects; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
     View view = inflater.inflate(R.layout.item_file,parent,false); 
     Movie movie = movieList.get(position); 
     TextView tv = (TextView) view.findViewById(R.id.name); 
     tv.setText(movie.getTitle()); 
     ImageView img = (ImageView) view.findViewById(R.id.img); 
     Picasso.with(getContext()).load(url+movie.getPoster_path()).resize(100,100).into(img); 
     return view; 
    } 
} 

在我的主(其中基本URL的叫法),

public class MainActivity extends ListActivity { 

    List<Movie> movieList; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     final RestAdapter restadapter = new RestAdapter.Builder().setEndpoint("http://api.themoviedb.org").build(); 

     api movieapi = restadapter.create(api.class); 

     movieapi.getData(new Callback<List<Movie>>() { 
      @Override 
      public void success(List<Movie> movies, Response response) { 
       movieList = movies; 
       adapter adapt = new adapter(getApplicationContext(),R.layout.item_file,movieList); 
       //ListView listView = (ListView) findViewById(R.id.list); 
       setListAdapter(adapt); 
      } 

      @Override 
      public void failure(RetrofitError error) { 
       Toast.makeText(getApplicationContext(),"Failed",Toast.LENGTH_SHORT).show(); 
      } 
     }); 


    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

我的logcat,

11-06 16:55:01.332 3269-3269/? D/AndroidRuntime: >>>>>> START com.android.internal.os.RuntimeInit uid 0 <<<<<< 
11-06 16:55:01.333 3269-3269/? D/AndroidRuntime: CheckJNI is ON 
11-06 16:55:01.342 3269-3269/? I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000 
11-06 16:55:01.345 3269-3269/? D/ICU: No timezone override file found: /data/misc/zoneinfo/current/icu/icu_tzdata.dat 
11-06 16:55:01.356 3269-3269/? E/memtrack: Couldn't load memtrack module (No such file or directory) 
11-06 16:55:01.356 3269-3269/? E/android.os.Debug: failed to load memtrack module: -2 
11-06 16:55:01.357 3269-3269/? I/Radio-JNI: register_android_hardware_Radio DONE 
11-06 16:55:01.365 3269-3269/? D/AndroidRuntime: Calling main entry com.android.commands.am.Am 
11-06 16:55:01.367 1280-1420/? I/ActivityManager: Force stopping com.wuno.moviesapp appid=10061 user=0: from pid 3269 
11-06 16:55:01.367 1280-1420/? I/ActivityManager: Killing 3224:com.wuno.moviesapp/u0a61 (adj 0): stop com.wuno.moviesapp 
11-06 16:55:01.370 1280-1291/? D/GraphicsStats: Buffer count: 2 
11-06 16:55:01.370 1280-1291/? I/WindowState: WIN DEATH: Window{7c3f7b2 u0 com.wuno.moviesapp/com.wuno.moviesapp.MainActivity} 
11-06 16:55:01.388 1280-1420/? W/ActivityManager: Force removing ActivityRecord{ab5c277 u0 com.wuno.moviesapp/.MainActivity t42}: app died, no saved state 
11-06 16:55:01.397 1280-1734/? W/ActivityManager: Spurious death for ProcessRecord{3ab6e1a 0:com.wuno.moviesapp/u0a61}, curProc for 3224: null 
11-06 16:55:01.400 3269-3269/? D/AndroidRuntime: Shutting down VM 
11-06 16:55:01.454 1942-1961/? W/EGL_emulation: eglSurfaceAttrib not implemented 
11-06 16:55:01.454 1942-1961/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaeef8e80, error=EGL_SUCCESS 
11-06 16:55:01.468 1280-1420/? W/InputMethodManagerService: Got RemoteException sending setActive(false) notification to pid 3224 uid 10061 
11-06 16:55:01.906 3279-3279/? D/AndroidRuntime: >>>>>> START com.android.internal.os.RuntimeInit uid 0 <<<<<< 
11-06 16:55:01.907 3279-3279/? D/AndroidRuntime: CheckJNI is ON 
11-06 16:55:01.912 3283-3283/? D/AndroidRuntime: >>>>>> START com.android.internal.os.RuntimeInit uid 0 <<<<<< 
11-06 16:55:01.913 3283-3283/? D/AndroidRuntime: CheckJNI is ON 
11-06 16:55:01.927 3279-3279/? I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000 
11-06 16:55:01.932 3283-3283/? I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000 
11-06 16:55:01.934 3279-3279/? D/ICU: No timezone override file found: /data/misc/zoneinfo/current/icu/icu_tzdata.dat 
11-06 16:55:01.943 3283-3283/? D/ICU: No timezone override file found: /data/misc/zoneinfo/current/icu/icu_tzdata.dat 
11-06 16:55:01.954 3279-3279/? E/memtrack: Couldn't load memtrack module (No such file or directory) 
11-06 16:55:01.954 3279-3279/? E/android.os.Debug: failed to load memtrack module: -2 
11-06 16:55:01.955 3279-3279/? I/Radio-JNI: register_android_hardware_Radio DONE 
11-06 16:55:01.967 3283-3283/? E/memtrack: Couldn't load memtrack module (No such file or directory) 
11-06 16:55:01.967 3283-3283/? E/android.os.Debug: failed to load memtrack module: -2 
11-06 16:55:01.968 3283-3283/? I/Radio-JNI: register_android_hardware_Radio DONE 
11-06 16:55:01.977 3279-3279/? D/AndroidRuntime: Calling main entry com.android.commands.wm.Wm 
11-06 16:55:01.979 3279-3279/? D/AndroidRuntime: Shutting down VM 
11-06 16:55:01.989 3279-3296/? E/art: Thread attaching while runtime is shutting down: Binder_1 
11-06 16:55:01.989 3279-3296/? I/AndroidRuntime: NOTE: attach of thread 'Binder_1' failed 
11-06 16:55:01.991 3283-3283/? D/AndroidRuntime: Calling main entry com.android.commands.am.Am 
11-06 16:55:01.995 1280-1415/? I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.wuno.moviesapp/.MainActivity} from uid 0 on display 0 
11-06 16:55:02.011 3283-3283/? D/AndroidRuntime: Shutting down VM 
11-06 16:55:02.020 1942-1961/? W/OpenGLRenderer: Incorrectly called buildLayer on View: ShortcutAndWidgetContainer, destroying layer... 
11-06 16:55:02.020 1942-1961/? W/OpenGLRenderer: Incorrectly called buildLayer on View: ShortcutAndWidgetContainer, destroying layer... 
11-06 16:55:02.046 3299-3299/? I/art: Not late-enabling -Xcheck:jni (already on) 
11-06 16:55:02.046 3299-3299/? I/art: Late-enabling JIT 
11-06 16:55:02.051 3299-3299/? I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000 
11-06 16:55:02.069 1280-1829/? I/ActivityManager: Start proc 3299:com.wuno.moviesapp/u0a61 for activity com.wuno.moviesapp/.MainActivity 
11-06 16:55:02.077 3299-3299/? W/System: ClassLoader referenced unknown path: /data/app/com.wuno.moviesapp-2/lib/x86 
11-06 16:55:02.127 941-996/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property 
11-06 16:55:02.127 3299-3314/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true 
11-06 16:55:02.129 3299-3299/? D/: HostConnection::get() New Host Connection established 0xabea8fd0, tid 3299 
11-06 16:55:02.164 1942-1961/? E/Surface: getSlotFromBufferLocked: unknown buffer: 0xa3651640 
11-06 16:55:02.181 3299-3314/? D/: HostConnection::get() New Host Connection established 0xb3fbc8a0, tid 3314 
11-06 16:55:02.187 3299-3314/? I/OpenGLRenderer: Initialized EGL, version 1.4 
11-06 16:55:02.197 3299-3314/? W/EGL_emulation: eglSurfaceAttrib not implemented 
11-06 16:55:02.197 3299-3314/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabebf120, error=EGL_SUCCESS 
11-06 16:55:02.228 1280-1299/? I/ActivityManager: Displayed com.wuno.moviesapp/.MainActivity: +186ms 
11-06 16:55:02.367 3299-3314/? W/EGL_emulation: eglSurfaceAttrib not implemented 
11-06 16:55:02.367 3299-3314/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabebf300, error=EGL_SUCCESS 
11-06 16:55:04.341 3299-3314/? E/Surface: getSlotFromBufferLocked: unknown buffer: 0xabdecce0 

回答

0

從我可以看到你的代碼似乎是正確的,但端點加載你的圖片是不正確的。

在您發佈poster_path鏈接JSON數據

是這樣的:

/jjBgi2r5cRt36xF6iNUEhzscEcb.jpg

讓你完整的圖像URL將是這樣的:

http://api.themoviedb.org/jjBgi2r5cRt36xF6iNUEhzscEcb.jpg

,這是不正確 ,應該是

http://image.tmdb.org/t/p/w500/jjBgi2r5cRt36xF6iNUEhzscEcb.jpg

其中:

"http://image.tmdb.org/t/p/" ==> is the host
"w500" ==> is the image width
"/jjBgi2r5cRt36xF6iNUEhzscEcb.jpg" ==> is the image path

所以你adaper類應該是這樣的:

public class adapter extends ArrayAdapter<Movie> { 

String url = "http://image.tmdb.org/t/p/"; 
int size = 500; // change this to whatever works for you 
private Context context; 
private List<Movie> movieList; 
public adapter(Context context, int resource, List<Movie> objects) { 
    super(context, resource, objects); 
    this.context = context; 
    this.movieList = objects; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
    View view = inflater.inflate(R.layout.item_file,parent,false); 
    Movie movie = movieList.get(position); 
    TextView tv = (TextView) view.findViewById(R.id.name); 
    tv.setText(movie.getTitle()); 
    ImageView img = (ImageView) view.findViewById(R.id.img); 
    Picasso.with(getContext()).load(url+size+movie.getPoster_path()).resize(100,100).into(img); 
    return view; 
} 

}

+0

我已經編輯了這個問題進行幾次,改變了網址每個班級的路徑。請你確認你的答案是否反映了我目前的網址路徑?當我將適配器類更改爲答案時,它仍然無法運行。 – wuno

+0

我一直在調試這一整天,我完全同意這是網址輸入路徑以獲得JSON作品與API密鑰,所以我有基地沒有API查詢和主要的關鍵,然後在API我有後基地是查詢加API鍵,然後是圖像和getPath_poster()的url,但仍然出現錯誤。我認爲它現在在logcat中一定是別的東西。 – wuno

+0

這對你有意義嗎? 11-06 17:17:23.315 1746-1746 /? E/NetworkScheduler.SchedulerReceiver:無效參數應用程序 11-06 17:17:23.315 1746-1746 /? E/NetworkScheduler.SchedulerReceiver:無效的軟件包名稱:或許您沒有在extras中包含PendingIntent? 11-06 17:17:23.322 3556-3572 /? D /:HostConnection :: get()建立新主機連接0xb3fdcf10,tid 3572 – wuno

相關問題