ResponseCode是301用于url连接,而不是android中的200

时间:2016-12-23 10:07:31

标签: android httpurlconnection

使用下面给出的代码,我必须点击多个URL来获取一些数据。此代码适用于除一个以外的所有URL。我在这个特定的URL中得到的ResponseCode是301,如下面Logcat部分所述。我不能在这里分享网址,因为它是私密的。请帮我弄清楚这有什么问题。先感谢您。

        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

1 个答案:

答案 0 :(得分:1)

看起来你正在被重定向到HTTPS而你的代码没有处理它。捕获特定异常并使用SSL连接