从mysql下载数据到android时获取与javascript相关的错误 ...当我显示下载的字符串时,它显示了一个html页面,我将其包含在图像中...请尽快帮助我摆脱它。
已下载字符串“sb”在此处显示为Toast,它显示如下:
代码:
public class Download extends AsyncTask<Void,Integer,String>{
Context c;
String address;
ProgressDialog pd;
public Download(Context c,String address){
this.c=c;
this.address=address;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
pd.dismiss();
if(s!=null){
Toast.makeText(c,s,Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(c,"download failed",Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd=new ProgressDialog(c);
pd.setTitle("Download");
pd.setMessage("Fetching Data...");
pd.show();
}
@Override
protected String doInBackground(Void... params) {
String data=downloaddata();
return data;
}
public String downloaddata() {
InputStream is=null;
String line=null;
try {
URL url = new URL(address);
HttpURLConnection con=(HttpURLConnection) url.openConnection();
is=con.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is));
StringBuffer sb=new StringBuffer();
if(br!=null){
while ((line=br.readLine())!=null) {
sb.append(line+"\n");
}
br.close();is.close();con.disconnect();
}
else{
return null;
}
return sb.toString();
}
catch (MalformedURLException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
finally {
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}
答案 0 :(得分:0)
您应该定义打开连接的目的。
HttpURLConnection con=(HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
此外,您与连接无关。
你必须写
con.connect();
用于连接。
与网络连接后,您就可以使用代码获取流数据
InputStream inputStream = urlConnection.getInputStream();
这些可以帮到你。
答案 1 :(得分:0)
这不是错误与Web scrapping相关的事情 有关详情,请查看此thread