簡體   English   中英

HttpPost 在 API 23 android 中給出錯誤

[英]HttpPost giving error in API 23 android

我的 android 應用程序中有一個使用 HttpPost 類的方法。 它在目標 sdk 4.4.2 上運行良好,但我做了一些更改並將目標 sdk 設為 23(6.0)。 現在 HttpPost 類給出錯誤。 我也讀過 HttpUrlConnection 但不知道如何使用它。 這是我的代碼

private String getJSON(String URL, JSONObject obj) throws JSONException {
        String responseString = null;
        try {

            HttpPost httppost = new HttpPost(URL);
            StringEntity se = new StringEntity(obj.toString(), HTTP.UTF_8);
            httppost.setHeader("Content-Type", "application/json");

            httppost.setEntity(se);

            HttpParams httpParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
            HttpClient httpclient = new DefaultHttpClient(httpParams);
            HttpResponse httpResponse = httpclient.execute(httppost);

            StatusLine statusLine = httpResponse.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            System.out.println("status code is" + statusCode);
            HttpEntity entity = httpResponse.getEntity();
            responseString = EntityUtils.toString(entity, "UTF-8");
            System.out.println("response string" + responseString);
            Log.i("RESPONSE XML ------> ", "-----> " + responseString);
            return responseString;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return responseString;
    }

請幫助我能做什么,或者如果可能的話,為這個功能提供適用於 23 的類。提前致謝

使用HttpUrlConnection用下面給定的代碼替換函數內部的代碼

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONException;
import org.json.JSONObject;

public class TestJava {

private String getJSON(String URL, JSONObject obj) throws JSONException {
    URL url;
    HttpURLConnection connection = null;
    try {
        //Create connection
        url = new URL(URL);
        connection = (HttpURLConnection)url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type",
                "application/json");
        connection.setUseCaches (false);
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setConnectTimeout(30000);
        //Send request
        DataOutputStream wr = new DataOutputStream (
                connection.getOutputStream ());
        wr.write(obj.toString().getBytes("UTF-8"));
        wr.flush ();
        wr.close ();

        //Get Response
        InputStream is = connection.getInputStream();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        StringBuffer response = new StringBuffer();
        while((line = rd.readLine()) != null) {
            response.append(line);
            response.append('\r');
        }
        rd.close();
        return response.toString();

    } catch (Exception e) {

        e.printStackTrace();
        return null;

    } finally {

        if(connection != null) {
            connection.disconnect();
        }
    }

}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM