In this article i will show you how to send ‘url request’ in android.
We need 4 file here :
1) MainActivity.java
2) MyTask.java
3) AsyncTaskCompleteListener.java
4) Utils.java
Let’s make it !
1) MainActivity.java
Update MainActivity.java with the following script :
package com.moko.Webservice; // change with your package import android.os.Bundle; import android.app.Activity; import android.widget.Toast; public class MainActivity extends Activity implements AsyncTaskCompleteListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //http request new MyTask(this).execute("http://ambar-hasbiyatmoko.com/download.php?versi=2"); } // http response public void onTaskComplete(String result) { Toast.makeText(getApplicationContext(), "result : " + result, Toast.LENGTH_LONG).show(); } }
change http://ambar-hasbiyatmoko.com/download.php?versi=2 with your url.
2) MyTask.java
This class used to waiting the url request until completed.
package com.moko.Webservice; // change with your package import android.app.Activity; import android.app.ProgressDialog; import android.os.AsyncTask; public class MyTask extends AsyncTask<String, Void, String> { private Activity activity; private ProgressDialog dialog; private AsyncTaskCompleteListener callback; public MyTask(Activity act) { this.activity = act; this.callback = (AsyncTaskCompleteListener)act; } @Override protected void onPreExecute() { super.onPreExecute(); dialog = new ProgressDialog(activity); dialog.setMessage("Loading..."); dialog.show(); } @Override protected String doInBackground(String... params) { return Utils.getJSONString(params[0]); } @Override protected void onPostExecute(String result) { super.onPostExecute(result); if (null != dialog && dialog.isShowing()) { dialog.dismiss(); } callback.onTaskComplete(result); } }
3) AsyncTaskCompleteListener.java
package com.moko.Webservice; // change with your package interface AsyncTaskCompleteListener { public void onTaskComplete(String result); }
4) Utils.java
package com.moko.Webservice; // change with your package import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class Utils { public static String getJSONString(String url) { String result = null; HttpURLConnection linkConnection = null; try { URL linkurl = new URL(url); linkConnection = (HttpURLConnection) linkurl.openConnection(); int responseCode = linkConnection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { InputStream linkinStream = linkConnection.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int j = 0; while ((j = linkinStream.read()) != -1) { baos.write(j); } byte[] data = baos.toByteArray(); result = new String(data); } } catch (Exception e) { e.printStackTrace(); } finally { if (linkConnection != null) { linkConnection.disconnect(); } } return result; } }
CONFIGURE ANDROIDMANiFEST.XML
The last, you must add the following script to allow internet connection in your app :
<uses-permission android:name="android.permission.INTERNET" />
TEST APP
FINISH!!