繁体   English   中英

从Android发送HTTP发布请求时崩溃

[英]Crash on Sending HTTP post request from android

我试图使用在SO中找到的代码段从Android发送HTTP请求,但遇到两个问题。 第一个是如何在我的http请求中放置标头。 第二个是当我尝试执行它(httpclient.execute)时,出现以下错误。 我已经使用高级Rest Client尝试了url,并获得了有效的响应。

这是一个成功的请求

POST /api/v1/login/ HTTP/1.1
HOST: api.example.com
user-agent: Example
content-type: application/json
content-length: 47

{"username":"test", "password":"123456"}

这是我的代码。

String restUrl="https://api.example.com/api/v1/login/";

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(restUrl);

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("username", mJidView.getText().toString()));
            nameValuePairs.add(new BasicNameValuePair("password",mPasswordView.getText().toString() ));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();

我在httpclient.execute(httppost)中遇到错误这是附加的logcat。

at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
                      at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:86)
                      at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:74)
                      at java.net.InetAddress.getAllByName(InetAddress.java:752)
                      at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:142)
                      at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169)
                      at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124)
                      at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:366)
                      at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560)
                      at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492)
                      at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470)

不建议使用http发送请求,您可以将其更改为volley。

看到这个链接:

齐射Google开发人员

如果您需要帮助,可以尝试在此处或在网络中搜索。

可能您启用了StrictMode并在主线程中发出了HTTP请求,而这在Android当前是不允许的。

请在工作线程中提出您的请求,或更改StrictMode策略:

错误StrictMode $ AndroidBlockGuardPolicy.onNetwork

但通常-您应该在工作线程中发出请求。

您是否尝试过为Post请求设置标题? 像这样

 httpPost.addHeader("content-type", "application/x-www-form-urlencoded;charset=UTF-8");
 httpPost.setHeader("Accept", "application/json");
 httpPost.setHeader("Accept-Charset", "utf-8");
 httpPost.setEntity(new UrlEncodedFormEntity(NameValuePairs, "UTF-8"));

编辑:

字符串restUrl =“ https://api.example.com/api/v1/login/”;

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(restUrl);

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("username", mJidView.getText().toString()));
        nameValuePairs.add(new     BasicNameValuePair("password",mPasswordView.getText().toString() ));
        httpPost.addHeader("content-type", "application/x-www-form-urlencoded; charset=UTF-8");
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Accept-Charset", "utf-8");

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
        }catch (UnsupportedEncodingException e) {
            e.printStackTrace();

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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