String str="";
mButton1 = (Button) findViewById(R.id.myButton1);
mButton1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
String uriAPI = "http://www.sina.com";
HttpPost httpRequest = new HttpPost(uriAPI);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("str", "post string"));
try {
httpRequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
String strResult = EntityUtils.toString(httpResponse.getEntity());
str=strResult;
} else {
str="Error Response: "+ httpResponse.getStatusLine().toString();
}
} catch (ClientProtocolException e) {
str=e.getMessage().toString();
e.printStackTrace();
} catch (IOException e) {
str=e.getMessage().toString();
e.printStackTrace();
} catch (Exception e) {
str=e.getMessage().toString();
e.printStackTrace();
}
}
});
if(str.equals("找不到网页"))
{
//输出101;
}
else
{
//输出100
}上面内容,str的结果为:找不到网页
但它扔然执行的输出100,这是为什么啊?