我正在尝试从本地服务器下载一个包,我为此传递了正确的 url,但它给出了异常,因为 IllegalStateException,目标主机不能为空或在参数中设置。
我的代码是
public class HeloAndroidActivity extends Activity{
Button btn;
String ss;
URI i;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// final String URL="http://www.xxx.com/webservice-demo/Package name.zip";
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getBaseContext(), "hii", Toast.LENGTH_LONG).show();
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
String path="http://www.xxx.com/webservice-demo/package name.zip";
try {
ss=URLEncoder.encode(path, "UTF-8");
} catch (UnsupportedEncodingException e1) {
Log.d("exception","coming");
e1.printStackTrace();
}
try {
i=new URI(ss);
} catch (URISyntaxException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpGet httpGet = new HttpGet(i);
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);
}
String result; //String result definieren
result = builder.toString(); //Hier wird der inhalt vom Stringbuilder zum String result hinzugefühgt
Log.d("packagedata", "coming " + result);
} else {
Log.e(HeloAndroidActivity.class.toString(), "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}