How to use HTTP-GET connect with input username and password for access API
android, http, http-get, java
Solution
Assuming your target API requires Basic Authentication, you need to add the credentials to the request as follows:
String user = "username";
String pwd = "password";
httpGet.addHeader("Authorization", "Basic " + Base64.encodeToString((user + ":" + pwd).getBytes(), Base64.NO_WRAP));
Problem
I use API for connect information but I can't to get data from this API because I don' know How to use HTTP get connect with input username and password for access this data. Main ``` String url = "http://flightxml.flightaware.com/json/FlightXML2/"; @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); StrictMode.ThreadPolicy policy = new StrictMode. ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); TextView txt1 = (TextView)findViewById(R.id.textView1); String result = HttpGet(url); txt1.setText(result); } // Connect Http Get // public String HttpGet(String url) { StringBuilder builder = new StringBuilder(); HttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); try { HttpResponse response = client.execute(httpGet); StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); if (statusCode == 200) { // Status OK HttpEntity entity = response.getEntity(); InputStream content = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(content)); String line; while ((line = reader.readLine()) != null) { builder.append(line);} } else { Log.e("Log", "Failed to download result.."); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return builder.toString(); }} ``` In browser I can input username and password but in Android App I don't know How to input. same this pic. https://i.stack.imgur.com/63EBI.jpg