Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Android Connecting HTTP With AsyncTask Example, Original Post : http://devahoy.com/posts/android-asynctask-tutorial/
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1">
<EditText
android:id="@+id/text_url"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.8"/>
<Button
android:id="@+id/button_ok"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:text="OK"/>
</LinearLayout>
<WebView
android:id="@+id/webView_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.phonbopit.sample" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.phonbopit.sample.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.phonbopit.sample;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MainActivity extends ActionBarActivity {
EditText mUrl;
Button mButtonOK;
WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mUrl = (EditText) findViewById(R.id.text_url);
mButtonOK = (Button) findViewById(R.id.button_ok);
mWebView = (WebView) findViewById(R.id.webView_content);
mButtonOK.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/* AsyncHttpClient client = new AsyncHttpClient();
client.get(mUrl.getText().toString().trim(), new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
updateWebView(response);
}
});*/
new SimpleTask().execute(mUrl.getText().toString().trim());
}
});
}
private class SimpleTask extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
// Create Show ProgressBar
}
protected String doInBackground(String... urls) {
String result = "";
try {
HttpGet httpGet = new HttpGet(urls[0]);
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
InputStream inputStream = response.getEntity().getContent();
BufferedReader reader = new BufferedReader
(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
result += line;
}
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return result;
}
protected void onPostExecute(String result) {
// Dismiss ProgressBar
updateWebView(result);
}
}
private void updateWebView(String result) {
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadData(result, "text/html; charset=utf-8", "utf-8");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.