簡體   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