У меня есть LoaderManager, который должен поддерживать login_loader. Поэтому у меня возникает проблема, когда я вызываю 2nd getSupportLoaderManager.initLoader (1, null, this) или я использую restartLoader () второй вызов не показывает HTTP-запрос.
Поток выполняется, когда URL 1 / Шаг 1 200 в порядке и ничего не происходит, URL 2 обработан. Мой подход к этому - вызов getSupportLoaderManager.initLoader on onLoadFinished () для шага 1 / URL 1.
Я использую сообщение HTTP, а затем в ответ возвращаются данные (JSON).
Естьфрагмент моего кода
MainActivity.java Раздел LoaderManager
@Override
public Loader<String> onCreateLoader(int id,Bundle bundle){
HashMap<String,String> params = new HashMap<String,String>();
String url = null;
if(step_login == 1 ){
url = Api.url_loginuser;
System.out.println("Loader step 1 "+url);
params.put("email",email_tf.getText().toString().trim());
params.put("password",password_tf.getText().toString().trim());
return new login_loader(this,url,params,CODE_POST_REQUEST);
}
else if(step_login == 2){
url = Api.url_decode_login;
System.out.println("Loader step 2 "+url);
//HashMap<String,String> params = new HashMap<>();
params.put("token",tokeen);
//params.put("email",email_tf.getText().toString().trim());
//params.put("password",password_tf.getText().toString().trim());
return new login_loader(this,url,params,CODE_POST_REQUEST);
}
System.out.println("Loader OK: "+url);
return null;
}
@Override
public void onLoadFinished(Loader<String>loader,String data){
try{
JSONObject object = new JSONObject(data);
if(!object.getBoolean("error")){
if(step_login == 1){
tokeen = object.getString("jwt");
step_login = step_login+1;
getSupportLoaderManager().restartLoader(1,null,this);
}
else if(step_login == 2){
login_decode(object.getJSONObject("parse"));
}
//Toast toast = Toast.makeText(getApplicationContext(),"Login OK!",Toast.LENGTH_LONG);
//toast.show();
//SqliteLoginBackground sqliteLoginBackground = new SqliteLoginBackground();
//sqliteLoginBackground.execute("add_user",)
//login_act();
}
else{
Toast toast = Toast.makeText(getApplicationContext(),object.getString("message"),Toast.LENGTH_SHORT);
toast.show();
}
}catch (JSONException e){
Toast toast = Toast.makeText(getApplicationContext(),"Some error occurred.",Toast.LENGTH_SHORT);
toast.show();
e.printStackTrace();
}
}
@Override
public void onLoaderReset(Loader<String>loader){
}
А это мой login_loader.java
public class login_loader extends AsyncTaskLoader<String> {
String url;
private static final int CODE_GET_REQUEST = 1024;
private static final int CODE_POST_REQUEST = 1025;
HashMap<String, String> params;
String a;
int requestCode;
public login_loader(Context context, String url, HashMap<String, String> params, int reqcode){
super(context);
this.url = url;
this.params = params;
this.requestCode = reqcode;
}
@Override
protected void onStartLoading(){
forceLoad();
}
@Override
protected void onStopLoading(){
cancelLoad();
}
@Override
public String loadInBackground(){
RequestHandler requestHandler = new RequestHandler();
if (requestCode == CODE_POST_REQUEST){
//a = requestHandler.sendPostRequest(url,params);
Log.e("INFO:",url);
return requestHandler.sendPostRequest(url, params);
}
if (requestCode == CODE_GET_REQUEST){
//a = requestHandler.sendGetRequest(url);
//Log.e("INFO:",a);
return requestHandler.sendGetRequest(url);
}
return null;
}
@Override
protected void onReset(){
onStopLoading();
}
}
Это фрагмент кода для RequestHandler в sendPostRequest, который возвращает данные
public String sendPostRequest(String requestURL,
HashMap<String, String> postDataParams) {
//Creating a URL
URL url;
//StringBuilder object to store the message retrieved from the server
StringBuilder sb = new StringBuilder();
try {
//Initializing Url
url = new URL(requestURL);
//System.out.println("PROCESSED URL:"+url);
//Creating an httmlurl connection
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//Configuring connection properties
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
//Creating an output stream
OutputStream os = conn.getOutputStream();
//Writing parameters to the request
//We are using a method getPostDataString which is defined below
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();
//System.out.println("HTP CODE:"+String.valueOf(responseCode));
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
sb = new StringBuilder();
String response;
//Reading server response
int ia = 1;
//System.out.println("A"+br.readLine());
while ((response = br.readLine()) != null) {
sb.append(response);
//System.out.println("LINE:"+ia+ response);
}
}
} catch (Exception e) {
System.out.println("ERROR!!!!!!"+e);
e.printStackTrace();
}
Log.e("DATA:",sb.toString());
return sb.toString();
}