报异常java.net.MalformedURLException: no protocol:

Sniper663785 2017-09-25 07:59:34
安卓初学者求助
尝试弄一个能从网上下载特定资源的多线程下载器(输入url地址,以及文件名后就能下载文件)当我在布局文件中写死了固定的url地址时能够正常访问并成功下载到文件,但是当我在输入框中自己输入url地址时却报异常MalformedURLException: no protocol:

package com.getsre;

import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private static final String TAG = "com.getsre";
private String Urlpath;

String file = "data/data/com.getsre/";
private File f;
List list;
private int threadNum = 3;
private LinearLayout tv;
private EditText edname;
private EditText edurl;
private Button btgo;

Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
list = new ArrayList();
switch (msg.what) {
case 0:
list.add("线程0开始下载……");
break;
case 1:
list.add("线程1开始下载……");
break;
case 2:
list.add("线程2开始下载……" + "\n" + "文件大小:" + msg.obj);
break;
case 20:
list.add("线程0完成");
break;
case 21:
list.add("线程1完成");
break;
case 22:
list.add("线程2完成");
break;
default:
break;
}
for (Object Info : list) {
TextView ChildView;
ChildView = new TextView(MainActivity.this);
ChildView.setTextSize(23);
ChildView.setTextColor(Color.BLACK);
ChildView.setText(String.valueOf(Info));
tv.addView(ChildView);
}
}
};


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

tv = (LinearLayout) findViewById(R.id.tv);
edname = (EditText) findViewById(R.id.ed_name);
edurl = (EditText) findViewById(R.id.ed_url);
btgo = (Button) findViewById(R.id.bt);

Urlpath=edurl.getText().toString();
// Urlpath = String.valueOf(edurl.getText());
f = new File(file + String.valueOf(edname.getText()));
btgo.setOnClickListener(this);

}

@Override
public void onClick(View view) {
new Thread(new Runnable() {
@Override
public void run() {
test();
}
}).start();
}

public void test() {
try {
URL url = new URL(Urlpath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
int code = conn.getResponseCode();

if (code == 200) {
long size = conn.getContentLength();
long block = size / threadNum;

RandomAccessFile raf = new RandomAccessFile(f, "rw");
raf.setLength(size);
raf.close();
Log.i(TAG, "文件大小= " + size);


for (int i = 0; i < threadNum; i++) {
long startpos = i * block;
long endpos = block * (i + 1) - 1;
if (i == threadNum) {
endpos = size;
}
Log.i(TAG, "线程" + i + "下载的部分为:" + startpos + "---" + endpos);

Message msg = new Message();
msg.what = i;
String b = Convert(size);
msg.obj = b;
handler.sendMessage(msg);
new Download(i, startpos, endpos).start();
}
} else {
Log.i(TAG, "访问失败: responseCode = " + code);
}
} catch (Exception e) {
e.printStackTrace();
}
}

public String Convert(long size) {
if (size > 1024) {
long d = size / (1024 * 1024);
return d + "MB";
} else return size + "字节";
}

class Download extends Thread {
private int i = 0;
private long startpos;
private long endpos;

public Download(int i, long startpos, long endpos) {
this.i = i;
this.startpos = startpos;
this.endpos = endpos;
}

@Override
public void run() {
try {
URL url = new URL(Urlpath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);

conn.setRequestProperty("range","bytes="+startpos+"-"+endpos);
if (conn.getResponseCode()==206) {
InputStream is = conn.getInputStream();
RandomAccessFile raf = new RandomAccessFile(f, "rw");
raf.seek(startpos);
Read(is, raf);
Log.i(TAG,"线程" + i + "下载完毕...");

Message msg=new Message();
msg.what=i+20;
// msg.obj="下载完成";
handler.sendMessage(msg);
}
}catch (Exception e) {
e.printStackTrace();
}
}
}



public void Read(InputStream is, RandomAccessFile raf) {
int len = 0;
byte[] buffer = new byte[1024];
try {
while ((len = is.read(buffer)) != -1) {
raf.write(buffer, 0, len);
}
is.close();
} catch (Exception e) {
Log.i(TAG, "运行Read方法出现异常!!!!!!!!!!!!!!!");
e.printStackTrace();
}
}
}

布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.getsre.MainActivity">

<LinearLayout
android:layout_width="368dp"
android:layout_height="495dp"
android:orientation="vertical">

<ScrollView
android:layout_width="match_parent"
android:layout_height="370dp">
<LinearLayout
android:id="@+id/tv"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

</LinearLayout>

</ScrollView>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="URL地址:"/>
<EditText
android:id="@+id/ed_url"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:hint="输入URL地址"/>

<!--android:text="http://dlsw.baidu.com/sw-search-sp/soft/24/13406/XiuXiu_V4.0.1.2002_BdSetup.1437647987.exe"-->

</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="重命名:"/>
<EditText
android:id="@+id/ed_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:hint="重命名加上后缀"/>

<!--android:text="XiuXiu_V4.0.1.2002_BdSetup.1437647987.exe"-->

</LinearLayout>
</LinearLayout>
<Button
android:id="@+id/bt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="GO" />

</LinearLayout>
</android.support.constraint.ConstraintLayout>
...全文
2097 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sniper663785 2017-09-26
  • 打赏
  • 举报
回复
已经解决了, 是我在获取EditText中文本内容的时候出了问题,在点击事件之前获取输入框中的内容,但此时输入框中还没有输入任何内容
雨焰 2017-09-26
  • 打赏
  • 举报
回复
你的url前面是不是没有带http 或者 https ?

80,349

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧