【问题标题】:Building URL For ListView With Picasso In Android App在 Android 应用程序中使用 Picasso 为 ListView 构建 URL
【发布时间】:2015-11-07 06:58:53
【问题描述】:

目标是使用 Picasso 显示电影数据库中的图像。

我正在从电影数据库访问 json。 这是有效的链接,

这是来自所有流行电影的 json

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

这是一部电影《星际穿越》的网址 http://image.tmdb.org/t/p/w185//nBNZadXqJSdt05SHLqgT0HuC5Gm.jpg

我想我需要类似的东西,

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

我很确定我构建的 url 不正确。如果有人可以查看该 url 并查看我是否正确处理它,我将非常感激。

处理这样返回的json,

在电影类中

 public String getPoster_path ()
    {
        return poster_path;
    }

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

我正在像这样构建这个链接,

接口api(使用api键调用base url之后),

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;
    }
}

在我的 main(调用基本 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);
    }
}

我的日志猫,

   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

【问题讨论】:

    标签: java android json


    【解决方案1】:

    据我所知,您的代码似乎是正确的,但加载图像的端点不正确。

    在您发布的链接中的 JSON 数据中,poster_path 是这样的:

    /jjBgi2r5cRt36xF6iNUEhzscEcb.jpg

    所以您的完整图片网址将如下所示:

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

    那是不正确,应该是

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

    地点:

    "http://image.tmdb.org/t/p/" ==&gt; is the host
    "w500" ==&gt; is the image width
    "/jjBgi2r5cRt36xF6iNUEhzscEcb.jpg" ==&gt; 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;
    }
    

    }

    【讨论】:

    • 我已经对这个问题进行了几次编辑,并更改了每个类中的 url 路径。您能否验证您的答案是否反映了我当前的 url 路径?当我将适配器类更改为您的答案时,它仍然无法运行。
    • 我一整天都在调试这个,我完全同意这是 url 键入路径以获取 json 与 api 密钥一起工作,所以我有没有 api 查询的基础并在 main 中键入,然后在api 我有后基,即查询加 api 键,然后是图像和 getPath_poster() 的 url,但仍然出现错误。我想它现在在 logcat 中一定是别的东西。
    • 这对你有意义吗? 11-06 17:17:23.315 1746-1746/? E/NetworkScheduler.SchedulerReceiver:无效的参数应用程序 11-06 17:17:23.315 1746-1746/? E/NetworkScheduler.SchedulerReceiver:无效的包名称:也许您没有在附加内容中包含 PendingIntent? 11-06 17:17:23.322 3556-3572/? D/: HostConnection::get() 新主机连接建立 0xb3fdcf10, tid 3572
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-12
    • 2012-04-02
    • 2012-03-28
    • 1970-01-01
    • 2019-01-14
    相关资源
    最近更新 更多