Reputation: 11
I am programming an app that uses HttpClient to connect to a web page (the aim is to be able to copy some of the HTML of a webpage into a String). I tried accomplishing this by using an HttpClient connection. This is the code I used:
public void getText() {
final TextView contentView = (TextView) findViewById(R.id.textview);
String url = "http://www.anandtech.com";
/* An instance of this class will be registered as a JavaScript interface */
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
ResponseHandler<String> handler = new BasicResponseHandler();
try {
String response = client.execute(get, handler);
contentView.setText(response);
} catch (Exception e) {
// TODO Auto-generated catch block
contentView.setText(e.getMessage());
}
}
I found the source of the error by removing each statement of this block and then testing if the error still occurs and found that the error originated from the client.execute(get, handler). I have permission to use internet on AndroidManifest.xml, and I know the internet works anyway on the app because a WebView can load just fine. How can I make this work?
Also, I tried to get the String by injecting javascript into a webview url, with the following code:
MyJavaScriptInterface.class class MyJavaScriptInterface { private TextView contentView;
public MyJavaScriptInterface(TextView aContentView)
{
contentView = aContentView;
}
}
MainActivity.java @SuppressWarnings("unused")
public void processContent(String aContent)
{
final String content = aContent;
contentView.post(new Runnable()
{
public void run()
{
contentView.setText(content);
}
});
}
}
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new MyJavaScriptInterface(contentView),"INTERFACE");
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
webView.loadUrl("javascript:window.INTERFACE.processContent(
document.getElementsByTagName('p')[0].innerHTML);");
}
});
webView.loadUrl("http://tabshowdown.blogspot.com");
Both the class and the processContent() method were defined in the Activity's void onCreate method, FIY. In this case the problem is the javascript isn't initiating the processContent() method, since a. If I try to initialize any other javascript command, e.g. document.write(), the webview reacts, so javascript isn't the problem. If I try to initiate the MyJavaScriptInterface and the processContent() method from anywhere else on the script, it works just fine. So it appears to me as if the javascript is having trouble initiating the method (even though other users reported this method to work).
Can someone help me get either of these methods to work? Thanks in advance
Upvotes: 1
Views: 8583
Reputation: 1735
This should work:
public class MainActivity extends Activity{
private TextView contentView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(your layout);
contentView = (TextView) findViewById(R.id.textview);
DownloadTask task = new DownloadTask();
task.execute(url);
}
private class DownloadTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... urls) {
HttpResponse response = null;
HttpGet httpGet = null;
HttpClient mHttpClient = null;
String s = "";
try {
if(mHttpClient == null){
mHttpClient = new DefaultHttpClient();
}
httpGet = new HttpGet(urls[0]);
response = mHttpClient.execute(httpGet);
s = EntityUtils.toString(response.getEntity(), "UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
@Override
protected void onPostExecute(String result){
contentView.setText(result);
}
}
}
Upvotes: 1