在android中无法请求URL时显示错误消息
我有这个代码来请求一个URL,并通过文本视图在屏幕上显示结果。这是我的代码:在android中无法请求URL时显示错误消息,android,Android,我有这个代码来请求一个URL,并通过文本视图在屏幕上显示结果。这是我的代码: public class AsyncronoustaskAndroidExample extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
public class AsyncronoustaskAndroidExample extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.asyncronoustask_android_example);
final Button GetServerData = (Button) findViewById(R.id.GetServerData);
GetServerData.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String serverURL = "http://androidexample.com/media/webservice/getPage.php";
new LongOperation().execute(serverURL);
}
});
}
private class LongOperation extends AsyncTask<String, Void, Void> {
private final HttpClient Client = new DefaultHttpClient();
private String Content;
private String Error = null;
private ProgressDialog Dialog = new ProgressDialog(AsyncronoustaskAndroidExample.this);
TextView uiUpdate = (TextView) findViewById(R.id.output);
protected void onPreExecute() {
uiUpdate.setText("Output : ");
}
protected Void doInBackground(String... urls) {
try {
// Server url call by GET method
HttpGet httpget = new HttpGet(urls[0]);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
Content = Client.execute(httpget, responseHandler);
} catch (ClientProtocolException e) {
Error = e.getMessage();
cancel(true);
} catch (IOException e) {
Error = e.getMessage();
cancel(true);
}
return null;
}
protected void onPostExecute(Void unused) {
// NOTE: You can call UI Element here.
if (Error != null) {
uiUpdate.setText("Output : "+Error);
} else {
uiUpdate.setText("Output : "+Content);
}
}
}
公共类AsynchronousTaskAndRoideXample扩展活动{
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.AsynchronousTask\u android\u示例);
最后一个按钮GetServerData=(按钮)findViewById(R.id.GetServerData);
GetServerData.setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
字符串serverURL=”http://androidexample.com/media/webservice/getPage.php";
新建LongOperation().execute(服务器URL);
}
});
}
私有类LongOperation扩展了异步任务{
私有最终HttpClient客户端=新的DefaultHttpClient();
私有字符串内容;
私有字符串错误=null;
private ProgressDialog=新建ProgressDialog(AsyncronoustaskAndroidExample.this);
TextView uiUpdate=(TextView)findViewById(R.id.output);
受保护的void onPreExecute(){
uiUpdate.setText(“输出:”);
}
受保护的Void doInBackground(字符串…URL){
试试{
//通过GET方法调用服务器url
HttpGet HttpGet=新的HttpGet(URL[0]);
ResponseHandler ResponseHandler=新BasicResponseHandler();
Content=Client.execute(httpget、responseHandler);
}捕获(客户端协议例外e){
错误=e.getMessage();
取消(真);
}捕获(IOE异常){
错误=e.getMessage();
取消(真);
}
返回null;
}
受保护的void onPostExecute(未使用的void){
//注意:您可以在这里调用UI元素。
if(错误!=null){
uiUpdate.setText(“输出:+错误);
}否则{
uiUpdate.setText(“输出:+内容”);
}
}
}
}
但是当URL无效时,我的程序停止,我试图显示一条错误消息,但我不能。请帮帮我
非常感谢。请查看您的日志。大多数情况下,你肯定得到了一个非法的州例外。如果是这种情况,你也必须抓住它。如果你想检查你的url是否有效,你需要使用url。下面是一个例子
URL url;
try {
url = new URL(urls[0]);
HttpGet httpGet = new HttpGet(url.toString());
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
你说的程序停止是什么意思?