解决方案 »

  1.   

    你连接的是 127.0.0.1 ? tomcat 也装在手机上了?
      

  2.   

    没有,是在电脑上装了,在电脑上android虚拟机运行的,感觉可能是路径不对,但是不知道怎么 弄
      

  3.   

    没有,是在电脑上装了,在电脑上android虚拟机运行的,感觉可能是路径不对,但是不知道怎么 弄
      

  4.   

    虚拟机访问127.0.0.1应该是访问不到你的电脑上的tomcat的
      

  5.   

    好吧 ,参考http://zhidao.baidu.com/link?url=Kg8b6m5OkTq7-oNXjDZt9ydDGCVGCZt-DaYEstFiP2tHsFVy1w1REYvlZkwFWns6d5a6dGO9-aAAG0iJvtC01q这个问题 ,我把自己的ip换上去了
    换后的地址url如下:http://10.28.20.74:8080/setup.exe
    然后访问是应该成功了 但是报出了新的错误
    11-22 12:55:17.342: I/System.out(643): 服务器出错
    11-22 12:55:17.363: W/System.err(643): java.lang.NullPointerException
    11-22 12:55:17.373: W/System.err(643):  at libcore.net.http.HttpConnection$Address.hashCode(HttpConnection.java:343)
    11-22 12:55:17.383: W/System.err(643):  at java.util.HashMap.get(HashMap.java:298)
    11-22 12:55:17.383: W/System.err(643):  at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:67)
    11-22 12:55:17.383: W/System.err(643):  at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
    11-22 12:55:17.383: W/System.err(643):  at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
    11-22 12:55:17.383: W/System.err(643):  at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
    11-22 12:55:17.393: W/System.err(643):  at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
    11-22 12:55:17.393: W/System.err(643):  at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
    11-22 12:55:17.413: I/WindowManager(78): createSurface Window{412f6a48 Toast paused=false}: DRAW NOW PENDING
    11-22 12:55:17.413: W/System.err(643):  at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
    11-22 12:55:17.413: W/System.err(643):  at com.myy.dowloaderforandrop.MainActivity$2.run(MainActivity.java:61)
    11-22 12:56:26.173: D/dalvikvm(643): GC_CONCURRENT freed 210K, 4% free 7104K/7367K, paused 5ms+3ms
    11-22 12:56:28.192: D/dalvikvm(171): GC_CONCURRENT freed 384K, 8% free 7358K/7943K, paused 4ms+4ms
    11-22 12:59:11.422: W/ThrottleService(78): unable to find stats for iface rmnet0
    好吧,如果还有人看的话,再帮一下吧。。
      

  6.   

    看起来是你从HashMap取值的时候,传了个null进去
      

  7.   

    全代码贴上我本身函数是没用,应该是java函数本身调用的HashMap
    package com.myy.dowloaderforandrop;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.RandomAccessFile;
    import java.net.HttpURLConnection;
    import java.net.URL;import android.os.Message;
    public class DownloadThread extends Thread { public int id,start,end;
    public String path;
    /**
     * @param path 下载路径
     * @param id 线程id
     * @param start 开始下载位置
     * @param end 结束下载位置
     */
    public DownloadThread(String path,int id, int start, int end) {
    this.id = id;
    this.start = start;
    this.end = end;
    this.path=path;
    } public void run() {

    try {

    File check = new File("/sdcard/"+id+".txt");
    byte [] temp = new byte[1024];
    int downloaded_len=0;
    if(check.exists()&&check.length()>0)
    {
    FileInputStream fis = new FileInputStream(check);
    int temp_len;
    temp_len = fis.read(temp);
    String down_str = new String(temp,0,temp_len); 
    downloaded_len = Integer.parseInt(down_str);
    System.out.println("线程"+id+" 已下载: "+downloaded_len);
    fis.close();
    }

    URL url;
    url = new URL(path);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    //重要:请求服务器只是下载部分内容且设置下载位
    conn.setRequestProperty("Range","bytes="+(start+downloaded_len)+"-"+end);
    System.out.println("线程"+id+" 下载位置: "+(start+downloaded_len));
    conn.setConnectTimeout(5000);
    conn.setRequestMethod("GET");

    //服务器请求全部资源成功返回200,请求部分资源成功返回203
    int code =conn.getResponseCode();
    System.out.println("线程"+id+" code: "+code);
    InputStream is = conn.getInputStream();
    RandomAccessFile raf = new RandomAccessFile("/sdcard/setup.exe","rwd");
    //  记录已经下载的位置到文件夹

    //定位
    raf.seek(start+downloaded_len);

    int len =0;

    byte [] buffer = new byte[1024];
    while((len=is.read(buffer))!=-1)
    {
    RandomAccessFile record = new RandomAccessFile("/sdcard/"+id+".txt","rwd");
    //只有一个参数的write,结束位置为buffer的长度(结尾),若buffer未装满
    //就会出错
    raf.write(buffer,0,len);
    downloaded_len+=len;
    //将下载长度放入记录文件
    record.write(String.valueOf(downloaded_len).getBytes());
    record.close();
    // System.out.println("线程"+id+" 下载位置: "+(start+downloaded_len));
    }
    is.close();
    raf.close();
    MainActivity.thread_num--;
    System.out.println("线程"+id+"下载完毕");
    } catch (Exception e) {
    System.out.println("线程"+id+"出错");
    // Message msg = new Message();
    // msg.what=MainActivity.ERROR;
    // msg.obj="线程"+id+"出错";
    // MainActivity.handler.sendMessage(msg);
    e.printStackTrace();
    }finally
    {
    System.out.println("还有"+MainActivity.thread_num+"个线程");
    if(MainActivity.thread_num==0)//所有线程已经完成
    {
    for(int id = 1;id<=MainActivity.thread_count;id++)
    {
    File dle_file = new File("/sdcard/"+id+".txt");
    dle_file.delete();
    System.out.println(id+".txt 已经删除");
    }
    }
    }

    }


    }
    package com.myy.dowloaderforandrop;import java.io.RandomAccessFile;
    import java.net.HttpURLConnection;
    import java.net.URL;import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.support.v7.app.ActionBarActivity;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ProgressBar;
    import android.widget.Toast;
    public class MainActivity extends ActionBarActivity {

    protected static final int ERROR = 0;
    public static int thread_count = 3;
    public static int thread_num = thread_count;
    EditText et;
    ProgressBar pb;
    Button but;

    public Handler handler = new Handler()
    {
    public void handleMessage(Message msg) {
    super.handleMessage(msg);
    if(msg.what==ERROR)
    {
    Toast.makeText(MainActivity.this,(String)msg.obj,Toast.LENGTH_SHORT).show();
    }
    }

    };
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            et = (EditText) findViewById(R.id.et_url);
            pb = (ProgressBar) findViewById(R.id.pb_down);
            but = (Button) findViewById(R.id.but_down);
        }    public void download(View view)
        {
         //连接服务器,获得文件长度并且在本地创建一个相同大小的文件
         new Thread()
         {
         public void run()
         {
         try {
        
         String path =et.getText().toString().trim();
         URL url = new URL(path);
         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
         conn.setConnectTimeout(1000);
         conn.setRequestMethod("GET");
         conn.connect();
         int code =conn.getResponseCode();
         int len;
         if(code==200)
         {
         len = conn.getContentLength();
         int blocklen = len/thread_count;
         System.out.println("文件总长度为"+len);
         //创建一个长度相同的文件
         RandomAccessFile raf = new RandomAccessFile("/sdcard/setup.exe","rwd");
         raf.setLength(len);
         raf.close();
        
         for(int id = 1;id<=thread_count;id++)
         {
         int start = (id-1)*blocklen;
         int end = id*blocklen-1;
         if(id==thread_count)//为最后一个线程时,使他的结尾为总长度 
         {
         end=len;
         }
         System.out.println("线程"+id+" start:"+start+" end: "+end);
         new DownloadThread(path,id,start,end).start();
         }
         }
         else
         {
         System.out.println("连接出错" );
         Message msg = new Message();
         msg.what=ERROR;
         msg.obj="连接出错";
         handler.sendMessage(msg);
         }
         } catch (Exception e) {
         System.out.println("服务器出错" );
         Message msg = new Message();
    msg.what=ERROR;
    msg.obj="服务器出错";
    handler.sendMessage(msg);
         e.printStackTrace();
         }
         }
         }.start();
        }
    }
    先贴着吧,以防有好心人帮我找......
    谢谢楼上了 ,我再检查下吧