从android发送HTTP post请求时崩溃

时间:2016-12-18 21:12:42

标签: android http request

我试图使用我在SO中找到的代码片段从Android发送HTTP请求,但我遇到了两个问题。第一个是如何将标题放入我的http请求。第二个是当我尝试执行它时(httpclient.execute)我收到以下错误。我已经使用高级rest客户端尝试了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)

3 个答案:

答案 0 :(得分:1)

http不会被推荐用于发送请求,您可以将其更改为截击。

请看这个链接:

volley Google developers

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

答案 1 :(得分:1)

可能你启用了StrictMode并且你在主线程中发出了你的http请求,这在Android中是不允许的。

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

Error StrictMode$AndroidBlockGuardPolicy.onNetwork

但一般情况下 - 你应该在工作线程中提出请求。

答案 2 :(得分:-1)

您是否尝试过为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"));

编辑:

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