简体   繁体   English

Android HTTP帖子问题

[英]Android HTTP post problem

I am developing a app that makes a connection to my drupal website. 我正在开发一个与我的drupal网站建立连接的应用程序。 The following the code I am attemping to use: 以下是我正在尝试使用的代码:

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

But, I keep getting a java.net.unknownhostexception . 但是,我一直得到一个java.net.unknownhostexception I have INTERNET permissions set in the manifest file. 我在清单文件中设置了INTERNET权限。

I should mention this problem is not happening on an emulator but on a real device. 我应该提到这个问题不是在模拟器上发生,而是在真实设备上发生。 Can anyone please help and give me some indication why this is malfunctioning? 任何人都可以请帮助,并告诉我为什么这是故障?

This is a common problem when developing on Android emulators. 在Android模拟器上进行开发时,这是一个常见问题。 Stop (kill) adb.exe and emulator and it usually works for me. 停止(杀死)adb.exe和模拟器,它通常适用于我。

401 means you need to properly authenticate: does the site use forms-based login or HTTP Basic Authentication? 401表示您需要正确进行身份验证:该站点是使用基于表单的登录还是HTTP基本身份验证? If it uses Basic authentication you'll need to set an appropriate Authorization header. 如果它使用基本身份验证,则需要设置适当的授权标头。 See the example at http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientAuthentication.java for how to perform Basic Auth. 有关如何执行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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM