[英]Android HTTP post problem
我正在開發一個與我的drupal網站建立連接的應用程序。 以下是我正在嘗試使用的代碼:
public class SignIn extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
HttpClient client = new DefaultHttpClient();
String postURL = "http://test2.icerge.com/testpoint/user";
HttpPost post = new HttpPost(postURL);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("account[name]", "Bob Kelso"));
params.add(new BasicNameValuePair("account[pass]", "awefulawefulman"));
params.add(new BasicNameValuePair("account[mail]", "bobkelso@sacredheart.org"));
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
post.setEntity(ent);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
if (resEntity != null) {
Log.i("RESPONSE",EntityUtils.toString(resEntity));
}
} catch (Exception e) {
e.printStackTrace();
Log.i("ERROR",e.toString());
}
}
}
但是,我一直得到一個java.net.unknownhostexception
。 我在清單文件中設置了INTERNET權限。
我應該提到這個問題不是在模擬器上發生,而是在真實設備上發生。 任何人都可以請幫助,並告訴我為什么這是故障?
在Android模擬器上進行開發時,這是一個常見問題。 停止(殺死)adb.exe和模擬器,它通常適用於我。
401表示您需要正確進行身份驗證:該站點是使用基於表單的登錄還是HTTP基本身份驗證? 如果它使用基本身份驗證,則需要設置適當的授權標頭。 有關如何執行Basic Auth的信息,請參閱http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientAuthentication.java中的示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.