Runnable Crashes中的HTTPPost

时间:2013-03-06 02:56:34

标签: java android multithreading http-post runnable

这个问题让我很生气。我正在重复使用同样的代码,该代码适用于其他项目/类,但不适用于我当前的项目。我所做的就是更改变量(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)

3 个答案:

答案 0 :(得分: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将处理来自服务器的响应,并可以相应地处理数据。

答案 1 :(得分:1)

当你的runnable完成后,使用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(...); });

答案 2 :(得分:0)

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

上的例子

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

相对完整,所以我不会在这里重新发布。

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