Created
May 27, 2022 05:04
-
-
Save dmitryweiner/1d004458752abce118e5b0ce2891e9da to your computer and use it in GitHub Desktop.
Web API call from Android Java
This file contains 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
package com.dmitryweiner.myapplication; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.text.Editable; | |
import android.text.TextWatcher; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
/** | |
* Don't forget to add to manifest.xml | |
* <uses-permission android:name="android.permission.INTERNET" /> | |
*/ | |
public class MainActivity extends AppCompatActivity { | |
EditText editText; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
editText = findViewById(R.id.editTextNumber); | |
Button button = findViewById(R.id.button); | |
button.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
AsyncCaller asyncCaller = new AsyncCaller(); | |
asyncCaller.execute(); | |
} | |
}); | |
} | |
public String getJSON(String url, int timeout) { | |
HttpURLConnection c = null; | |
try { | |
URL u = new URL(url); | |
c = (HttpURLConnection) u.openConnection(); | |
c.setRequestMethod("GET"); | |
c.setRequestProperty("Content-length", "0"); | |
c.setUseCaches(false); | |
c.setAllowUserInteraction(false); | |
c.setConnectTimeout(timeout); | |
c.setReadTimeout(timeout); | |
c.connect(); | |
int status = c.getResponseCode(); | |
switch (status) { | |
case 200: | |
case 201: | |
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream())); | |
StringBuilder sb = new StringBuilder(); | |
String line; | |
while ((line = br.readLine()) != null) { | |
sb.append(line + "\n"); | |
} | |
br.close(); | |
return sb.toString(); | |
} | |
} catch (MalformedURLException ex) { | |
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); | |
} catch (IOException ex) { | |
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); | |
} finally { | |
if (c != null) { | |
try { | |
c.disconnect(); | |
} catch (Exception ex) { | |
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
} | |
return null; | |
} | |
private class AsyncCaller extends AsyncTask<Void, Void, Void> { | |
@Override | |
protected Void doInBackground(Void... voids) { | |
String response = getJSON("https://jsonplaceholder.typicode.com/users/" + editText.getText().toString(), 1000); | |
JSONObject jsonObject = null; | |
try { | |
jsonObject = new JSONObject(response); | |
Logger.getLogger(getClass().getName()).log(Level.INFO, jsonObject.getString("name")); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment