Je veux récupérer des photos en arrière-plan en utilisant AsyncTask. Je reçois une photo sous forme de chaîne sous forme encodée en base64. Cependant, j'ai "L'application peut-être faire trop de travail sur son thread principal" message d'erreur.Lors de la récupération d'images à partir de json obtenir "L'application peut-être faire trop de travail sur son thread principal" erreur
Mon activité:
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, ItemClickHandler{
private RecyclerView recyclerView;
private RecyclerViewAdapter adapter;
private LayoutManager layoutManager;
private ArrayList<Device> devices;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_action_bar);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
adapter = new RecyclerViewAdapter(devices, this);
recyclerView.setAdapter(adapter);
initImages();
}
private void initImages() {
Thread thread = new Thread() {
@Override
public void run() {
for(int i = 0; i < devices.size(); i++){
final int pos = i;
GetImageJSON getImage = new GetImageJSON(MainActivity.this){
@Override
protected void onPostExecute(final String result) {
Log.d(TAG, result);
if(pos <= recyclerView.getLayoutManager().getChildCount()){
adapter.updateItem(ImageManager.convertToBitmap(result), pos);
}
}
};
getImage.execute(ConnectionConfig.getUserItemImage(devices.get(i).getId()));
}
}
};
thread.start();
}
}
classe GetImageJSON:
public class GetDataJSON extends AsyncTask<String, Void, String> {
private static String charset = "UTF-8";
@Override
protected String doInBackground(String... args) {
String result = parseJSONString(args[0]);
if(!result.isEmpty()){
try{
JSONObject json = new JSONObject(result);
JSONObject jsonObject = json.getJSONObject(ConnectionConfig.TAG_RESULT);
String base64String = jsonObject.getString("image");
Log.d(TAG, base64String);
Bitmap bitmap = ImageManager.convertToBitmap(base64String);
bitmap = ImageManager.scaleDownBitmap(bitmap, context);
Log.d(TAG, "got result: " + result);
return ImageManager.convertBitMapToString(bitmap);
}catch (JSONException e){
e.printStackTrace();
}
}
return result;
}
public static String parseJSONString(String... args){
String result = "";
InputStream inputStream = null;
Log.d(TAG, args[0]);
try {
URL url = new URL(args[0]);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setDoOutput(false);
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept-Charset", charset);
conn.setConnectTimeout(15000);
conn.connect();
try {
InputStream in = new BufferedInputStream(conn.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
result += line;
}
} catch (IOException e) {
e.printStackTrace();
}
conn.disconnect();
} catch (Exception e) {
Log.d(TAG, "Exception", e);
} finally {
try{
if(inputStream != null)
inputStream.close();
}catch(Exception e){
Log.d(TAG, e.getMessage());
}
}
return result;
}
}
Je ne pouvais pas trouver une solution. S'il vous plaît, suggérez n'importe lequel. Comment je peux optimiser le processus de récupération des données.
vous initialisez 'devices' ArrayList? –
Oui, je l'obtiens de l'activité précédente – Madina
avez-vous essayé avec le nouveau Runnable() dans le Thread? comme 'Thread t1 = new Thread (nouveau Runnable() { public void run() { // code va ici }}); t1.start(); ' –