2016-12-23 5 views
1

Mit dem unten angegebenen Code muss ich mehrere URLs treffen, um einige Daten zu holen. Dieser Code funktioniert für alle URLs außer einem. Der ResponseCode, den ich für diese bestimmte URL erhalte, ist 301, wie unten im Logcat-Abschnitt erwähnt. Ich kann die URL hier nicht teilen, da sie privat ist. Bitte helfen Sie mir herauszufinden, was damit nicht stimmt. Vielen Dank im Voraus.ResponseCode ist 301 für URL-Verbindung statt der 200 in Android

 package com.example.abc; 
     import android.content.Intent; 
     import java.io.BufferedInputStream; 
     import java.io.BufferedReader; 
     import java.io.FileOutputStream; 
     import java.io.InputStream; 
     import java.io.InputStreamReader; 
     import java.io.OutputStream; 
     import java.net.HttpURLConnection; 
     import java.net.URL; 
     import java.net.URLConnection; 

     import javax.net.ssl.HostnameVerifier; 

     import org.json.JSONArray; 
     import org.json.JSONException; 
     import android.app.Activity; 
     import android.content.Intent; 
     import android.util.Base64; 
     import android.util.Log; 
     import android.widget.Toast; 

     // Used to parse json data into json array 
     public class JSONfunctions { 

      public static JSONArray getJSONfromURL(String url,Activity activity){ 
       String result = ""; 
       JSONArray jArray = null; 
       InputStream is = null; 
       int http_status; 
       //check the network connection 
       if (Operation.isNetworkAvailable(activity)) 
       { 
        try    
        { 
         Log.d("MyTag", "url : "+url); 

         Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(activity)); // use to avoid force close, check Exceptionhandler.java 

         // Making HTTP request 
         try{     

          URL new_url= new URL (url); // convert string to url 


          HttpURLConnection urlConnection = (HttpURLConnection) new_url.openConnection(); // open url connection using HttpURLConnection 


          urlConnection.setInstanceFollowRedirects(false); 


          http_status = urlConnection.getResponseCode(); // get response code from url connection 

          if(http_status!=200) 
          { 

           Operation.showToast(activity,R.string.no_network);  

           Intent i=new Intent(activity,Settings.class);      
           activity.startActivity(i); 

          } 


          Log.d("MyTag", "http_status : "+http_status); 

          urlConnection.setDefaultUseCaches(false); // clear cache 

          is = new BufferedInputStream(urlConnection.getInputStream()); // get input stream from url connection  

        }catch(Exception e) 
        { 
         Log.d("Exception",e.toString()); 
        }   
        try{ 

          BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"),8); // read input stream 



          //creating a StringBuilder object it is use for string concatenation, calling the append() method and finally toString() 
          StringBuilder sb = new StringBuilder(); 


          String line = null; 
          while ((line = reader.readLine()) != null) 
          { 
           sb.append(line + "\n"); 

          } 
          is.close(); 
          result=sb.toString(); 

        }catch(Exception e) 
        { 
         Log.d("Buffered reader Exception :",e.toString()); 
        } 
        Log.d("MyTag", "result1 : "+result); 


         //check API result and perform action according to result 
         if(result.startsWith("Invalid")) // when API return invalid login details 
         {  
          Operation.showToast(activity,R.string.invalid_login); 
          Intent i=new Intent(activity,Settings.class);    
          activity.startActivity(i);  
         } 
         else if(result.startsWith("Domain")) // when API return domain not found 
         { 
          Operation.showToast(activity,R.string.domain_not_found); 
          Intent i=new Intent(activity,Settings.class);    
          activity.startActivity(i); 
         } 
         else if(result.startsWith("{")) // when API return json object then make it in json array format with adding "[ result ]". 
         { 
          result="["+result+"]"; 
          jArray = new JSONArray(result);     
         } 
         else if(result.startsWith("[")) // when API return json array then keep it as it is. 
         { 
          jArray = new JSONArray(result); 
         } 
         else if(result.startsWith("success")) // when API return success string then then make it in json array format with adding "[ result ]". 
         { 
          result="["+result+"]"; 
          jArray = new JSONArray(result); 

         } 
         else 
         { 
          result=result.replaceAll(" ", ""); // remove space 
          result="["+result+"]"; 
          jArray = new JSONArray(result); 
         }   

        } catch (JSONException e) 
        { 
         Operation.showToast(activity,e.getMessage());   
        } 
       } 
       else 
       { 
        Operation.showToast(activity,R.string.no_network);  // show network error 
        Intent i=new Intent(activity,Settings.class);      
        activity.startActivity(i); 
       } 

       // finally return the json array 

       return jArray; 

      } 

     } 

Logcat

12-23 04:55:55.191: D/Response Code :(3071): Response Code : 301 
12-23 04:55:55.201: D/Exception(3071): java.lang.ClassCastException: com.android.okhttp.internal.http.HttpURLConnectionImpl cannot be cast to javax.net.ssl.HttpsURLConnection 

Antwort

1

, die wie Sie HTTPS und Ihren Code umgeleitet, wenn es darum sieht ist der Umgang mit ihm nicht. Catch die spezifische Ausnahme und verbinden mit SSL

+0

Dann wie mit SSl irgendein Beispiel zu verbinden.Ich bin neu zu Android –

+0

Ihr Code scheint Umleitungen zu ignorieren, versuchen, das zu entfernen und sehen, ob es hilft – Cez

+0

Ich habe versucht, aber nichts passieren, nur zeigen theHTTP_STATUS = 301 und result1: 301 dauerhaft verschoben

dauerhaft verschoben

Das Dokument wird verschoben here.

Verwandte Themen