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