Android:java.net.ProtocolException:意外的状态行:HTTP / 1.2 200 OK

时间:2017-03-08 14:09:30

标签: java android

我一直在研究以下代码。 该代码适用于我的应用程序的5.x版本,但我无法使代码适用于Android 6.x及更高版本。

public class PostAsync extends AsyncTask<String, Integer, Double> {
private Context _context = null;

public PostAsync(Context context) {
    _context = context;
}

@Override
protected Double doInBackground(String... params) {

    String serverResponse = postData(params[0]);

    try {
        JSONObject obj = new JSONObject(serverResponse);
        String id = "";
        JSONObject locationobj = obj.getJSONObject("X");
        JSONObject response = locationobj.getJSONObject("Y");
        id = response.getString("id");
        Settings.idcode = id;



        // Convert , to %2c, since we're working with a URI here
        String number = Settings.number + Settings.code + "," + Settings.idcode; // %2c
        _context.startActivity(new Intent(Intent.ACTION_CALL).setData(Uri.parse("tel://" + number)));
    }
    catch (Exception e) {

        // TODO: Errorhandler
        e.printStackTrace();
    }

    return null;
}

protected void onPostExecute(Double result) {
}

protected void onProgressUpdate(Integer... progress) {
}

// Send a POST request to specified url in Settings class, with defined JSONObject message
public String postData(String msg) {
    String result = null;
    StringBuffer sb = new StringBuffer();
    InputStream is = null;

    try {
        URL url = new URL(Settings.webURL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setChunkedStreamingMode(0);
        connection.setReadTimeout(15000);
        connection.setConnectTimeout(15000);
        connection.setRequestProperty("Content-Encoding", "identity");
        connection.setRequestProperty("Accept-Encoding", "identity");
        connection.setRequestProperty("User-Agent", "Mozilla/5.0");
        connection.setRequestProperty("TYPE", "JSON");
        connection.setRequestProperty("KEY", "key");

        DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
        wr.writeBytes(msg);
        wr.flush();
        wr.close();

        int responseCode = connection.getResponseCode();
        String responseMessage = connection.getResponseMessage();
        System.out.println("Response code: " + responseCode);
        System.out.println("Response message: " + responseMessage);

        if(responseCode == HttpURLConnection.HTTP_OK){

            is = new BufferedInputStream(connection.getInputStream());
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String inputLine = "";
            try {
                while ((inputLine = br.readLine()) != null) {
                    sb.append(inputLine);
                }

                result = sb.toString();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    }
                    catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    }

    return result;
}

}

我收到以下错误 java.net.ProtocolException:意外的状态行:HTTP / 1.2 200 OK

有人可以告诉我我错过了什么吗?

0 个答案:

没有答案