2017-07-29 3 views
0

Ici, j'ai écrit le code pour mettre en œuvre la demande HttpPost dans android qui devrait fonctionner après la méthode onClick. Eh bien, je n'ai pas eu la solution, c'est le troisième programme que j'ai essayé et maintenant mettre à jour la question.javax.net.ssl.SSLException avec la demande HttpPost dans android

Mais je ne sais pas chaque fois que je reçois le même error. Veuillez jeter un coup d'oeil de cette situation. Je crois, les codes sont presque corrects avec une petite correction nécessaire, besoin de penser différemment de ce que je fais ici. Parce que cela fonctionne très bien en tant que simple programme Java dans eclipse mais android studio lance exception.

C'est la méthode 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(); 
} 

C'est 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(); 

    } 
} 

Ici, je ne reçois pas exception indiquant dans stackTrace de Android Monitor, mais après le débogage je l'ai trouvé est de jeter l'exception

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

Et le code d'erreur diffère parfois pour le même programme. Je ne suis pas capable de trouver quelle erreur je fais ici.

S'il vous plaît, aidez-moi.

Répondre

0

Essayez de mettre à jour votre niveau d'application build.gradle, en ajoutant cette ligne

useLibrary 'org.apache.http.legacy' 

dans le premier bloc/partie désignée comme androïde Exemple:

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

puis exécutez votre programme

+0

Ça fait beaucoup. En ajoutant cette ligne, le programme s'exécute. mais maintenant à cette ligne 'HttpResponse response = (HttpResponse) httpclient.execute (httpPostRequest);' la 'response' est _null_. et le _error_ est la connexion 'javax.net.ssl.sslexception réinitialisée par peer android' – Shambhu