MainActivity.java
package
com.hmkcode.android;
import
java.io.BufferedReader;
import
java.io.IOException;
import
java.io.InputStream;
import
java.io.InputStreamReader;
import
org.apache.http.HttpResponse;
import
org.apache.http.client.HttpClient;
import
org.apache.http.client.methods.HttpGet;
import
org.apache.http.impl.client.DefaultHttpClient;
import
android.net.ConnectivityManager;
import
android.net.NetworkInfo;
import
android.os.AsyncTask;
import
android.os.Bundle;
import
android.util.Log;
import
android.widget.EditText;
import
android.widget.TextView;
import
android.widget.Toast;
import
android.app.Activity;
public
class
MainActivity
extends
Activity {
EditText etResponse;
TextView tvIsConnected;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
etResponse = (EditText) findViewById(R.id.etResponse);
tvIsConnected = (TextView) findViewById(R.id.tvIsConnected);
if
(isConnected()){
tvIsConnected.setBackgroundColor(
0xFF00CC00
);
tvIsConnected.setText(
"You are conncted"
);
}
else
{
tvIsConnected.setText(
"You are NOT conncted"
);
}
}
public
static
String GET(String url){
InputStream inputStream =
null
;
String result =
""
;
try
{
HttpClient httpclient =
new
DefaultHttpClient();
HttpResponse httpResponse = httpclient.execute(
new
HttpGet(url));
inputStream = httpResponse.getEntity().getContent();
if
(inputStream !=
null
)
result = convertInputStreamToString(inputStream);
else
result =
"Did not work!"
;
}
catch
(Exception e) {
Log.d(
"InputStream"
, e.getLocalizedMessage());
}
return
result;
}
private
static
String convertInputStreamToString(InputStream inputStream)
throws
IOException{
BufferedReader bufferedReader =
new
BufferedReader(
new
InputStreamReader(inputStream));
String line =
""
;
String result =
""
;
while
((line = bufferedReader.readLine()) !=
null
)
result += line;
inputStream.close();
return
result;
}
public
boolean
isConnected(){
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(
this
.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if
(networkInfo !=
null
&& networkInfo.isConnected())
return
true
;
else
return
false
;
}
private
class
HttpAsyncTask
extends
AsyncTask<String, Void, String> {
@Override
protected
String doInBackground(String... urls) {
return
GET(urls[
0
]);
}
@Override
protected
void
onPostExecute(String result) {
Toast.makeText(getBaseContext(),
"Received!"
, Toast.LENGTH_LONG).show();
etResponse.setText(result);
}
}
}
AndroidManifest.xml
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
package
=
"com.hmkcode.android"
android:versionCode
=
"1"
android:versionName
=
"1.0"
>
<
uses-sdk
android:minSdkVersion
=
"8"
android:targetSdkVersion
=
"17"
/>
<
uses-permission
android:name
=
"android.permission.INTERNET"
/>
<
uses-permission
android:name
=
"android.permission.ACCESS_NETWORK_STATE"
/>
<
application
android:allowBackup
=
"true"
android:icon
=
"@drawable/ic_launcher"
android:label
=
"@string/app_name"
android:theme
=
"@style/AppTheme"
>
<
activity
android:name
=
"com.hmkcode.android.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
>
XML parsing
HttpGet uri = new HttpGet("http://example.com");
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse resp = client.execute(uri);
StatusLine status = resp.getStatusLine();
if (status.getStatusCode() != 200) {
Log.d(tag, "HTTP error, invalid server status code: " + resp.getStatusLine());
}
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(resp.getEntity().getContent());
Basic AUTH
String auth = Base64Converter.encode(String.format("%s:%s", user, pass));
URL u = new URL(url);
conn = (HttpsURLConnection) u.openConnection();
conn.addRequestProperty("Authorization", "Basic " + auth);
No comments:
Post a Comment