开发者

Android HTTP post problem

I am developing a app that makes a connection to my drupal website. The following the code I am attemping to use:

public class SignIn ext开发者_开发百科ends 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. I have INTERNET permissions set in the manifest file.

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. Stop (kill) adb.exe and emulator and it usually works for me.


401 means you need to properly authenticate: does the site use forms-based login or HTTP Basic Authentication? 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜