我有一个LoaderManager来维护login_loader。因此, 我有一个问题,当我调用第二个 getSupportLoaderManager.initLoader(1,null,this)或第二次调用 restartLoader()而不显示HTTP请求时。 >
流程是URL 1 /步骤1200正常且没有任何错误,URL 2已处理。我的解决方法是在 onLoadFinished()上调用 getSupportLoaderManager.initLoader 步骤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();
}
}
这是sendPostRequest上RequestHandler的片段,返回数据
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();
}