i want to run the script behind the button of Google search that means...posting a form to google server and getting the response in the form of html string and finally i put that string on webview to display result.... then i run the code ... some times it shows correct result (like someone click on google search button) but some times it shows me message to "force to close" without any changes i do in to code....that means prediction about output is unexpected... My code is like this......
public class url extends Activity
{
HttpResponse end = null;
String endResult = null;
WebView mWebView;
Intent myWebViewIntent;
public static final int TIMEOUT_MS=10000;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), TIMEOUT_MS);
HttpConnectionParams.setSoTimeout(client.getParams(), TIMEOUT_MS);
HttpPost post = new HttpPost("http://www.google.com/m");
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("hl", "en"));
pairs.add(new BasicNameValuePair("gl", "us"));
pairs.add(new BasicNameValuePair("source", "android-launcher-widget"));
pairs.add(new BasicNameValuePair("q", "persistent"));
try {
post.setEntity(new UrlEncodedFormEntity(pairs));
/* Uri uri = Uri.parse(post.toString());
TextView t1=new TextView(this);
t1.setText(post.getRequestLine().getUri());
this.setContentView(t1);*/
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Toast msg = Toast.makeText(url.this, "Message", Toast.LENGTH_LONG);
msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
msg.show();
HttpResponse response = client.execute(post);
end = response;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
//Toast.makeText(url.this, "problem in execute....", 20).show();
// TODO Auto-generated catch block
//put the balance is empty dialog box here.
e.printStackTrace();
}
BasicResponseHandler myHandler = n开发者_运维技巧ew BasicResponseHandler();
try {
// Log.i("file tag","we are out of url");
endResult = myHandler.handleResponse(end);
} catch (HttpResponseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
WebView engine = (WebView)findViewById(R.id.webview);
engine.loadDataWithBaseURL(post.getRequestLine().getUri(), endResult, "text/html", "UTF-8", null);
//engine.loadData(endResult, "text/html", "UTF-8");
engine.requestFocus(View.FOCUS_DOWN);
//engine.loadUrl(endResult);
}
}
so is there any problem in emulator or installation of SDk or program itself....? becoz it seems to be the connection with the internet is done but i use the proper permission.. thank you..
The ANR you get is because you're making a blocking call on the UI thread. To perform a blocking action without blocking the UI thread you have to use a thread and a handler, or (recommended) an AsyncTask
.
See Painless Threading.
精彩评论