Skip to content

Commit

Permalink
HTTP in fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
sillyjohn committed Apr 19, 2024
1 parent a93975f commit 46ddd37
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 32 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ dependencies {
implementation libs.navigation.fragment
implementation libs.navigation.ui
testImplementation libs.junit
testImplementation 'org.junit.jupiter:junit-jupiter:5.8.1'
androidTestImplementation libs.ext.junit
androidTestImplementation libs.espresso.core
//HTTP Request
Expand All @@ -52,5 +53,7 @@ dependencies {
implementation 'com.fasterxml.jackson.core:jackson-databind:2.17.0'
implementation 'com.squareup.okhttp3:okhttp:4.9.0'

testImplementation 'junit:junit:4.13.2'


}
38 changes: 6 additions & 32 deletions app/src/main/java/com/example/proj2/Classes/HTTP_Request.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.example.proj2.Classes;

import androidx.fragment.app.Fragment;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.logging.type.HttpRequest;
Expand All @@ -18,37 +22,7 @@
import okhttp3.OkHttpClient;

public final class HTTP_Request {
private HTTP_Request() {}

public static void requestURL(){
ObjectMapper objectMapper = new ObjectMapper();

try {
URL url = new URL("https://ddragon.leagueoflegends.com/cdn/14.6.1/data/en_US/champion.json");
HttpURLConnection client = (HttpURLConnection) url.openConnection();
client.setRequestProperty("Content-Type", "application/json");
client.setRequestProperty("Accept", "application/json");
client.setRequestMethod("POST");
client.setDoOutput(true);
try (BufferedReader br = new BufferedReader(
new InputStreamReader(client.getInputStream(), "utf-8"))) {

// on below line creating a string builder.
StringBuilder response = new StringBuilder();

// on below line creating a variable for response line.
String responseLine = null;

// on below line writing the response
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}

private HTTP_Request() {
}


}
}
4 changes: 4 additions & 0 deletions app/src/main/java/com/example/proj2/FirstFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import androidx.navigation.fragment.NavHostFragment;

import com.example.proj2.Classes.Champion;
import com.example.proj2.Classes.HTTP_Request;
import com.example.proj2.databinding.FragmentFirstBinding;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
Expand Down Expand Up @@ -64,6 +65,9 @@ public void onFailure(@NonNull Exception e) {
Log.w(TAG, "Error adding document", e);
}
});
//HTTP Request testing
// HTTP_Request.requestURL();

Champion champ = new Champion();
Toast myToast = Toast.makeText(getActivity(), champ.name, Toast.LENGTH_SHORT);
myToast.show();
Expand Down
35 changes: 35 additions & 0 deletions app/src/main/java/com/example/proj2/SecondFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@
import androidx.fragment.app.FragmentResultListener;
import androidx.navigation.fragment.NavHostFragment;

import com.android.volley.toolbox.JsonObjectRequest;
import com.example.proj2.databinding.FragmentSecondBinding;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONObject;

public class SecondFragment extends Fragment {

Expand All @@ -32,9 +41,35 @@ public void onFragmentResult(@NonNull String requestKey, @NonNull Bundle bundle)
System.out.print(result);
}
});
//getData();
return binding.getRoot();

}
// private void getData(){
// String url = "https://ddragon.leagueoflegends.com/cdn/14.6.1/data/en_US/champion.json";
//
// JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
// (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
//
// @Override
// public void onResponse(JSONObject response) {
// //textView.setText("Response: " + response.toString());
// System.out.print("Response received");
// }
// }, new Response.ErrorListener() {
//
// @Override
// public void onErrorResponse(VolleyError error) {
// // TODO: Handle error
//
// }
// });
//
//
//
//
//
// }

public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Expand Down

0 comments on commit 46ddd37

Please sign in to comment.