我使用API来获取连接信息,但我无法从此API获取数据,因为我不知道如何使用HTTP获取连接输入用户名和密码来访问此数据。
主要
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(); }}
在浏览器中我可以输入用户名和密码,但在Android App中我不知道如何输入。同样这张照片。
答案 0 :(得分:3)
假设您的目标API需要基本身份验证,您需要将凭据添加到请求中,如下所示:
String user = "username";
String pwd = "password";
httpGet.addHeader("Authorization", "Basic " + Base64.encodeToString((user + ":" + pwd).getBytes(), Base64.NO_WRAP));
答案 1 :(得分:1)
只是提示,请使用AsyncTask
而不是停用StrictMode。
答案 2 :(得分:0)
尝试将您的凭据传递给设置credential provider的HTTP客户端,并指定您要使用它们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(); }}