【问题标题】:javax.net.ssl.SSLException with HttpPost request in androidjavax.net.ssl.SSLException 与 Android 中的 HttpPost 请求
【发布时间】:2018-01-05 04:30:33
【问题描述】:

在这里,我编写了代码来在 android 中实现 HttpPost 请求,它应该在 onClick 方法之后运行。

好吧,我没有得到解决方案,这是我尝试的第三个程序,现在更新问题。

但我不知道每次我得到相同的error。请看看这种情况。

我相信,代码几乎是正确的,需要进行任何小的修正,需要与我在这里做的不同的想法。

因为它在eclipse 中作为简单的java 程序运行良好,但android studio 抛出exception

这是onClick 方法:

public void onClick(View v) {
    inputSub = subIn.getText().toString();
    String url = "https://some_url";
    String inputJson = null;

    try {
        inputJson = "{  \"asset\": {\"id\": ..........}";
    }catch (JSONException e){
        e.printStackTrace();
    }
    new CreateIncident(url, inputJson).execute();
}

这是AsyncTask class

private class CreateIncident extends AsyncTask<Void, Void, Void> {
    private final String TAG = "HttpClient";

    final String user ="some_user";
    final String password ="some_pwd";

    String serUrl;
    String inputJson ="";

    public CreateIncident(String serUrl, String inputJson) {
        this.serUrl = serUrl;
        this.inputJson = inputJson;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        // Showing progress dialog
        pDialog = new ProgressDialog(CreateIncidentActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) { 
      try {
        String authString = user + ":" + password;

        byte[] authBytes = authString.getBytes();
        String authStringEnc = Base64.encodeToString(authBytes, Base64.DEFAULT);

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(serUrl);

        postRequest.setHeader("Authorization", "Basic " + authStringEnc);
        postRequest.setHeader("Accept", "application/json");
        postRequest.setHeader("Content-type", "application/json");

        StringEntity input = new StringEntity(inputJson);

        input.setContentType("application/json");
        postRequest.setEntity(input);

        HttpResponse response = httpClient.execute(postRequest);

        StringBuilder sb = new StringBuilder();
        try {
            BufferedReader reader =
                    new BufferedReader(new InputStreamReader(response.getEntity().getContent()), 65728);
            String line = null;

            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        }
        catch (IOException e) { e.printStackTrace(); }
        catch (Exception e) { e.printStackTrace(); }


        System.out.println("finalResult " + sb.toString());
        System.out.println(response.getStatusLine().getReasonPhrase());

        if (response.getStatusLine().getStatusCode() != 201) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + response.getStatusLine().getStatusCode());
        }

        BufferedReader br = new BufferedReader(
                new InputStreamReader((response.getEntity().getContent())));


        httpClient.getConnectionManager().shutdown();

    } catch (MalformedURLException e) {

        e.printStackTrace();

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

    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();

    }
}

这里我没有在Android MonitorstackTrace 中显示任何异常,但调试后我发现它抛出异常

javax.net.ssl.SSLException: Read error: ssl=0xd6483780: I/O error during system call, Connection reset by peer

同一程序的错误代码有时会有所不同。

我在这里找不到我做错了什么。

请有人帮助我。

【问题讨论】:

    标签: java android android-asynctask http-post sslexception


    【解决方案1】:

    尝试更新您的应用级别 build.gradle, 通过添加这一行

    useLibrary 'org.apache.http.legacy'
    

    进入名为 android 的第一个块/部分 例如:

    apply plugin: 'com.android.application'
    
        android {
            useLibrary 'org.apache.http.legacy'
            compileSdkVersion 26
            buildToolsVersion "26.0.0"
            defaultConfig {
                applicationId "com.example.mnaum.getgpscoordinates"
                minSdkVersion 15
                targetSdkVersion 26
                versionCode 1
                versionName "1.0"
                testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
            }
    

    然后运行你的程序

    【讨论】:

    • 非常感谢。通过添加此行,程序将执行。但现在在HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest); 这一行,responsenullerrorjavax.net.ssl.sslexception connection reset by peer android
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    相关资源
    最近更新 更多