- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
使用下面给出的代码,我必须点击多个 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/
我正在使用 SoapException 来处理无效输入和我的网络服务中的所有其他类型的异常处理。它工作正常,我能够格式化并将正确的信息返回给客户。我正在遵循一种类似于此处解释的方法 http://ww
您好,我正在使用以下代码建立 URL 连接。但我随机得到 responseCode -1(这是 responseCode 的默认值): try { URL url = new UR
我使用的是 Indy 10.5.7 最近向我展示了如何使用 Indy 使用有效凭据登录网站。现在我可以执行搜索并返回我需要的结果。 但是,我仍然有一个问题。我必须理所当然地认为我已根据页面重定向成功登
我正在编写一个测试,其中不同的用户具有向团队添加和删除其他用户的不同权限。该测试检查用户是否可以向团队添加新用户或从团队中删除现有用户。 但是,无法添加新用户的用户不会返回新添加用户的 id,因此他们
我的 GoogleSignin 代码无法正常工作,但没有发生以下错误: 代码正在编译中。 此外,当我点击登录时,我会收到一个弹出窗口,询问我要使用哪个 Google 帐户登录。 onActivityR
我想覆盖 j-meter 中的状态为 200-OK,耗时为 0ms。 实际上,我已经将java与jmeter一起使用,并放置了SampleResult.sampleStart()和sampleEnd(
使用下面给出的代码,我必须点击多个 URL 才能获取一些数据。此代码适用于除一个以外的所有 URL。我为这个特定 URL 获得的 ResponseCode 是 301,如下面的 Logcat 部分所述
我正在使用谷歌应用内结算库 1.0 在我的应用程序中为无广告版本编写应用内结算功能。我经历了所有阶段 here codelab到目前为止。然而,我总是遇到这个错误,返回的 responseCode 是
我正在尝试让 Facebook 用户参加一个 Activity , Request.executePostRequestAsync(...) 但是服务器总是返回以下内容: {Response: re
我现在正在使用新的 Play 结算库 基本上,我是按照文档查询可购买的元素 SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder
我实现了 spark 应用程序。我创建了 spark 上下文: private JavaSparkContext createJavaSparkContext() {
我正在构建 Cordova 应用程序并使用 Crittercism 插件进行错误记录。当我从 Xcode 运行它时,我不断收到错误流: [Crittercism] 警告:responseCode ==
我是一名优秀的程序员,十分优秀!