HttpPost giving error in API 23 android

2019-09-20 15:41发布

I have a method in my android app which uses HttpPost class. It was working fine with targeted sdk 4.4.2 but i've made some changes and made the targeted sdk to 23(6.0). Now HttpPost class is giving error. I've also read about HttpUrlConnection but don't know how to use it. Here is my code

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;
    }

Please help what can i do or if possible provide this function with classes that will work on 23. Thanks in advance

1条回答
Evening l夕情丶
2楼-- · 2019-09-20 16:10

Replace your code inside function with below given code, using 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();
        }
    }

}
}
查看更多
登录 后发表回答