我的Android应用程序出现问题,我正在尝试开发,显然它会在执行doInBackground时崩溃,我的logcat看起来像这样:
08-19 04:40:57.046: D/request!(4766): starting
08-19 04:40:57.125: W/dalvikvm(4766): threadid=9: thread exiting with uncaught exception (group=0x40018578)
08-19 04:40:57.132: E/AndroidRuntime(4766): FATAL EXCEPTION: AsyncTask #1
08-19 04:40:57.132: E/AndroidRuntime(4766): java.lang.RuntimeException: An error occured while executing doInBackground()
08-19 04:40:57.132: E/AndroidRuntime(4766): at android.os.AsyncTask$3.done(AsyncTask.java:200)
08-19 04:40:57.132: E/AndroidRuntime(4766): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
08-19 04:40:57.132: E/AndroidRuntime(4766): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
08-19 04:40:57.132: E/AndroidRuntime(4766): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
08-19 04:40:57.132: E/AndroidRuntime(4766): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
08-19 04:40:57.132: E/AndroidRuntime(4766): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
08-19 04:40:57.132: E/AndroidRuntime(4766): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
08-19 04:40:57.132: E/AndroidRuntime(4766): at java.lang.Thread.run(Thread.java:1019)
08-19 04:40:57.132: E/AndroidRuntime(4766): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters.
08-19 04:40:57.132: E/AndroidRuntime(4766): at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:577)
08-19 04:40:57.132: E/AndroidRuntime(4766): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:292)
08-19 04:40:57.132: E/AndroidRuntime(4766): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
08-19 04:40:57.132: E/AndroidRuntime(4766): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-19 04:40:57.132: E/AndroidRuntime(4766): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
08-19 04:40:57.132: E/AndroidRuntime(4766): at com.example.prova1.JSONParser.makeHttpRequest(JSONParser.java:110)
08-19 04:40:57.132: E/AndroidRuntime(4766): at com.example.prova1.Login$AttemptLogin.doInBackground(Login.java:159)
08-19 04:40:57.132: E/AndroidRuntime(4766): at com.example.prova1.Login$AttemptLogin.doInBackground(Login.java:1)
08-19 04:40:57.132: E/AndroidRuntime(4766): at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-19 04:40:57.132: E/AndroidRuntime(4766): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
08-19 04:40:57.132: E/AndroidRuntime(4766): ... 4 more
08-19 04:41:04.726: E/WindowManager(4766): Activity com.example.prova1.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4054b1a8 that was originally added here
08-19 04:41:04.726: E/WindowManager(4766): android.view.WindowLeaked: Activity com.example.prova1.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4054b1a8 that was originally added here
08-19 04:41:04.726: E/WindowManager(4766): at android.view.ViewRoot.<init>(ViewRoot.java:263)
08-19 04:41:04.726: E/WindowManager(4766): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:171)
08-19 04:41:04.726: E/WindowManager(4766): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:114)
08-19 04:41:04.726: E/WindowManager(4766): at android.view.Window$LocalWindowManager.addView(Window.java:424)
08-19 04:41:04.726: E/WindowManager(4766): at android.app.Dialog.show(Dialog.java:241)
08-19 04:41:04.726: E/WindowManager(4766): at com.example.prova1.Login$AttemptLogin.onPreExecute(Login.java:126)
08-19 04:41:04.726: E/WindowManager(4766): at android.os.AsyncTask.execute(AsyncTask.java:391)
08-19 04:41:04.726: E/WindowManager(4766): at com.example.prova1.Login.onClick(Login.java:93)
08-19 04:41:04.726: E/WindowManager(4766): at android.view.View.performClick(View.java:2485)
08-19 04:41:04.726: E/WindowManager(4766): at android.view.View$PerformClick.run(View.java:9080)
08-19 04:41:04.726: E/WindowManager(4766): at android.os.Handler.handleCallback(Handler.java:587)
08-19 04:41:04.726: E/WindowManager(4766): at android.os.Handler.dispatchMessage(Handler.java:92)
08-19 04:41:04.726: E/WindowManager(4766): at android.os.Looper.loop(Looper.java:130)
08-19 04:41:04.726: E/WindowManager(4766): at android.app.ActivityThread.main(ActivityThread.java:3687)
08-19 04:41:04.726: E/WindowManager(4766): at java.lang.reflect.Method.invokeNative(Native Method)
08-19 04:41:04.726: E/WindowManager(4766): at java.lang.reflect.Method.invoke(Method.java:507)
08-19 04:41:04.726: E/WindowManager(4766): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-19 04:41:04.726: E/WindowManager(4766): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
08-19 04:41:04.726: E/WindowManager(4766): at dalvik.system.NativeStart.main(Native Method)
08-19 04:41:05.070: D/dalvikvm(4766): GC_CONCURRENT freed 235K, 48% free 2955K/5639K, external 507K/517K, paused 3ms+3ms
我的java代码是这样的;
public class Login extends Activity implements OnClickListener{
private EditText user, pass;
private Button mSubmit, mRegister;
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
//php login script location:
//localhost :
//testing on your device
//put your local ip instead, on windows, run CMD > ipconfig
//or in mac's terminal type ifconfig and look for the ip under en0 or en1
//testing on Emulator:
private static final String LOGIN_URL = "login_url";
//testing from a real server:
//JSON element ids from repsonse of php script:
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//setup input fields
user = (EditText)findViewById(R.id.username);
pass = (EditText)findViewById(R.id.password);
//setup buttons
mSubmit = (Button)findViewById(R.id.button1);
mRegister = (Button)findViewById(R.id.button2);
//register listeners
mSubmit.setOnClickListener(this);
mRegister.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
new AttemptLogin().execute();
break;
case R.id.button2:
Intent i = new Intent(this, Register.class);
startActivity(i);
break;
default:
break;
}
}
class AttemptLogin extends AsyncTask<String, String, String> {
/**
* */
boolean failure = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
String username = user.getText().toString();//my app seems to be crushing here
String password = pass.getText().toString();
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Log.d("request!", "starting");
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "POST", params);
// check your log for json response
Log.d("Login attempt", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", json.toString());
Intent i = new Intent(Login.this, ReadComments.class);
finish();
startActivity(i);
return json.getString(TAG_MESSAGE);
}else{
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null){
Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
}
任何帮助都将受到高度赞赏!!
答案 0 :(得分:0)
您的日志非常清楚地说:
Target host must not be null, or set in parameters.
正如已经指出的那样,LOGIN_URL
可能会格格不入。我建议你Log.v()
并亲自看看。
答案 1 :(得分:0)
尝试在onPreExecute()
中保存用户名和密码,因为您无法在doInBackground()
答案 2 :(得分:0)
您不能直接在EditText, TextView, Buttons
中使用doInBackground()
之类的小部件,而是在onPreExecute()
方法中使用这些小部件。其次,如果您必须在doInBackground()
方法中使用这些方法,则应使用runOnUIThread()
另一件事是您正在调用doInBackground()
中的其他活动,并在onPostExecute()
中撤消您的对话框,您应该从json.getString(TAG_MESSAGE)
和doInBackground()
之后返回dialog.dismiss()
字符串开始你的下一个活动见下面的代码:
@Override
protected String doInBackground(String... args) {
String username, password;
// Check for success tag
int success;
runOnUiThread(new Runnable() {
@Override
public void run() {
username = user.getText().toString();
password = pass.getText().toString();
}
});
//do your other stuff
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Log.d("request!", "starting");
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "POST", params);
// check your log for json response
Log.d("Login attempt", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
return json.getString(TAG_MESSAGE);
}else{
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url.equals("success message")){
Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
Intent i = new Intent(Login.this, ReadComments.class);
finish();
startActivity(i);
} else{
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
}
}
您可以从here了解runOnUiThread()
。
希望这会对你有所帮助。
答案 3 :(得分:0)
使用以下代码。
class LoginTask extends AsyncTask<String, Void, JSONObject> {
private ProgressDialog progressDialog;
String username;
String password;
@Override
protected void onPreExecute() {
username = user.getText().toString();
password = pass.getText().toString();
progressDialog = new ProgressDialog(mContext);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);
progressDialog.setTitle(R.string.message_progress_title);
progressDialog.show();
}
@Override
protected JSONObject doInBackground(String... params) {
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Log.d("request!", "starting");
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST", params);
return jsonObject;
}
@Override
protected void onPostExecute(JSONObject jsonObject) {
if(progressDialog.isShowing()) {
progressDialog.dismiss();
}
if(jsonObject != null) {
Log.i(TAG, "Login response : " + jsonObject);
try {
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Intent i = new Intent(Login.this, ReadComments.class);
startActivity(i);
finish();
}
else {
Toast.makeText(mContext, getResources().getString(R.string.login_failed), Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
else {
Toast.makeText(mContext, getResources().getString(R.string.connection_error), Toast.LENGTH_LONG).show();
}
}
答案 4 :(得分:0)
您有一个名为泄漏窗口的错误,因为您正在尝试 从doInBackground(...)打开一个Activity,pDialog(ProgressDialog)仍然是 打开你没有解雇。
因此,全局声明success
变量(声明failure
变量的地方)。尝试在Activity
view update
或onPostExecute(...)
onPostExecute(...)
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
if(pDialog != null) {
pDialog.dismiss();
if (success == 1) {
Log.d("Login Successful!", json.toString());
Intent i = new Intent(Login.this, ReadComments.class);
finish();
startActivity(i);
}
if (file_url != null){
Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
}
}
}