Pergunta

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.

http://i.stack.imgur.com/63EBI.jpg

Foi útil?

Solução

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

Outras dicas

Just a tip, use AsyncTask instead of deactivating StrictMode.

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(); }}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top