- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试通过 POST 将 json obj 通过 volley 在我的 android 应用程序中发送到 api 并捕获 json 响应,但我不断收到此错误: E/Volley:[194] BasicNetwork.performRequest:http://phoneapi.adro.co/Services/AdService.svc/GetAd 的意外响应代码 307| .
有一段代码发出请求:
public class JsonObjectSend
{
private static final String TAG_action = "Action";
private static final String TAG_acValue = "ActionValue";
private static final String TAG_bg = "BgColor";
private static final String TAG_id = "CampId";
private static final String TAG_ctype = "CampType";
private static final String TAG_adr = "ClickAddress";
private static final String TAG_endAnim = "EndAnimation";
private static final String TAG_expText = "ExplainText";
private static final String TAG_imType = "ImageType";
private static final String TAG_imgUBanner = "ImageUrlBanner";
private static final String TAG_imgULBanner = "ImageUrlLargBanner";
private static final String TAG_imgUMRect = "ImageUrlMediumRectange";
private static final String TAG_imgUFBanner = "ImageUrlFullBanner";
private static final String TAG_imgULeadBrd = "ImageUrlLeaderBoard";
private static final String TAG_logoU = "LogoUrl";
private static final String TAG_mainText = "MainText";
private static final String TAG_packName = "PackageName";
private static final String TAG_repText = "ReplaceText";
private static final String TAG_startAnim = "StartAnimation";
private static final String TAG_videoU = "VideoUrl";
private static final String TAG_type = "type";
private static final String TAG_atag = "atag";
private static final String TAG_msg = "message";
private static final String TAG_pix = "pixel";
private static final String TAG_scrpt = "script";
private static final String TAG_tkn = "token";
public void sendobj()
{
JSONObject jsonBody = new JSONObject();
try {
jsonBody.put("DeviceId", "sdasda");
jsonBody.put("AppId", "ECC7BCE40126408386BCFCF8AB9187E7");
jsonBody.put("AdType", "text");
jsonBody.put("Location", "35.7209331,51.472983");
jsonBody.put("UserOperator", "Irancell");
jsonBody.put("Ip", "127.0.0.1");
jsonBody.put("NetworkType", "3G");
jsonBody.put("DeviceType", "Android");
jsonBody.put("DeviceBrand", "Samsung");
jsonBody.put("Width", "0");
jsonBody.put("Height", "0");
jsonBody.put("TestMode", "true");
} catch (JSONException e) {
e.printStackTrace();
}
String tag_string_req = "Send_Object";
JsonObjectRequest Req = new JsonObjectRequest(Request.Method.POST, AppConfig.API_URL, jsonBody, new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response)
{
try
{
JSONObject jObj = response;
boolean error = jObj.getBoolean("error");
if (!error)
{
String action = jObj.getString(TAG_action);
String actionvalue = jObj.getString(TAG_acValue);
String bg = jObj.getString(TAG_bg);
String campid = jObj.getString(TAG_id);
String camptype = jObj.getString(TAG_ctype);
String clickadr = jObj.getString(TAG_adr);
String endanim = jObj.getString(TAG_endAnim);
String exptext = jObj.getString(TAG_expText);
String imgtype = jObj.getString(TAG_imType);
String imgurlbanner = jObj.getString(TAG_imgUBanner);
String imgurllargebanner = jObj.getString(TAG_imgULBanner);
String imgurlmedrectangle = jObj.getString(TAG_imgUMRect);
String imgurlfullbanner = jObj.getString(TAG_imgUFBanner);
String imgurlleaderboard = jObj.getString(TAG_imgULeadBrd);
String logourl = jObj.getString(TAG_logoU);
String maintext = jObj.getString(TAG_mainText);
String packagename = jObj.getString(TAG_packName);
String replacetext = jObj.getString(TAG_repText);
String startenim = jObj.getString(TAG_startAnim);
String videourl = jObj.getString(TAG_videoU);
String type = jObj.getString(TAG_type);
String atag = jObj.getString(TAG_atag);
String msg = jObj.getString(TAG_msg);
String pixel = jObj.getString(TAG_pix);
String script = jObj.getString(TAG_scrpt);
String token = jObj.getString(TAG_tkn);
Log.d("action", action);
} else
{
String errorMsg = jObj.getString("error_msg");
Log.d("Debug", errorMsg);
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
}, new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
Log.d("error","Error");
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError
{
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json; charset=utf-8");
return params;
}//
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(Req, tag_string_req);
}
}//class end
这是我的应用程序 Controller 类:
public class AppController extends Application
{
public static final String TAG = AppController.class.getSimpleName();
private RequestQueue mRequestQueue;
private static AppController mInstance;
@Override
public void onCreate()
{
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance()
{
return mInstance;
}
public RequestQueue getRequestQueue()
{
if (mRequestQueue == null)
{
// mRequestQueue = Volley.newRequestQueue(getApplicationContext());
Network network = new BasicNetwork(new OkHttpStack());
mRequestQueue = new RequestQueue(new DiskBasedCache(new File(getCacheDir(), "volley")), network);
mRequestQueue.start();
}
return mRequestQueue;
}
public class OkHttpStack extends HurlStack
{
private final OkUrlFactory mFactory;
public OkHttpStack()
{
this(new OkHttpClient());
}
private OkHttpClient getUnsafeOkHttpClient()
{
try {
final TrustManager[] trustAllCerts = new TrustManager[]
{
new X509TrustManager()
{
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) {}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) {}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers()
{
return null;
}
}
};
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setSslSocketFactory(sslSocketFactory);
okHttpClient.setHostnameVerifier(new HostnameVerifier()
{
@Override
public boolean verify(String hostname, SSLSession session)
{
return true;
}
});
return okHttpClient;
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
public OkHttpStack(OkHttpClient client)
{
client=getUnsafeOkHttpClient();
if (client == null)
{
throw new NullPointerException("Client must not be null.");
}
mFactory = new OkUrlFactory(client);
}
@Override
protected HttpURLConnection createConnection(URL url) throws IOException
{
return mFactory.open(url);
}
}
public <T> void addToRequestQueue(Request<T> req, String tag)
{
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req)
{
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag)
{
if (mRequestQueue != null)
{
mRequestQueue.cancelAll(tag);
}
}
}
这是我的完整日志:
06-17 23:20:54.637 14338-14338/? I/art﹕ Late-enabling -Xcheck:jni
06-17 23:20:54.751 14338-14338/ir.adro.adrosdk I/WebViewFactory﹕ Loading com.android.webview version 37 (eng.buildbot-x86) (code 199997)
06-17 23:20:54.757 14338-14338/ir.adro.adrosdk I/LibraryLoader﹕ Loading: webviewchromium
06-17 23:20:54.758 14338-14338/ir.adro.adrosdk I/LibraryLoader﹕ Time to load native libraries: 1 ms (timestamps 9529-9530)
06-17 23:20:54.758 14338-14338/ir.adro.adrosdk I/LibraryLoader﹕ Expected native library version number "",actual native library version number ""
06-17 23:20:54.766 14338-14338/ir.adro.adrosdk V/WebViewChromiumFactoryProvider﹕ Binding Chromium to main looper Looper (main, tid 1) {39d8cd06}
06-17 23:20:54.767 14338-14338/ir.adro.adrosdk I/LibraryLoader﹕ Expected native library version number "",actual native library version number ""
06-17 23:20:54.767 14338-14338/ir.adro.adrosdk I/chromium﹕ [INFO:library_loader_hooks.cc(106)] Chromium logging enabled: level = 0, default verbosity = 0
06-17 23:20:54.771 14338-14338/ir.adro.adrosdk I/BrowserStartupController﹕ Initializing chromium process, renderers=0
06-17 23:20:54.771 14338-14338/ir.adro.adrosdk W/art﹕ Attempt to remove local handle scope entry from IRT, ignoring
06-17 23:20:54.775 14338-14338/ir.adro.adrosdk W/chromium﹕ [WARNING:resource_bundle.cc(315)] locale_file_path.empty()
06-17 23:20:54.776 14338-14338/ir.adro.adrosdk I/chromium﹕ [INFO:aw_browser_main_parts.cc(63)] Load from apk succesful, fd=32 off=46092 len=2953
06-17 23:20:54.776 14338-14338/ir.adro.adrosdk I/chromium﹕ [INFO:aw_browser_main_parts.cc(78)] Loading webviewchromium.pak from, fd:33 off:228796 len:643667
06-17 23:20:54.780 14338-14358/ir.adro.adrosdk W/AudioManagerAndroid﹕ Requires BLUETOOTH permission
06-17 23:20:54.787 14338-14338/ir.adro.adrosdk D/libEGL﹕ loaded /system/lib/egl/libEGL_emulation.so
06-17 23:20:54.788 14338-14338/ir.adro.adrosdk D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_emulation.so
06-17 23:20:54.797 14338-14338/ir.adro.adrosdk D/libEGL﹕ loaded /system/lib/egl/libGLESv2_emulation.so
06-17 23:20:54.806 14338-14338/ir.adro.adrosdk D/﹕ HostConnection::get() New Host Connection established 0xa363fc10, tid 14338
06-17 23:20:54.859 14338-14338/ir.adro.adrosdk W/chromium﹕ [WARNING:mailbox_synchronizer.cc(36)] MailboxSync not supported due to missing EGL image/fence support
06-17 23:20:54.871 14338-14338/ir.adro.adrosdk W/chromium﹕ [WARNING:data_reduction_proxy_settings.cc(403)] SPDY proxy OFF at startup
06-17 23:20:54.881 14338-14338/ir.adro.adrosdk W/art﹕ Attempt to remove local handle scope entry from IRT, ignoring
06-17 23:20:54.882 14338-14338/ir.adro.adrosdk W/AwContents﹕ onDetachedFromWindow called when already detached. Ignoring
06-17 23:20:54.908 14338-14364/ir.adro.adrosdk W/chromium﹕ [WARNING:proxy_service.cc(901)] PAC support disabled because there is no system implementation
06-17 23:20:54.910 14338-14379/ir.adro.adrosdk D/OpenGLRenderer﹕ Render dirty regions requested: true
06-17 23:20:54.912 14338-14338/ir.adro.adrosdk D/Atlas﹕ Validating map...
06-17 23:20:54.980 14338-14379/ir.adro.adrosdk I/OpenGLRenderer﹕ Initialized EGL, version 1.4
06-17 23:20:54.980 14338-14379/ir.adro.adrosdk D/﹕ HostConnection::get() New Host Connection established 0xa1f9fdc0, tid 14379
06-17 23:20:55.005 14338-14379/ir.adro.adrosdk D/OpenGLRenderer﹕ Enabling debug mode 0
06-17 23:20:55.023 14338-14379/ir.adro.adrosdk W/EGL_emulation﹕ eglSurfaceAttrib not implemented
06-17 23:20:55.023 14338-14379/ir.adro.adrosdk W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa1fba7a0, error=EGL_SUCCESS
06-17 23:20:55.235 14338-14377/ir.adro.adrosdk E/Volley﹕ [199] BasicNetwork.performRequest: Unexpected response code 307 for http://phoneapi.adro.co/Services/AdService.svc/GetAd
06-17 23:20:55.235 14338-14338/ir.adro.adrosdk D/error﹕ Error
谁能告诉我为什么我总是收到这个错误?
最佳答案
您可以通过添加 /
来避免 307 响应代码在您的网址末尾,即 http://phoneapi.adro.co/Services/AdService.svc/GetAd/
然后使用以下示例代码(我使用了你的 jsonBody
),你应该更多地检查你的网络服务,因为这段代码将得到 400 响应代码
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, jsonBody, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i("onResponse", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("onErrorResponse", error.toString());
}
}){
@Override
protected VolleyError parseNetworkError(VolleyError volleyError) {
if(volleyError.networkResponse != null && volleyError.networkResponse.data != null){
VolleyError error = new VolleyError(new String(volleyError.networkResponse.data));
volleyError = error;
}
return volleyError;
}
};
调试时(在VolleyError error = new VolleyError(new String(volleyError.networkResponse.data));
设置断点),会得到一些信息如
The server encountered an error processing the request. The exception message is 'Object reference not set to an instance of an object.'. See server logs for more details. The exception stack trace is: </p>
<p> at AdroMobileApi.Services.AdService.GetAd(AdRequest request)
at SyncInvokeGetAd(Object , Object[] , Object[] )
at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</p>
关于android - E/Volley:[194] BasicNetwork.performRequest : Unexpected response code 307 for url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37892945/
我正在尝试使用 encog 库作为强化学习问题的函数逼近器。更准确地说,我正在尝试启动并运行一个多层感知器 (BasicNetwork)。 由于我的代理会根据我选择的任何 RL 算法以某种方式探索世界
我试图通过 volley 库向我的服务器发送一个发布请求。但它不起作用。我已经在 Postman 中测试了我的 API,它在那里工作。我使用了相同的参数。 我得到的错误: BasicNetwork.p
我在使用 volley 库发出网络请求时遇到此错误,我已点击这些链接 Android Volley - BasicNetwork.performRequest: Unexpected response
当我尝试使用 Volley StringRequest 或 JsonObjectRequest 通过 rest api 获取数据时。我总是收到 400 错误。它与 postman 合作良好。 http
我想从数据库中获取用户的信息并显示出来,但我一直收到这个错误: BasicNetwork.performRequest: Unexpected response code 404 这是我的javaco
为什么当我点击“登录”按钮时出现此错误:“E/Volley: [126] BasicNetwork.performRequest: 意外响应代码 500”? PS:它适用于本地,因此适用于本地数据库。
对于我的移动应用程序,我使用 Android Pay + Stripe 作为支付网关。我使用 Stripe 创建了一个商家帐户,并使用 PUBLISHABLE key 生成了一个 token 。在过去
我正在尝试将数据从 Android 应用程序发送到 Servlet。我的 Android 应用程序运行正常。 问题仅与发送到 Servlet 的请求有关。它给出错误 BasicNetwork.perf
我最近开始开发我的 Android 应用程序,我需要一个登录和注册系统。我在 androidhive.info 上找到了一个教程,但我经常遇到错误。我正在使用 volley 库发送 HTTP 请求,L
我正在做一个用户注册是一个小模块的项目。我在这里使用了一些参数,例如姓名、电子邮件、联系方式、年龄、地址和个人资料图片。 //Here is my Android part Code Where Pr
问题陈述: 我正在尝试访问一个 REST API,该 API 将使用 Volley 为各种 HTTP 状态代码(400、403、200 等)返回一个 JSON 对象。 对于 200 以外的任何 HTT
我的第一个帖子:Anything I try to store on database enters with value '0' 我解决了这个问题,但现在我遇到了另一个问题:为什么我尝试登录时出现此
使用 Volley,我 POST StringRequest 并且当我访问 url 时出现错误,因为它是 https://www.google.com/?gfe_rd=cr&ei=vX8jVdTvOs
我在我的 android samsung 上尝试下面的代码,它给出了错误。 (我要获取 channel 属性) String URL = "https://www.googleapis.com/you
我正在尝试通过 POST 将 json obj 通过 volley 在我的 android 应用程序中发送到 api 并捕获 json 响应,但我不断收到此错误: E/Volley:[194] Bas
API 是在 Wordpress 中使用 POST 方法开发的。 API 在 POSTMAN 和 iOS(swift) 中运行良好。我在 POSTMAN 中收到回复,我的同事 iOS 开发人员也收到回
我正在使用 Volley 连接到 MySql 数据库。我创建了数据库表并编写了 php 函数,下面是 java 代码。所有这一切一直有效,但就在昨天,它醒来时在 logcat 中给出了这个错误并且它没
我遇到了这个错误,我不确定如何解决这个问题。尝试了很多搜索,但没有任何进展。 这是错误 BasicNetwork.performRequest: Unexpected response code 40
也许这个问题之前已经发布过,但它并没有解决我的问题。我正在尝试使用 laravel 作为后端将数据从 android 发送到 mysql 数据库。我开发了restful Api。这是我的代码。
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
我是一名优秀的程序员,十分优秀!