首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >视频上传技术的改造--主干网

视频上传技术的改造--主干网
EN

Stack Overflow用户
提问于 2015-11-12 16:37:56
回答 3查看 156关注 0票数 0

我想上传一个视频(从库中选择)到服务器(localhost-Wampserver正在我的计算机上运行Apache2.4.4)。从异常和搜索中,我发现网络操作不应该发生在运行UI的线程中。但我该怎么做呢?我的代码如下:

PostFile

代码语言:javascript
复制
public final class PostFile 
{
  public static final MediaType MEDIA_TYPE_MARKDOWN
      = MediaType.parse("vide/mp4");

  private final OkHttpClient client = new OkHttpClient();

  public void run(String path) throws Exception 
  {
    File file = new File(path);

    Request request = new Request.Builder()
        .url("http://192.168.1.7")
        .post(RequestBody.create(MEDIA_TYPE_MARKDOWN, file))
        .build();

    Response response = client.newCall(request).execute();
    if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

        System.out.println(response.body().string());
  }

}

MainActivity.java

代码语言:javascript
复制
public class MainActivity extends Activity 
{
    private static int RESULT_LOAD_IMG = 1;
    String decodableString;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void loadImagefromGallery(View view) 
    {
        // Create intent to Open Image applications like Gallery, Google Photos
        Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
        // Start the Intent
        startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            // When a video is picked
            if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
                    && null != data) 
            {
                // Get the video from data
                Uri selectedVideo = data.getData();
                String[] filePathColumn = { MediaStore.Video.Media.DATA };
                Cursor cursor = getContentResolver().query(selectedVideo,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                decodableString = cursor.getString(columnIndex);
                Log.i("mok",decodableString);
                cursor.close();
                new PostFile().run(decodableString);
                Log.i("mok","done");
            } else 
            {
                Toast.makeText(this, "You haven't picked any video",
                        Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) 
        {
            e.printStackTrace();
            Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                        .show();
            }
        }
}

堆栈跟踪:

EN

回答 3

Stack Overflow用户

发布于 2015-11-12 16:40:41

AsyncTask提供了doInBackground方法。

http://developer.android.com/reference/android/os/AsyncTask.html

票数 3
EN

Stack Overflow用户

发布于 2015-11-12 16:53:07

你所需要做的就是了解如何使用Retrofit。以下是如何使用Retrofit上传文件以及如何进行上传的说明。

https://futurestud.io/blog/retrofit-how-to-upload-files/

应该使用回调机制在后台执行查询。查询在后台执行,回调在主线程上执行。

如何使用Square的Retrofit网络库实现异步回调

在这里使用-平方-改造-网络-库你可以找到用回调定义rest方法的很好的例子。

这是你需要的一切。甚至不要试图改变线程策略,这是强烈不推荐的。

票数 2
EN

Stack Overflow用户

发布于 2015-11-12 16:40:54

您需要使用AsyncTask来执行它

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33676636

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档