我是Android的新手,我想尝试创建登录系统....但是当我用一些值填充edittexts时它会给我一个错误:
FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
这是我的代码:
public class Login extends Activity implements OnClickListener {
private EditText user, pass;
private Button mSubmit;
private ProgressDialog pDialog;
static{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
JSONParser jsonParser = new JSONParser();
private static final String LOGIN_URL = "sorifgroupcom.ipage.com/Android/login.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
user = (EditText) findViewById(R.id.emailLogin);
pass = (EditText) findViewById(R.id.MPLogin);
mSubmit = (Button) findViewById(R.id.BtnLogin);
mSubmit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.BtnLogin:
new AttemptLogin().execute();
break;
default:
break;
}
}
class AttemptLogin extends AsyncTask<String, String, String> {
@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();
String password = pass.getText().toString();
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("email", username));
params.add(new BasicNameValuePair("motdepasse", password));
Log.d("request!", "starting");
// getting product details by making HTTP request
Log.i("tesssssssssssssssssssst", "test");
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());
// save user data
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(Login.this);
Editor edit = sp.edit();
edit.putString("email", username);
edit.commit();
finish();
return json.getString(TAG_MESSAGE);
} else {
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
Log.i("tesssssssssssssssssssst", "test");
return null;
}
protected void onPostExecute(String file_url) {
pDialog.cancel();
if (file_url != null) {
Toast.makeText(Login.this, "", Toast.LENGTH_LONG).show();
}
}
}
}
logCat打印:
01-08 00:14:23.345: E/Trace(1016): error opening trace file: No such file or directory (2)
01-08 00:14:29.244: E/AndroidRuntime(1016): FATAL EXCEPTION: AsyncTask #1
01-08 00:14:29.244: E/AndroidRuntime(1016): java.lang.RuntimeException: An error occured while executing doInBackground()
01-08 00:14:29.244: E/AndroidRuntime(1016): at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-08 00:14:29.244: E/AndroidRuntime(1016): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-08 00:14:29.244: E/AndroidRuntime(1016): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-08 00:14:29.244: E/AndroidRuntime(1016): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-08 00:14:29.244: E/AndroidRuntime(1016): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-08 00:14:29.244: E/AndroidRuntime(1016): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-08 00:14:29.244: E/AndroidRuntime(1016): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-08 00:14:29.244: E/AndroidRuntime(1016): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-08 00:14:29.244: E/AndroidRuntime(1016): at java.lang.Thread.run(Thread.java:856)
01-08 00:14:29.244: E/AndroidRuntime(1016): Caused by: java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=sorifgroupcom.ipage.com/Android/login.php
01-08 00:14:29.244: E/AndroidRuntime(1016): at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:591)
01-08 00:14:29.244: E/AndroidRuntime(1016): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
01-08 00:14:29.244: E/AndroidRuntime(1016): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-08 00:14:29.244: E/AndroidRuntime(1016): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-08 00:14:29.244: E/AndroidRuntime(1016): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-08 00:14:29.244: E/AndroidRuntime(1016): at org.Soufiane.voyagesociale.JSONParser.makeHttpRequest(JSONParser.java:51)
01-08 00:14:29.244: E/AndroidRuntime(1016): at org.Soufiane.voyagesociale.Login$AttemptLogin.doInBackground(Login.java:128)
01-08 00:14:29.244: E/AndroidRuntime(1016): at org.Soufiane.voyagesociale.Login$AttemptLogin.doInBackground(Login.java:1)
01-08 00:14:29.244: E/AndroidRuntime(1016): at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-08 00:14:29.244: E/AndroidRuntime(1016): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-08 00:14:29.244: E/AndroidRuntime(1016): ... 5 more
01-08 00:14:31.865: E/BufferQueue(35): [org.Soufiane.voyagesociale/org.Soufiane.voyagesociale.Login] drainQueueLocked: BufferQueue has been abandoned!
01-08 00:14:32.205: E/WindowManager(1016): Activity org.Soufiane.voyagesociale.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4102a2e0 that was originally added here
01-08 00:14:32.205: E/WindowManager(1016): android.view.WindowLeaked: Activity org.Soufiane.voyagesociale.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4102a2e0 that was originally added here
01-08 00:14:32.205: E/WindowManager(1016): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
01-08 00:14:32.205: E/WindowManager(1016): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)
01-08 00:14:32.205: E/WindowManager(1016): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
01-08 00:14:32.205: E/WindowManager(1016): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
01-08 00:14:32.205: E/WindowManager(1016): at android.view.Window$LocalWindowManager.addView(Window.java:547)
01-08 00:14:32.205: E/WindowManager(1016): at android.app.Dialog.show(Dialog.java:277)
01-08 00:14:32.205: E/WindowManager(1016): at org.Soufiane.voyagesociale.Login$AttemptLogin.onPreExecute(Login.java:109)
01-08 00:14:32.205: E/WindowManager(1016): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
01-08 00:14:32.205: E/WindowManager(1016): at android.os.AsyncTask.execute(AsyncTask.java:534)
01-08 00:14:32.205: E/WindowManager(1016): at org.Soufiane.voyagesociale.Login.onClick(Login.java:88)
01-08 00:14:32.205: E/WindowManager(1016): at android.view.View.performClick(View.java:4084)
01-08 00:14:32.205: E/WindowManager(1016): at android.view.View$PerformClick.run(View.java:16966)
01-08 00:14:32.205: E/WindowManager(1016): at android.os.Handler.handleCallback(Handler.java:615)
01-08 00:14:32.205: E/WindowManager(1016): at android.os.Handler.dispatchMessage(Handler.java:92)
01-08 00:14:32.205: E/WindowManager(1016): at android.os.Looper.loop(Looper.java:137)
01-08 00:14:32.205: E/WindowManager(1016): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-08 00:14:32.205: E/WindowManager(1016): at java.lang.reflect.Method.invokeNative(Native Method)
01-08 00:14:32.205: E/WindowManager(1016): at java.lang.reflect.Method.invoke(Method.java:511)
01-08 00:14:32.205: E/WindowManager(1016): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-08 00:14:32.205: E/WindowManager(1016): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-08 00:14:32.205: E/WindowManager(1016): at dalvik.system.NativeStart.main(Native Method)
01-08 00:16:32.855: E/ThrottleService(163): problem during onPollAlarm: java.lang.IllegalStateException: problem parsing stats: java.io.FileNotFoundException: /proc/net/xt_qtaguid/iface_stat_all: open failed: ENOENT (No such file or directory)
01-08 00:26:32.896: E/ThrottleService(163): problem during onPollAlarm: java.lang.IllegalStateException: problem parsing stats: java.io.FileNotFoundException: /proc/net/xt_qtaguid/iface_stat_all: open failed: ENOENT (No such file or directory)
答案 0 :(得分:1)
您缺少网址的协议。设置http://
https://
或LOGIN_URL
请点击此处了解详情:Target host must not be null, or set in parameters
答案 1 :(得分:1)
看一下logcat:
目标主机不能为空,或者在参数中设置。方案= NULL, host = null,path = sorifgroupcom.ipage.com / Android / login.php
它将一切都视为一条路径,没有东道主。那是因为你没有指定方案。
更改
private static final String LOGIN_URL = "sorifgroupcom.ipage.com/Android/login.php";
到
private static final String LOGIN_URL = "http://sorifgroupcom.ipage.com/Android/login.php";