当我使用jsonObject请求进行网络调用时。我没有收到任何答复。我总是收到请求的HTTP响应=
当请求缓慢时,Android截击双发:
来源1,来源2,来源3
package com.example.mts3.hammadnewsapp;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONObject;
import java.util.ArrayList;
public class NewsDetailsActivity extends AppCompatActivity {
RecyclerView recyclerView;
NewsAdapter adapter;
ArrayList<NewsOpen> newsList =new ArrayList<>();
Intent intent;
TextView tv_newsheading;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
Context context;
String DATA_URL="http://ec2-54-147-238-136.compute-1.amazonaws.com/hmc/api/getnewsfeeds?order=asc";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_details);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
newsList = new ArrayList<>();
recyclerView = findViewById(R.id.rv_newsdetails);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new NewsAdapter(this, newsList);
String string = (String) getText(R.string.random_text);
newsList.add(new NewsOpen(R.drawable.hdr_bg_plain, "Chief Medical Officer", "10-11-2020", string));
// newsList.add(new NewsOpen("image!!", "Chief Medical Officer", "10-11-2020", string));
recyclerView.setAdapter(adapter);
/*backgroundTask.getList(new BackgroundTask.arrayCallBack() {
@Override
public void onSuccess(ArrayList<Contact> contacts) {
adapter = new RecyclerAdapter(contacts);
recyclerView.setAdapter(adapter);
}
@Override
public void onFail(String msg) {
// Do Stuff
}
});*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_news_list, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_share) {
//Toast.makeText(NewsDetailsActivity.this, "Share menu clicked ", Toast.LENGTH_LONG).show();
callApi();
return true;
}
if (id == R.id.action_logout) {
sharedPreferences= PreferenceManager.getDefaultSharedPreferences(this);
editor= sharedPreferences.edit();
editor.putBoolean("Verified user",false);
editor.commit();
Toast.makeText(NewsDetailsActivity.this,"Logged out ",Toast.LENGTH_LONG).show();
Intent intent=new Intent(NewsDetailsActivity.this,MainActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
private void callApi() {
// new DefaultRetryPolicy(30000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
// int x=3;// retry count
// DefaultRetryPolicy defaultRetryPolicy=new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS*1,x,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
//DefaultRetryPolicy defaultRetryPolicy= new DefaultRetryPolicy(20 * 1000, 0, 1.0f);
RequestQueue requestQueue= Volley.newRequestQueue(this);
//System.setProperty("http.keepAlive", "false");
JsonObjectRequest jsonObject =new JsonObjectRequest(Request.Method.GET,DATA_URL, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.e("res",response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//int x=2;// retry count
//jsonObject.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 48, x, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
//jsonObject.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 48,0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
//jsonObject.setRetryPolicy(new DefaultRetryPolicy(0, -1, 0));
//jsonObject.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
//jsonObject.setRetryPolicy(new DefaultRetryPolicy(0,-1, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
/*jsonObject.setRetryPolicy(
new DefaultRetryPolicy(
500000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
)
);*/
//jsonObject.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
//new DefaultRetryPolicy(10000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
//jsonObject.setRetryPolicy(new DefaultRetryPolicy(0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
//jsonObject.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
//int socketTimeout = 30000;//30 seconds - change to what you want
// RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
// jsonObject.setRetryPolicy(policy);
int custom_timeout_ms = 10000;
DefaultRetryPolicy policy = new DefaultRetryPolicy(custom_timeout_ms,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
jsonObject.setRetryPolicy(policy);
requestQueue.add(jsonObject);
}
}
尝试使用字符串请求
public class TestActivity extends AppCompatActivity {
RequestQueue requestQueue;
String DATA_URL = "http://ec2-54-147-238-136.compute-1.amazonaws.com/hmc/api/getnewsfeeds?order=asc";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestQueue = Volley.newRequestQueue(this);
callApi();
}
private void callApi() {
StringRequest stringRequest = new StringRequest(Request.Method.GET, DATA_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("res", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("onErrorResponse: ", error.getLocalizedMessage());
}
});
requestQueue.add(stringRequest);
}
如果您的应用程序在响应之前完成,则会导致连接超时。您必须在截取和重试策略中添加连接超时。这里有一个例子
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
url,
request,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
}
}, new VolleyErrorListener(this, AuthController.LOGIN_ERROR)) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
return new VolleyHeader().getHeaders();
}
};
jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
50000,
0,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
NetworkRequestQueue.getInstance(mContext).getRequestQueue().add(jsonObjectRequest);
NetworkRequestQueue类如下所示:
public class NetworkRequestQueue {
private static NetworkRequestQueue mInstance;
private static Context mCtx;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private NetworkRequestQueue(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
public static synchronized NetworkRequestQueue getInstance(Context context) {
if (mInstance == null) {
mInstance = new NetworkRequestQueue(context);
}
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
Cache cache = new DiskBasedCache(mCtx.getCacheDir(), 10 * 1024 * 1024);
Network network = new BasicNetwork(new HurlStack());
mRequestQueue = new RequestQueue(cache, network);
// Don't forget to start the volley request queue
mRequestQueue.start();
}
return mRequestQueue;
}
public ImageLoader getImageLoader() {
return mImageLoader;
}
}
另一个使用StringRequest
获取Json响应的解决方案
private void callApi() {
RequestQueue mrequestQueue;
StringRequest stringRequest;
mrequestQueue = Volley.newRequestQueue(this);
stringRequest = new StringRequest(Request.Method.GET, DATA_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
System.out.println("json"+jsonObject);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//Log.i(TAG, "Response: " + error.toString());
}
});
mrequestQueue.add(stringRequest);
}
我正在使用截击来管理我的应用程序。有时,当响应很大时,我会在没有太多RAM的设备上获得OOM。我不确定如何解决这个问题。我知道Volley会将其响应存储在内存中,但我的应用程序太多地围绕着Volley,切换起来会很痛苦。我在使用改装时也遇到了问题。我尝试过使用JsonReader,但它似乎仍在发生。我有一个用于截击的自定义请求。它返回一个Gson JsonObject。这是我目前的代码,当响应数据
我正在尝试使用Volley获取JSON对象以创建动态自动完成,但在使用Volley请求时,我在日志cat中看到: 截击:[486]基本网络。logSlowRequests:请求的HTTP响应= 这些答案并没有解决我的问题。 这里是初始化请求队列的Singleton类: 发出请求的方法: 下面是HTTP Connector类:
问题内容: 我想捕获到我正在浏览使用python的Selenium的网站的访问量,因为使用代理将使访问量达到https的水平,这将不会使我走远。 我的想法是使用selenium来运行phantomJS并使用phantomJS执行脚本(不是在页面上使用webdriver.execute_script(),而是在phantomJS本身上)。我在想netlog.js脚本(从这里https://githu
问题内容: 我在使用时遇到了麻烦,因为我之前从未见过它,也不知道我在用它做什么。 基本上,我正在关闭部队,因为我试图在主类上运行连接。有人可以帮我添加代码吗: 问题答案: 只是一个简单的例子,它看起来像:
我是RXJava的新手。在一个场景中,我希望调用第一个登录webservice(),如果成功,则希望调用另一个webservice()以获取用户信息。
问题内容: 我可以使用Firefox.get_screenshot_as_file(‘2.png’)对整个页面进行屏幕截图,但是当我使用pass.screenshot(‘1.png’)对网络元素进行屏幕截图时,总是会引发以下异常: 问题答案: 之所以会出现此异常,是因为如果没有某些第三方库或您自己的代码来处理硒中的元素,就无法截取它的屏幕快照。 它使用称为PIL的库来执行此操作: