简体   繁体   中英

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 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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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