3

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.sstatic.net/63EBI.jpg

1
  • You can directly passed username and password into string like: String url = "http://www.internet.com/api?UserName=YourUsername&Password=yourpassword" or another way is pass username and password into Headers like: request.addHeader("UserName", username); request.addHeader("Password", password); Commented Feb 23, 2014 at 11:22

3 Answers 3

3

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));
Sign up to request clarification or add additional context in comments.

2 Comments

I have a basic sandman REST API set up in front of my MySQL db, and doing curl http://user:[email protected] would work in terminal, but not in my Android app. This answer helped me. Thanks!
You need to import => import org.apache.commons.codec.binary.Base64
1

Just a tip, use AsyncTask instead of deactivating StrictMode.

Comments

0

Try passing your credentials to the HTTP client setting your credential provider, specifying you want to use them just for that server:

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();

// Set up the credential provider for that server and port
CredentialsProvider credentials = new BasicCredentialsProvider();
credProvider.setCredentials(new AuthScope("flightxml.flightaware.com", 80),
    new UsernamePasswordCredentials("username", "password"));

// and tell your client to use it
client.setCredentialsProvider(credentials);

// then fetch your data
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(); }}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.