0

这个问题让我很生气。我正在重新使用适用于其他项目/类但不适用于我当前项目的相同代码。我所做的只是更改变量(POST 值和 EditText 名称)。但它没有用。

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;


import android.app.Activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.preference.PreferenceManager;
import android.content.SharedPreferences;

public class LoginActivity extends BaseActivity implements OnClickListener {
    private SharedPreferences settings;
    private EditText email;
    private EditText epassword;
    private EditText password2;
    private EditText mobile;
    private EditText eusername;
    private EditText txtMessage;
    private Button sendBtn;
    //private String uriAPI =getString(R.string.loginurl);

    protected static final int REFRESH_DATA = 0x00000001;


    Handler mHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
            case REFRESH_DATA:
                String result = null;
                if (msg.obj instanceof String)
                    result = (String) msg.obj;
                if (result != null)
                    Toast.makeText(LoginActivity.this, result, Toast.LENGTH_LONG).show();


                break;
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        eusername = (EditText) findViewById(R.id.musername);
        epassword = (EditText) findViewById(R.id.mpassword);
        sendBtn = (Button) findViewById(R.id.loginnow_btn);

        if (sendBtn != null) {
            sendBtn.setOnClickListener(this);
        }

    }

    @Override
    public void onClick(View v)

    {
        //Toast.makeText(LoginActivity.this,"Button Clicked!", Toast.LENGTH_LONG).show();
        if (v == sendBtn)
        {
            String err= null;
            final String dusername = eusername.getEditableText().toString();
            final String dpassword = epassword.getEditableText().toString();

            Toast.makeText(LoginActivity.this, dusername, Toast.LENGTH_SHORT).show();
            if (dusername.isEmpty())
            {

                err = err + "Please Enter Email";
            }
            else if (dpassword.isEmpty()) {
                err = err + "Please enter password";
            }

            if (err==null) {

                Toast.makeText(LoginActivity.this, "About to run sendPostRunnable", Toast.LENGTH_LONG).show();      
                Thread gt = new Thread(new sendPostRunnable(dusername,dpassword));
                gt.start();
            }
            else {
                Toast.makeText(LoginActivity.this, err, Toast.LENGTH_LONG).show();              
            }
        }
    }

    class sendPostRunnable implements Runnable
    {
        String strTxt = null;
        String eusername = null;
        String epassword = null;
        public sendPostRunnable(String username, String password)
        {
            this.epassword = password;
            this.eusername = username;

            Toast.makeText(LoginActivity.this,this.eusername, Toast.LENGTH_SHORT).show();
            Toast.makeText(LoginActivity.this,this.epassword, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void run()
        {
            Toast.makeText(LoginActivity.this,"SendPostDataToInternet entrance", Toast.LENGTH_LONG).show();
            String result = sendPostDataToInternet(eusername, epassword);
            //Toast.makeText(LoginActivity.this,"SendPostDataToInternet running", Toast.LENGTH_LONG).show();
            //Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
            mHandler.obtainMessage(REFRESH_DATA, result).sendToTarget();
            //Toast.makeText(LoginActivity.this,"Obtainmessaged", Toast.LENGTH_LONG).show();
        }


    }

    private String sendPostDataToInternet(String username, String password)
    {
        String uriAPI =getString(R.string.loginurl);
        HttpPost httpRequest = new HttpPost(uriAPI);

        List<NameValuePair> params = new ArrayList<NameValuePair>();

        params.add(new BasicNameValuePair("email", username));
        params.add(new BasicNameValuePair("password", password));
        Toast.makeText(LoginActivity.this,"TT", Toast.LENGTH_LONG).show();
        try

        {



            httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

            HttpResponse httpResponse = new DefaultHttpClient()
                    .execute(httpRequest);

            if (httpResponse.getStatusLine().getStatusCode() == 200) {

                String strResult = EntityUtils.toString(httpResponse
                        .getEntity());

                return strResult;
            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }


}

在调试期间,我意识到问题出在

gt.start();

这是一个日志

03-06 02:39:35.134: E/AndroidRuntime(1916): FATAL EXCEPTION: Thread-140
03-06 02:39:35.134: E/AndroidRuntime(1916): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
03-06 02:39:35.134: E/AndroidRuntime(1916):     at android.os.Handler.<init>(Handler.java:197)
03-06 02:39:35.134: E/AndroidRuntime(1916):     at android.os.Handler.<init>(Handler.java:111)
03-06 02:39:35.134: E/AndroidRuntime(1916):     at android.widget.Toast$TN.<init>(Toast.java:324)
03-06 02:39:35.134: E/AndroidRuntime(1916):     at android.widget.Toast.<init>(Toast.java:91)
03-06 02:39:35.134: E/AndroidRuntime(1916):     at android.widget.Toast.makeText(Toast.java:238)
03-06 02:39:35.134: E/AndroidRuntime(1916):     at com.pbd.b_prototype.LoginActivity$sendPostRunnable.run(LoginActivity.java:162)
03-06 02:39:35.134: E/AndroidRuntime(1916):     at java.lang.Thread.run(Thread.java:856)
4

3 回答 3

2

问题出在你打电话的方式上sendPostDataToInternet()。您正在创建一个新线程来进行网络通信,这很好:

Thread gt = new Thread(new sendPostRunnable(dusername,dpassword));

但是你需要在这个线程中为处理程序设置一个 Looper。简短的回答是在Looper中创建您的处理程序,文档非常清楚,但可以这样编写:

public class LooperThread extends Thread {

    @Override
    public void run() {
        Looper.prepare();

        Handler mHandler = new Handler()
        {
            @Override
            public void handleMessage(Message msg)
            {
                 // handle your message here
            }
        };
        Looper.loop();
    }
}

您的 Looper 将处理来自服务器的响应,并可以相应地对数据采取行动。

于 2013-03-06T03:31:30.733 回答
1

当您的可运行文件完成后,使用 post.. 或 runOnUiThread 将 Toast 发布回 UI 线程:

LoginActivity.this.runOnUiThread(new Runnable() { Toast.makeText( ... ); });

或者,将您的 ui 控件声明为 final 并从异步任务中发布到它们:

final EditText someControl;
....

....
In async Task:
....
someControl.post(new Runnable() { Toast.makeText(...); });
于 2013-03-06T03:58:22.267 回答
0

publishProgress(...)您应该转换您的代码,以便使用 AsyncTask 而不是实现 Runnable - 使用 AsyncTask,您可以使用这些方法提供实时更新,onProgressUpdate(...)最后,您可以使用它onPostExecute(...)来包装并在 UI 上进行调用。

上的例子

http://developer.android.com/reference/android/os/AsyncTask.html

比较完整,这里就不转发了。

另一方面,您需要清理 HTTP 连接,确保调用consumeContent()您的 HTTP 实体(每一个实体......)。

于 2013-03-06T03:05:05.853 回答