【问题标题】:How to use HTTP-GET connect with input username and password for access API如何使用 HTTP-GET 连接输入用户名和密码来访问 API
【发布时间】:2014-02-23 11:11:24
【问题描述】:

我使用 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 应用程序中我不知道如何输入。和这张照片一样。

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

【问题讨论】:

  • 您可以直接将用户名和密码传递到字符串中,例如:String url = "http://www.internet.com/api?UserName=YourUsername&Password=yourpassword" 或另一种方式是将用户名和密码传递到 Headers 中,例如:request.addHeader("UserName", username); request.addHeader("Password", password);

标签: java android http http-get


【解决方案1】:

假设您的目标 API 需要基本身份验证,您需要将凭据添加到请求中,如下所示:

String user = "username";
String pwd = "password";
httpGet.addHeader("Authorization", "Basic " + Base64.encodeToString((user + ":" + pwd).getBytes(), Base64.NO_WRAP));

【讨论】:

  • 我在我的 MySQL 数据库前面设置了一个基本的sandman REST API,并且在终端中执行curl http://user:pwd@myserver.com 可以工作,但不能在我的 Android 应用程序中工作。这个答案帮助了我。谢谢!
  • 你需要导入 => import org.apache.commons.codec.binary.Base64
【解决方案2】:

提示,请使用 AsyncTask 而不是停用 StrictMode。

【讨论】:

    【解决方案3】:

    尝试将您的凭据传递给设置您的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(); }}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-30
      • 2013-06-10
      • 2015-03-13
      • 2020-12-09
      • 2015-03-27
      • 2016-06-25
      • 2012-08-09
      • 1970-01-01
      相关资源
      最近更新 更多