Vimeo获取请求405响应代码错误

时间:2016-07-26 06:50:00

标签: android vimeo vimeo-player vimeo-android

https://player.vimeo.com/video/136004191/config尝试在收到错误时从vimeo获取真实网址:

response code:405
07-26 11:42:46.236 22054-22183/com.sample.samplevideoview W/System.err: java.io.FileNotFoundException: https://player.vimeo.com/video/136004191/config
07-26 11:42:46.236 22054-22183/com.sample.samplevideoview W/System.err:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:238)
07-26 11:42:46.236 22054-22183/com.sample.samplevideoview W/System.err:     at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
07-26 11:42:46.236 22054-22183/com.sample.samplevideoview W/System.err:     at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java)
07-26 11:42:46.237 22054-22183/com.sample.samplevideoview W/System.err:     at com.sample.samplevideoview.AsyncHttpClient$AsyncDataRequestHideDialog.doInBackground(AsyncHttpClient.java:353)
07-26 11:42:46.237 22054-22183/com.sample.samplevideoview W/System.err:     at com.sample.samplevideoview.AsyncHttpClient$AsyncDataRequestHideDialog.doInBackground(AsyncHttpClient.java:287)
07-26 11:42:46.237 22054-22183/com.sample.samplevideoview W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:295)
07-26 11:42:46.237 22054-22183/com.sample.samplevideoview W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-26 11:42:46.237 22054-22183/com.sample.samplevideoview W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
07-26 11:42:46.237 22054-22183/com.sample.samplevideoview W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
07-26 11:42:46.237 22054-22183/com.sample.samplevideoview W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
07-26 11:42:46.237 22054-22183/com.sample.samplevideoview W/System.err:     at java.lang.Thread.run(Thread.java:818)
07-26 11:42:46.281 22054-22054/com.sample.samplevideoview E/MainActivity: Must implement the interface java.lang.NullPointerException: Attempt to invoke virtual method 'com.sample.samplevideoview.Request com.sample.samplevideoview.VimeoResponse.getRequest()' on a null object reference

这是代码:

    private void getRealUrl(String videoId)
        {
            AsyncHttpClient mAsyncHttpClient = new AsyncHttpClient();
            HashMap<String, String> params = new HashMap<>();
            String url = "https://player.vimeo.com/video/" + videoId + "/config";

            mAsyncHttpClient.postWithStringData(MainActivity.this, TAG,
                    url, StringConstants.METHOD_GET, params,false,
                    new AsyncHttpClient.AsyncHttpResponseCallBack() {
                        @Override
                        public void onResponse(String response) {
                            Gson gson = new GsonBuilder().create();
                            VimeoResponse movie = gson.fromJson(response, VimeoResponse.class);

                        }
                    }
        );
        }
public class AsyncHttpClient {

    public interface AsyncHttpResponseCallBack {
        void onResponse(String response);
    }

    private Vector<Object> params;
    private static final String UTF_ENCODING = "UTF-8";

    private static final int CONTEXT_INDEX = 0;
    private static final int TAG_INDEX = 1;
    private static final int URL_INDEX = 2;
    private static final int PARAMS_STRING_INDEX = 3;
    private static final int RESPONSE_CALLBACK_INDEX = 4;
    private static final int REQUEST_METHOD_INDEX = 5;
    private static final int SHOW_PROGRESS_DIALOG_INDEX = 6;
public void postWithStringData(Context context, String TAG, String url, String method,
                                   HashMap<String, String> hashMapParams, boolean showDialog,
                                   AsyncHttpResponseCallBack asyncHttpResponseCallBack) {
        if (Utilities.isConnected(context)) {
            try {
                params = new Vector<>();
                params.insertElementAt(context, CONTEXT_INDEX);
                params.insertElementAt(TAG, TAG_INDEX);
                params.insertElementAt(url, URL_INDEX);
                params.insertElementAt(getPostDataString(hashMapParams), PARAMS_STRING_INDEX);
                params.insertElementAt(asyncHttpResponseCallBack, RESPONSE_CALLBACK_INDEX);
                params.insertElementAt(method, REQUEST_METHOD_INDEX);
                new AsyncDataRequestHideDialog(context).execute(params);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
    }
private class AsyncDataRequestHideDialog extends AsyncTask<Vector<Object>, Void, String> {

        AlertDialog mAlert;
        String TAG, url;
        String paramsString;
        AsyncHttpResponseCallBack asyncHttpResponseCallBack;
        Context context;

        String method;
        public AsyncDataRequestHideDialog(Context context) {
            // TODO Auto-generated constructor stub
            this.context = context;
        }

        @Override
        protected String doInBackground(Vector<Object>... params) {
            Vector<Object> requestParams = params[0];
            TAG = (String) requestParams.get(TAG_INDEX);
            url = (String) requestParams.get(URL_INDEX);
            paramsString = (String) requestParams.get(PARAMS_STRING_INDEX);
            asyncHttpResponseCallBack = (AsyncHttpResponseCallBack) requestParams.get(RESPONSE_CALLBACK_INDEX);
            method = (String) requestParams.get(REQUEST_METHOD_INDEX);

            String result = "";
            URL requestURL;
            StringBuffer response;

            try {
                requestURL = new URL(url);
                HttpURLConnection conn = (HttpURLConnection) requestURL.openConnection();
                conn.setReadTimeout(StringConstants.READ_TIMEOUT);
                conn.setConnectTimeout(StringConstants.CONNECTION_TIMEOUT);
                conn.setRequestMethod(method);
                conn.setDoInput(true);
                conn.setDoOutput(true);
                OutputStream os = conn.getOutputStream();
                BufferedWriter writer = new BufferedWriter(
                        new OutputStreamWriter(os, UTF_ENCODING));
                writer.write(paramsString);
                writer.flush();
                writer.close();
                os.close();
                String line;
                response = new StringBuffer();
                Log.d(TAG,"response code:"+conn.getResponseCode());
                BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                while ((line = br.readLine()) != null) {
                    response.append(line);
                }
                result = response.toString();
            } catch (SocketTimeoutException e1) {
                e1.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();

            } finally {
                Log.d(TAG, result);
                return result;
            }
        }

        // Overriding onPreExecute to initialize and show progress dialog
        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();

        }
        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);

            // If result is not null
            if (result != null) {
                try {
                    // Making a log just to make sure of data.
                    Log.e(TAG, result);
                    asyncHttpResponseCallBack.onResponse(result);
                } catch (Exception e) {
                    Log.e(TAG, "Must implement the interface " + e.toString());
                }
            } else {
                mAlert = Utilities.createAlertDialog(context, "Error",
                        StringConstants.DATA_ERROR);
            }
        }
    }

当我得到响应代码405时?问题是什么?移动和桌面浏览器中的相同网址响应我。

2 个答案:

答案 0 :(得分:1)

我强烈建议使用官方vimeo-networking Java / Android库与Vimeo API进行对话。

图书馆将负责所有的网络逻辑(因此您不需要HttpURLConnection样板),它还提供通过URI获取视频的调用。

Here is how to request a single video。它基本上看起来像:

String uri = ...;// the video uri; if you have a Video, this is video.uri
VimeoClient.getInstance().fetchNetworkContent(uri, new ModelCallback<Video>(Video.class) {
     @Override
     public void success(Video video) {
          // use the video
     }

     @Override
     public void failure(VimeoError error) {
          // voice the error
     }
});

然后,如果您拥有该视频,则可以直接从库中的Video对象访问您想要的任何信息。 video.files之类的信息会返回VideoFile,以便您访问自己想要的链接。

答案 1 :(得分:0)

您的代码似乎有问题,因为对于简单的GET字符串请求OutputStreamBuffer类导致异常。试试这个简单的代码。工作得很好。

 @Override
            protected String doInBackground(Vector<Object>... params) {

                String result = "";
                url = "https://player.vimeo.com/video/136004191/config";

                try {

                    URL obj = new URL(url);

                    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

                    // optional default is GET
                    con.setRequestMethod("GET");
                    BufferedReader in = new BufferedReader(
                            new InputStreamReader(con.getInputStream()));
                    String inputLine;
                    StringBuffer response = new StringBuffer();

                    while ((inputLine = in.readLine()) != null) {
                        response.append(inputLine);
                    }
                    in.close();

                    //print result
                    System.out.println(response.toString());

                    result = response.toString();
                } catch (SocketTimeoutException e1) {
                    e1.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();

                } finally {
                    Log.d(TAG, result);
                    return result;
                }
            }