HttpClient无法从URI获取登录信用。
你必须给他们指定的方法。
如果使用的HttpClient 4.x中对此有看:
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html
但请注意,如果你不希望使用在HttpClient的新版本(Android使用版本3。 X),你应该看看这里:
http://hc.apache.org/httpclient-3.x/authentication.html
这是理论,现在我们使用它们:
基本上w^e使用HTTP,但是如果要使用HTTPS,则必须将以下分配new HttpHost("www.google.com", 80, "http")
编辑为new HttpHost("www.google.com", 443, "https")
。
此外,您必须编辑主机(www.google.com)以解决您的问题。
注意:只需要完整合格的域名(FQDN)而不是完整的URI。
的HttpClient 3.X:
package com.test;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.os.Bundle;
public class Test2aActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
HttpHost targetHost = new HttpHost("www.google.com", 80, "http");
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
// Store the user login
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials("user", "password"));
// Create request
// You can also use the full URI http://www.google.com/
HttpGet httpget = new HttpGet("/");
// Execute request
HttpResponse response = httpclient.execute(targetHost, httpget);
HttpEntity entity = response.getEntity();
System.out.println(EntityUtils.toString(entity));
} finally {
httpclient.getConnectionManager().shutdown();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
的HttpClient 4.x版:
注意:您将需要新HttpClient的Apache的和此外,您必须重新排列顺序,即jar文件位于Android库之前。
package com.test;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.os.Bundle;
public class TestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
HttpHost targetHost = new HttpHost("www.google.com", 80, "http");
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
// Store the user login
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(targetHost.getHostName(), targetHost.getPort()),
new UsernamePasswordCredentials("user", "password"));
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local
// auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
// Add AuthCache to the execution context
BasicHttpContext localcontext = new BasicHttpContext();
localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);
// Create request
// You can also use the full URI http://www.google.com/
HttpGet httpget = new HttpGet("/");
// Execute request
HttpResponse response = httpclient.execute(targetHost, httpget, localcontext);
HttpEntity entity = response.getEntity();
System.out.println(EntityUtils.toString(entity));
} finally {
httpclient.getConnectionManager().shutdown();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
我已经编辑我的答案。 ;) – CSchulz
你看过我的评论吗?您正在尝试使用* HttpClient *版本4.x并需要这些库和**更改库的顺序**! – CSchulz
* HttpClient *库必须位于android库之前。我不知道你正在使用哪个IDE。在eclipse中,你可以在*项目属性* - > * java构建路径* - > *命令并导出* – CSchulz