Last active
April 19, 2024 14:14
-
-
Save moh-affan/55dce191faae295c6604ff09b4c846fa to your computer and use it in GitHub Desktop.
Traditional android using java to perform http task
This file contains hidden or 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 android.content.Context; | |
| import org.json.JSONException; | |
| import org.json.JSONObject; | |
| import java.io.BufferedReader; | |
| import java.io.DataOutputStream; | |
| import java.io.InputStreamReader; | |
| import java.net.HttpURLConnection; | |
| import java.net.URL; | |
| import java.util.concurrent.Executor; | |
| import java.util.concurrent.Executors; | |
| public class HttpClient { | |
| public interface OnResponseListener { | |
| void onResponse(JSONObject jsonResponse); | |
| } | |
| private final Executor executor = Executors.newCachedThreadPool(); | |
| public void get(String urlString, Context context, Runnable loadingCode, OnResponseListener listener) { | |
| executor.execute(() -> { | |
| HttpURLConnection connection = null; | |
| BufferedReader reader = null; | |
| try { | |
| URL url = new URL(urlString); | |
| connection = (HttpURLConnection) url.openConnection(); | |
| connection.setRequestMethod("GET"); | |
| // Read the response | |
| reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); | |
| StringBuilder response = new StringBuilder(); | |
| String line; | |
| while ((line = reader.readLine()) != null) { | |
| response.append(line); | |
| } | |
| JSONObject jsonResponse = new JSONObject(response.toString()); | |
| if (listener != null) { | |
| listener.onResponse(jsonResponse); | |
| } | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } finally { | |
| if (connection != null) { | |
| connection.disconnect(); | |
| } | |
| try { | |
| if (reader != null) { | |
| reader.close(); | |
| } | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| }); | |
| } | |
| public void post(String urlString, JSONObject jsonBody, Context context, Runnable loadingCode, OnResponseListener listener) { | |
| executor.execute(() -> { | |
| HttpURLConnection connection = null; | |
| BufferedReader reader = null; | |
| try { | |
| URL url = new URL(urlString); | |
| connection = (HttpURLConnection) url.openConnection(); | |
| connection.setRequestMethod("POST"); | |
| connection.setRequestProperty("Content-Type", "application/json"); | |
| connection.setDoOutput(true); | |
| // Write the JSON body to the output stream | |
| DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream()); | |
| outputStream.writeBytes(jsonBody.toString()); | |
| outputStream.flush(); | |
| outputStream.close(); | |
| // Read the response | |
| reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); | |
| StringBuilder response = new StringBuilder(); | |
| String line; | |
| while ((line = reader.readLine()) != null) { | |
| response.append(line); | |
| } | |
| JSONObject jsonResponse = new JSONObject(response.toString()); | |
| if (listener != null) { | |
| listener.onResponse(jsonResponse); | |
| } | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } finally { | |
| if (connection != null) { | |
| connection.disconnect(); | |
| } | |
| try { | |
| if (reader != null) { | |
| reader.close(); | |
| } | |
| } catch (Exception e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| }); | |
| } | |
| } |
This file contains hidden or 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 android.content.Context; | |
| import android.os.Bundle; | |
| import android.support.v7.app.AppCompatActivity; | |
| import android.util.Log; | |
| import org.json.JSONException; | |
| import org.json.JSONObject; | |
| public class MainActivity extends AppCompatActivity { | |
| private static final String TAG = "MainActivity"; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| // Example GET request | |
| HttpClient httpClient = new HttpClient(); | |
| httpClient.get("https://jsonplaceholder.typicode.com/posts/1", this, () -> { | |
| // Show loading dialog | |
| Log.d(TAG, "Loading..."); | |
| }, new HttpClient.OnResponseListener() { | |
| @Override | |
| public void onResponse(JSONObject jsonResponse) { | |
| // Hide loading dialog | |
| Log.d(TAG, "GET Response: " + jsonResponse); | |
| // Handle response | |
| if (jsonResponse != null) { | |
| try { | |
| String title = jsonResponse.getString("title"); | |
| Log.d(TAG, "Title: " + title); | |
| } catch (JSONException e) { | |
| e.printStackTrace(); | |
| } | |
| } else { | |
| // Handle error | |
| Log.e(TAG, "GET Request failed"); | |
| } | |
| } | |
| }); | |
| // Example POST request | |
| JSONObject postBody = new JSONObject(); | |
| try { | |
| postBody.put("userId", 1); | |
| postBody.put("id", 1); | |
| postBody.put("title", "My title"); | |
| postBody.put("body", "My body"); | |
| } catch (JSONException e) { | |
| e.printStackTrace(); | |
| } | |
| httpClient.post("https://jsonplaceholder.typicode.com/posts", postBody, this, () -> { | |
| // Show loading dialog | |
| Log.d(TAG, "Loading..."); | |
| }, new HttpClient.OnResponseListener() { | |
| @Override | |
| public void onResponse(JSONObject jsonResponse) { | |
| // Hide loading dialog | |
| Log.d(TAG, "POST Response: " + jsonResponse); | |
| // Handle response | |
| if (jsonResponse != null) { | |
| // Handle successful post | |
| } else { | |
| // Handle error | |
| Log.e(TAG, "POST Request failed"); | |
| } | |
| } | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment