对不起我完全是Http的新手,我几乎无法用我见过的示例写一个Http服务器,我试图用Picasso Lib从这个Http服务器下载图像,但它似乎不起作用,这是我的服务器:< / p>
我的内容是:D:\ Users \ Default,我试图将Default.jpg加载到iv:
public class SimpleHttpServer {
public static void main(String[] args) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress(8000), 0);
server.createContext("/info", new InfoHandler());
server.createContext("/get", new GetHandler());
server.setExecutor(null); // creates a default executor
server.start();
}
static class InfoHandler implements HttpHandler {
public void handle(HttpExchange t) throws IOException {
String response = "Use /get to download an Image";
t.sendResponseHeaders(200, response.length());
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
}
static class GetHandler implements HttpHandler {
public void handle(HttpExchange t) throws IOException {
// add the required response header for an Image file
Headers h = t.getResponseHeaders();
h.add("Content-Type", "image/jpg");
// a PDF (you provide your own!)
File file = new File ("D:/Users/Default/Default.jpg");
byte [] bytearray = new byte [(int)file.length()];
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(bytearray, 0, bytearray.length);
// ok, we are ready to send the response.
t.sendResponseHeaders(200, file.length());
OutputStream os = t.getResponseBody();
os.write(bytearray,0,bytearray.length);
os.close();
}
}
}
从毕加索加载它:
Picasso.with(getActivity()).load(Uri.parse("http://192.168.1.103:8000/D:/Users
/Default/Default.jpg")).into(iv);
这是我的错误,它们有多么有趣? :P
答案 0 :(得分:0)
首先,您对服务器将返回的文件进行了硬编码,因此您不需要Uri中的文件位置....您最后会使用/ get ...它甚至会这样说在您的代码中...... String response = "Use /get to download an Image";
。此外,您还需要添加:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
到您的清单文件,如果您还没有...发布日志会有用,不仅仅是问错了什么并且没有给出任何提示。