0

何らかの理由で doinbackground メソッドは実行されますが、onPost は実行されません。オーバーライド注釈を付けてパラメーターを一致させ、doinbackground を呼び出すだけでなく task.execute も実行していますが、まだ実行されません。これは私のコードがどのように見えるかです:

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 メソッドで login を呼び出します。これは私の非同期タスクがどのように見えるかです:

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;
    }

}
4

1 に答える 1

0

マニフェストでインターネット許可を与えたかどうかを確認してください..インターネットが正常に機能している場合は、ダウンロードURLが有効かどうかを確認してください..ダウンロードURLが有効でない場合は、doInBackgroundメソッドで例外が発生するため、onPostExecuteメソッドは呼び出されません

于 2012-11-05T05:53:42.643 に答える