Android HTTP帖子问题

时间:2011-05-11 22:45:13

标签: android drupal http-post

我正在开发一个与我的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权限。

我应该提到这个问题不是在模拟器上发生,而是在真实设备上发生。 任何人都可以请帮助,并告诉我为什么这是故障?

2 个答案:

答案 0 :(得分:1)

在Android模拟器上进行开发时,这是一个常见问题。停止(杀死)adb.exe和模拟器,它通常适用于我。

答案 1 :(得分:1)

401表示您需要正确进行身份验证:网站是否使用基于表单的登录或HTTP基本身份验证?如果它使用基本身份验证,则需要设置适当的授权标头。请参阅http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientAuthentication.java上的示例,了解如何执行Basic Auth。

相关问题