如何从PC通过HTTPGET获取文件在Android本地网络 [英] How to get file via HttpGet from pc in local network on android

查看:448
本文介绍了如何从PC通过HTTPGET获取文件在Android本地网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的电脑上一边收听HTTP请求运行C#程序和我尽量让这HTTPGET通过会从我的电脑文件的应用程序。

i have c# program running on my pc listenning http requests and i try to make an application which gets a file from my pc via HttpGet.

新HTTPGET(网址+文件路径);

new HttpGet(url + filepath);

文件是在相同的目录和路径为C://Users/abc/def/test.txt

the file is in the same directory and the path is C://Users/abc/def/test.txt

但如果我写这文件路径我不能获取文件。我应该写什么文件路径?

but if i write this to filepath i can not get the file. what should i write to filepath?

在此先感谢

推荐答案

您需要创建的AsyncTask做你的要求原因,因为Android蜂窝,不能添加在主线程上的网络请求。

You need to create an AsyncTask to do your request 'cause since Android Honeycomb, you cannot add a network request on the main thread.

下面是一个例子:

private class HttpGetter extends AsyncTask<URL, Void, Void> {

                @Override
                protected Void doInBackground(URL... urls) {
                        // TODO Auto-generated method stub
                        StringBuilder builder = new StringBuilder();
                        HttpClient client = new DefaultHttpClient();
                        HttpGet httpGet = new HttpGet(urls[0]);

                        try {
                                HttpResponse response = client.execute(httpGet);
                                StatusLine statusLine = response.getStatusLine();
                                int statusCode = statusLine.getStatusCode();
                                if (statusCode == 200) {
                                        HttpEntity entity = response.getEntity();
                                        InputStream content = entity.getContent();
                                        BufferedReader reader = new BufferedReader(
                                                        new InputStreamReader(content));
                                        String line;
                                        while ((line = reader.readLine()) != null) {
                                                builder.append(line);
                                        }
                                        Log.v("Getter", "Your data: " + builder.toString()); //response data
                                } else {
                                        Log.e("Getter", "Failed to download file");
                                }
                        } catch (ClientProtocolException e) {
                                e.printStackTrace();
                        } catch (IOException e) {
                                e.printStackTrace();
                        }

                        return null;
                }
    }

HttpGetter get = new HttpGetter();
get.execute("http://192.168.1.2/song.mp3");

这篇关于如何从PC通过HTTPGET获取文件在Android本地网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆