2012-11-05 137 views
0

出於某種原因,我的doinbackground方法執行,但onPost執行不我把把覆蓋註釋和匹配的參數,我也做task.execute而不是隻調用doinbackground,但它仍然沒有執行。 這是我的代碼如下所示:我的onPostExecute方法不會被調用

Android的活動:

package com.Keb3mr.workoutscheduler; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 

import org.apache.http.HttpResponse; 
import org.apache.http.entity.mime.MultipartEntity; 
import org.apache.http.entity.mime.content.StringBody; 

import com.Keb3mr.workoutscheduler.MyApp; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 

public class MainActivity extends Activity implements HttpCallback, OnClickListener{ 

    private EditText user_; 
    private EditText pass_; 
    private Button login_; 
    private Button signUp_; 
    private MyApp appState_; 
    private String url_ = "http://192.168.1.110:8888/vshare/account/create"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     setTitle("Workout Scheduler"); 
     MyApp appState = ((MyApp)getApplicationContext()); 
     appState_ = appState; 
     user_ = (EditText) findViewById(R.id.userText); 
     pass_ = (EditText) findViewById(R.id.passwordText); 
     login_ = (Button) findViewById(R.id.loginButton); 
     signUp_ = (Button) findViewById(R.id.signupButton); 
     appState_.setUser("default"); 
     appState_.setPass("default"); 
     login_.setOnClickListener(this); 
     signUp_.setOnClickListener(this); 
    } 

    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     if(v.getId() == R.id.loginButton){ 
      Log.i("amre", "button pushed"); 
      appState_.setUser(user_.getText().toString()); 
      appState_.setPass(pass_.getText().toString()); 
      try { 
       login(url_); 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      Intent i = new Intent(this, ViewCreate.class); 
      startActivity(i); 
     } 
     else if(v.getId() == R.id.signupButton){ 
      appState_.setUser(user_.getText().toString()); 
      appState_.setPass(pass_.getText().toString()); 
     } 

    } 

    public void login(String server) throws Exception{ 
     Log.i("button", "login"); 
     MultipartEntity entity = new MultipartEntity(); 
     entity.addPart("user", new StringBody(appState_.getUser())); 
     entity.addPart("pass", new StringBody(appState_.getPass())); 
     LoginTask t = new LoginTask(entity, url_, this); 
     t.execute(); 
     Log.i("Task", "Executed"); 
    } 

    public void onResponse(HttpResponse resp) { 

     String line = null; 
     InputStream is = null; 
     try { 
      is = resp.getEntity().getContent(); 
     } catch (IllegalStateException e1) { 

      e1.printStackTrace(); 
     } catch (IOException e1) { 

      e1.printStackTrace(); 
     } 


     BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 

     StringBuilder sb = new StringBuilder(); 

     try { 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
     } catch (IOException e1) { 

      e1.printStackTrace(); 
     } 
     String result = sb.toString(); 
     Log.i("reuslt:", result); 
    } 

    public void onError(Exception e) { 
     // TODO Auto-generated method stub 

    } 
} 

我打電話登錄中調用t.execute的onclick方法。 這是我的任務異步的樣子:

package com.Keb3mr.workoutscheduler; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 


import android.os.AsyncTask; 
import android.util.Log; 

public class LoginTask extends AsyncTask<Void, Void, HttpResponse> { 

    private HttpEntity entity_; 

    private String url_; 

    private HttpCallback callback_; 

    public LoginTask(HttpEntity entity, String url, HttpCallback callback){ 
     super(); 
     entity_ = entity; 
     url_ = url; 
     callback_ = callback; 
    } 



    @Override 
    protected void onPostExecute(HttpResponse result) { 
     // TODO Auto-generated method stub 
     Log.i("onPost", "executing"); 
     super.onPostExecute(result); 
     callback_.onResponse(result); 


    } 



    @Override 
    protected HttpResponse doInBackground(Void... params) { 
     // TODO Auto-generated method stub 
     Log.i("doinbackground", "executing"); 
     HttpResponse resp = null; 
     try{ 
      HttpPost post = new HttpPost(url_); 
      Log.i("doinbackground", "httppst"); 
      post.setEntity(entity_); 
      Log.i("doinbackground", "setentity"); 
      HttpClient client = new DefaultHttpClient(); 
      Log.i("doinbackground", "client"); 
      resp = client.execute(post); 
      Log.i("doinbackground", "resp"); 
     } 
     catch(Exception e){ 
      Log.i("doinbackground", "error"); 
      e.printStackTrace(); 

     } 
     return resp; 
    } 

} 
+1

什麼是您的輸出? 「'resp」'曾經顯示過嗎? – Eric

+0

我只是再看看日誌貓,它看起來沒有得到響應,因爲它沒有被打印後的權利。我猜這些服務器端有問題 – Amre

+0

如果您在doInBackground方法中遇到異常,Post執行方法將不會調用 – Sandy09

回答

0

請檢查您已經給出了明顯的或不..互聯網的權限,如果網絡工作正常,請檢查您的下載地址是否有效。如果你下載url無效它引發了doInBackground方法中的異常,所以onPostExecute方法將不會調用