Created
August 25, 2013 07:10
-
-
Save cirocosta/6332436 to your computer and use it in GitHub Desktop.
Simple HTML Reader and HTTP Post implementation on Java for Android.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedInputStream; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.util.concurrent.ExecutionException; | |
import android.app.ProgressDialog; | |
import android.content.Context; | |
import android.net.ConnectivityManager; | |
import android.net.NetworkInfo; | |
import android.os.AsyncTask; | |
public class HTMLPage { | |
Context mContext; | |
URL url; | |
HttpURLConnection mConn; | |
public HTMLPage(Context context, String endereco) { | |
super(); | |
this.mContext = context; | |
try { | |
this.url = new URL(endereco); | |
} catch (MalformedURLException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
public String getHtml() { | |
GetHtmlTask task = new GetHtmlTask(); | |
try { | |
String html = task.execute().get(); | |
return html; | |
} catch (InterruptedException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (ExecutionException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
public String getHtmlWithProgress(String message) { | |
GetHtmlTask task = new GetHtmlTask(true, message); | |
try { | |
String html = task.execute().get(); | |
return html; | |
} catch (InterruptedException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} catch (ExecutionException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
class GetHtmlTask extends AsyncTask<Void, Void, String> { | |
boolean progDialog = false; | |
private ProgressDialog pdia; | |
String message; | |
public GetHtmlTask() { | |
super(); | |
} | |
public GetHtmlTask(boolean progDialog, String message) { | |
super(); | |
this.progDialog = true; | |
if (message != null) { | |
this.message = message; | |
} else { | |
this.message = "Carregando"; | |
} | |
} | |
@Override | |
protected void onPreExecute() { | |
// TODO Auto-generated method stub | |
super.onPreExecute(); | |
pdia = new ProgressDialog(mContext); | |
pdia.setMessage(message); | |
pdia.show(); | |
} | |
@Override | |
protected String doInBackground(Void... params) { | |
// TODO Auto-generated method stub | |
if (isNetworkAvailable()) { | |
if (serverResponds()) { | |
StringBuilder sb = new StringBuilder(); | |
BufferedReader reader = null; | |
String line = null; | |
try { | |
InputStream in = new BufferedInputStream( | |
mConn.getInputStream()); | |
reader = new BufferedReader(new InputStreamReader(in)); | |
while ((line = reader.readLine()) != null) { | |
sb.append(line + "\n"); | |
} | |
return sb.toString(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
} | |
return null; | |
} | |
@Override | |
protected void onPostExecute(String result) { | |
// TODO Auto-generated method stub | |
super.onPostExecute(result); | |
pdia.dismiss(); | |
} | |
} | |
private boolean isNetworkAvailable() { | |
// TODO Auto-generated method stub | |
ConnectivityManager cm = (ConnectivityManager) mContext | |
.getSystemService(Context.CONNECTIVITY_SERVICE); | |
NetworkInfo networkInfo = cm.getActiveNetworkInfo(); | |
if (networkInfo != null && networkInfo.isConnected()) { | |
return true; | |
} | |
return false; | |
} | |
private boolean serverResponds() { | |
int http_status = 0; | |
try { | |
mConn = (HttpURLConnection) url.openConnection(); | |
http_status = mConn.getResponseCode(); | |
} catch (IOException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
if (http_status / 100 != 2) { | |
return false; | |
} else { | |
return true; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.io.UnsupportedEncodingException; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.net.URLEncoder; | |
import java.util.ArrayList; | |
import java.util.Scanner; | |
import android.os.AsyncTask; | |
import android.util.Log; | |
public class PostData { | |
URL url; | |
public PostData(String url) { | |
super(); | |
try { | |
this.url = new URL(url); | |
} catch (MalformedURLException e) { | |
e.printStackTrace(); | |
} | |
} | |
public void sendData(ArrayList<String> data) | |
throws UnsupportedEncodingException { | |
StringBuffer param = new StringBuffer(); | |
for (int i = 0; i < data.size(); i++) { | |
if (i == 0) { | |
param.append("param" + String.valueOf(i) + "="); | |
} else { | |
param.append("¶m" + String.valueOf(i) + "="); | |
} | |
param.append(URLEncoder.encode(data.get(i), "UTF-8")); | |
} | |
Log.v("params", param.toString()); | |
SendDataTask task = new SendDataTask(param.toString()); | |
task.execute(); | |
} | |
private class SendDataTask extends AsyncTask<Void, Void, String> { | |
String params; | |
public SendDataTask(String params) { | |
this.params = params; | |
} | |
@Override | |
protected void onPreExecute() { | |
super.onPreExecute(); | |
Log.v("SendDataTask", "initializing"); | |
} | |
@Override | |
protected String doInBackground(Void... arg0) { | |
try { | |
HttpURLConnection conn = (HttpURLConnection) url | |
.openConnection(); | |
conn.setDoOutput(true); | |
conn.setDoInput(true); | |
conn.setRequestMethod("POST"); | |
conn.setFixedLengthStreamingMode(params.getBytes().length); | |
conn.setRequestProperty("Content-Type", | |
"application/x-www-form-urlencoded"); | |
PrintWriter out = new PrintWriter(conn.getOutputStream()); | |
out.print(params); | |
out.close(); | |
// handle the response (opt) | |
String response = ""; | |
Scanner inStream = new Scanner(conn.getInputStream()); | |
while (inStream.hasNextLine()) { | |
response += (inStream.nextLine()); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
@Override | |
protected void onPostExecute(String result) { | |
super.onPostExecute(result); | |
Log.v("SendDataTask", "FINISHED"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment