url连接的响应代码是301,而不是android中的200

url连接的响应代码是301,而不是android中的200,android,httpurlconnection,Android,Httpurlconnection,使用下面给出的代码,我必须点击多个URL来获取一些数据。此代码适用于除一个URL之外的所有URL。我得到的这个特定URL的响应代码是301,如下面的Logcat部分所述。我不能在这里共享URL,因为它是私有的。请帮我找出这有什么问题。先谢谢你 package com.example.abc; import android.content.Intent; import java.io.BufferedInputStream; impo

使用下面给出的代码,我必须点击多个URL来获取一些数据。此代码适用于除一个URL之外的所有URL。我得到的这个特定URL的响应代码是301,如下面的Logcat部分所述。我不能在这里共享URL,因为它是私有的。请帮我找出这有什么问题。先谢谢你

        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

看起来您被重定向到HTTPS,而您的代码无法处理它。捕获特定异常并使用SSL连接,然后选择如何使用SSL连接任何示例。我不熟悉Android您的代码似乎忽略了重定向,请尝试删除它,看看它是否有帮助我尝试了,但什么都没有发生,只需显示http_STATUS=301和result1:301 Moved permanually Moved permanually文档已移动。只需确保url在应该使用https时使用https即可。我改为http的https instrad,现在工作正常,没有301错误。