gpt4 book ai didi

android - ResponseCode 是 301 用于 url 连接而不是 200 在 android

转载 作者:行者123 更新时间:2023-11-30 00:49:28 25 4
gpt4 key购买 nike

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

        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;

}

}

逻辑猫

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 连接

关于android - ResponseCode 是 301 用于 url 连接而不是 200 在 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41299302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com