2
I would like to redeem the result value that is inside Try, the code is correct directly in Activity, but when I put in asyncTask
I cannot set it in the Return result;.
Follows the code AsyncTask
:
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
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.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
@SuppressWarnings("ALL")
public class MecanismoString extends AsyncTask <String, String, String> {
RequestQueue requestQueue;
StringRequest stringRequest;
private Interfacestring execinterface;
private Context context;
String tipo, url, usuario, senha, result;
Toast toast;
public MecanismoString (Context context, Interfacestring execinterface){
this.context = context;
this.execinterface = execinterface;
}
@Override
public String doInBackground(String... params) {
Log.i("Script", "1 ->" + params[0]);
Log.i("Script", "2 ->" + params[1]);
Log.i("Script", "3 ->" + params[2]);
Log.i("Script", "4 ->" + params[3]);
tipo = params[0];
url = params[1];
usuario = params[2];
senha = params[3];
requestQueue = Volley.newRequestQueue(context);
stringRequest = new StringRequest(Request.Method.POST,
url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
int perfil = jsonObject.getInt("perfil");
result = String.valueOf(perfil);
} catch (Exception e) {
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("login", usuario);
params.put("senha", senha);
return params;
}
};
requestQueue.add(stringRequest);
return result;
}
private void chamarJSON() {
requestQueue = Volley.newRequestQueue(context);
stringRequest = new StringRequest(Request.Method.POST,
url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
int perfil = jsonObject.getInt("perfil");
String result = String.valueOf(perfil);
} catch (Exception e) {
Log.v("LogLogin", e.getMessage());
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("LogLogin", error.getMessage());
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
params.put("login", usuario);
params.put("senha", senha);
return params;
}
};
requestQueue.add(stringRequest);
}
@Override
protected void onPostExecute(String result) {
execinterface.carregarString(result);
}
}
got a little confused understanding your doubt, put the code with the
asyncTask
to make it easier– Ricardo Pontual
I put the complete code, summarizing I want to take the String result that is inside Try and set in onPostExecute
– Tiago Marisco
You’ve done Debug to see which way is going?
– Murillo Comino
It’s round and error free, but it’s like there’s nothing inside Try, it actually reads the Try but it doesn’t send the result = String.valueOf(profile); to the Return result;
– Tiago Marisco