parent
7cefe84329
commit
76232ff9dc
@ -0,0 +1,30 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
package="com.hmkcode"
|
||||||
|
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.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>
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 50 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 7.5 KiB |
@ -0,0 +1,30 @@
|
|||||||
|
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:paddingBottom="@dimen/activity_vertical_margin"
|
||||||
|
android:paddingLeft="@dimen/activity_horizontal_margin"
|
||||||
|
android:paddingRight="@dimen/activity_horizontal_margin"
|
||||||
|
android:paddingTop="@dimen/activity_vertical_margin"
|
||||||
|
tools:context=".MainActivity" >
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tvIsConnected"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="@string/hello_world" />
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/btnRequest"
|
||||||
|
android:layout_width="200dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="center_horizontal"
|
||||||
|
android:text="Request"
|
||||||
|
android:layout_below="@+id/tvIsConnected"/>
|
||||||
|
|
||||||
|
<EditText
|
||||||
|
android:id="@+id/etRespose"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="400dp"
|
||||||
|
android:layout_below="@+id/btnRequest"/>
|
||||||
|
</RelativeLayout>
|
||||||
@ -0,0 +1,9 @@
|
|||||||
|
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
|
||||||
|
|
||||||
|
<item
|
||||||
|
android:id="@+id/action_settings"
|
||||||
|
android:orderInCategory="100"
|
||||||
|
android:showAsAction="never"
|
||||||
|
android:title="@string/action_settings"/>
|
||||||
|
|
||||||
|
</menu>
|
||||||
@ -0,0 +1,7 @@
|
|||||||
|
<resources>
|
||||||
|
|
||||||
|
<!-- Default screen margins, per the Android Design guidelines. -->
|
||||||
|
<dimen name="activity_horizontal_margin">16dp</dimen>
|
||||||
|
<dimen name="activity_vertical_margin">16dp</dimen>
|
||||||
|
|
||||||
|
</resources>
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<resources>
|
||||||
|
|
||||||
|
<string name="app_name">Clean HTTP AsyncTask</string>
|
||||||
|
<string name="action_settings">Settings</string>
|
||||||
|
<string name="hello_world">Hello world!</string>
|
||||||
|
|
||||||
|
</resources>
|
||||||
@ -0,0 +1,20 @@
|
|||||||
|
<resources>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
Base application theme, dependent on API level. This theme is replaced
|
||||||
|
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
|
||||||
|
-->
|
||||||
|
<style name="AppBaseTheme" parent="android:Theme.Light">
|
||||||
|
<!--
|
||||||
|
Theme customizations available in newer API levels can go in
|
||||||
|
res/values-vXX/styles.xml, while customizations related to
|
||||||
|
backward-compatibility can go here.
|
||||||
|
-->
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<!-- Application theme. -->
|
||||||
|
<style name="AppTheme" parent="AppBaseTheme">
|
||||||
|
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
|
||||||
|
</style>
|
||||||
|
|
||||||
|
</resources>
|
||||||
@ -0,0 +1,75 @@
|
|||||||
|
package com.hmkcode;
|
||||||
|
|
||||||
|
import org.apache.http.client.methods.HttpGet;
|
||||||
|
import org.apache.http.client.methods.HttpUriRequest;
|
||||||
|
|
||||||
|
import com.hmkcode.http.HttpHandler;
|
||||||
|
|
||||||
|
import android.net.ConnectivityManager;
|
||||||
|
import android.net.NetworkInfo;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.View.OnClickListener;
|
||||||
|
import android.widget.Button;
|
||||||
|
import android.widget.EditText;
|
||||||
|
import android.widget.TextView;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
public class MainActivity extends Activity implements OnClickListener {
|
||||||
|
|
||||||
|
private Button btnRequest;
|
||||||
|
private EditText etResponse;
|
||||||
|
private TextView tvIsConnected;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
setContentView(R.layout.activity_main);
|
||||||
|
|
||||||
|
tvIsConnected = (TextView) findViewById(R.id.tvIsConnected);
|
||||||
|
btnRequest = (Button) findViewById(R.id.btnRequest);
|
||||||
|
etResponse = (EditText) findViewById(R.id.etRespose);
|
||||||
|
|
||||||
|
if(isConnected()){
|
||||||
|
tvIsConnected.setBackgroundColor(0xFF00CC00);
|
||||||
|
tvIsConnected.setText("You are conncted");
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
tvIsConnected.setText("You are NOT conncted");
|
||||||
|
}
|
||||||
|
|
||||||
|
btnRequest.setOnClickListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
|
||||||
|
new HttpHandler() {
|
||||||
|
@Override
|
||||||
|
public HttpUriRequest getHttpRequestMethod() {
|
||||||
|
|
||||||
|
return new HttpGet("http://hmkcode.com/examples/index.php");
|
||||||
|
|
||||||
|
// return new HttpPost(url)
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void onResponse(String result) {
|
||||||
|
Toast.makeText(getBaseContext(), "Received!", Toast.LENGTH_LONG).show();
|
||||||
|
etResponse.setText(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
}.execute();
|
||||||
|
}
|
||||||
|
public boolean isConnected(){
|
||||||
|
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE);
|
||||||
|
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
|
||||||
|
if (networkInfo != null && networkInfo.isConnected())
|
||||||
|
return true;
|
||||||
|
else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,71 @@
|
|||||||
|
package com.hmkcode.http;
|
||||||
|
|
||||||
|
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.impl.client.DefaultHttpClient;
|
||||||
|
|
||||||
|
import com.hmkcode.http.HttpHandler;
|
||||||
|
|
||||||
|
import android.os.AsyncTask;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
public class AsyncHttpTask extends AsyncTask<String, Void, String>{
|
||||||
|
|
||||||
|
private HttpHandler httpHandler;
|
||||||
|
public AsyncHttpTask(HttpHandler httpHandler){
|
||||||
|
|
||||||
|
this.httpHandler = httpHandler;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String doInBackground(String... arg0) {
|
||||||
|
InputStream inputStream = null;
|
||||||
|
String result = "";
|
||||||
|
try {
|
||||||
|
|
||||||
|
// create HttpClient
|
||||||
|
HttpClient httpclient = new DefaultHttpClient();
|
||||||
|
|
||||||
|
// make the http request
|
||||||
|
HttpResponse httpResponse = httpclient.execute(httpHandler.getHttpRequestMethod());
|
||||||
|
|
||||||
|
// receive response as inputStream
|
||||||
|
inputStream = httpResponse.getEntity().getContent();
|
||||||
|
|
||||||
|
// convert inputstream to string
|
||||||
|
if(inputStream != null)
|
||||||
|
result = convertInputStreamToString(inputStream);
|
||||||
|
else
|
||||||
|
result = "Did not work!";
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.d("InputStream", e.getLocalizedMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
protected void onPostExecute(String result) {
|
||||||
|
httpHandler.onResponse(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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
package com.hmkcode.http;
|
||||||
|
|
||||||
|
import org.apache.http.client.methods.HttpUriRequest;
|
||||||
|
|
||||||
|
import com.hmkcode.http.AsyncHttpTask;
|
||||||
|
|
||||||
|
public abstract class HttpHandler {
|
||||||
|
|
||||||
|
public abstract HttpUriRequest getHttpRequestMethod();
|
||||||
|
|
||||||
|
public abstract void onResponse(String result);
|
||||||
|
|
||||||
|
public void execute(){
|
||||||
|
new AsyncHttpTask(this).execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue