簡體   English   中英

可運行崩潰中的HTTPPost

[英]HTTPPost within Runnable Crashes

這個問題使我生氣。 我正在重復使用可在其他項目/類上使用的相同代碼,但不適用於當前項目。 我所做的就是更改變量(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)

問題出在您調用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();
    }
}

您的循環器將處理來自服務器的響應,並可以對數據進行相應的處理。

當您的可運行對象完成后,使用post ..或runOnUiThread將Toast發布回UI線程:

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

或者,將您的ui控件聲明為final並從async任務發布到它們:

final EditText someControl;
....

....
In async Task:
....
someControl.post(new Runnable() { Toast.makeText(...); });

您應該轉換代碼,以便使用AsyncTask而不是實現Runnable -通過AsyncTask,您可以使用publishProgress(...)方法與onProgressUpdate(...)結合提供實時更新onProgressUpdate(...)最后,您可以使用onPostExecute(...)整理內容並在UI上進行調用。

上的例子

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

相對比較完整,所以我不會在這里重新發布。

另一方面,您需要清理HTTP連接,確保對HTTP實體(每個實體中的最后一個...)都調用了consumeContent() )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM