在API的android 23给HttpPost错误 [英] HttpPost giving error in API 23 android

查看:522
本文介绍了在API的android 23给HttpPost错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Andr​​oid应用程序的方法,它使用HttpPost类。
这是工作的罚款有针对性的SDK 4.4.2,但我已经做了一些修改,并提出针对性的SDK至23(6.0)。
现在HttpPost类是给错误。
我也看到了有关HttpURLConnection类,但不知道如何使用它。
这里是我的code

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

请帮我能做些什么,或者如果可能提供类此功能,将在23工作。
在此先感谢

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

推荐答案

有如下code替换里面的功能您code,使用 HttpURLConnection类

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

}
}

这篇关于在API的android 23给HttpPost错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆