我是android新手。我正在构建一个登录活动。一切都很好但在我提交正确的用户名和密码后,该应用程序不会转到下一页。
运行应用程序后,这些是LOGCAT:
11-12 15:20:37.223 1585-1585/com.infinidea.loginapplication D/dalvikvm﹕ GC_FOR_ALLOC freed 137K, 7% free 3002K/3216K, paused 40ms, total 50ms
11-12 15:20:37.373 1585-1598/com.infinidea.loginapplication D/request!﹕ starting
11-12 15:20:37.513 1585-1585/com.infinidea.loginapplication I/Choreographer﹕ Skipped 70 frames! The application may be doing too much work on its main thread.
11-12 15:20:37.643 1585-1585/com.infinidea.loginapplication I/Choreographer﹕ Skipped 70 frames! The application may be doing too much work on its main thread.
11-12 15:20:37.953 1585-1585/com.infinidea.loginapplication I/Choreographer﹕ Skipped 132 frames! The application may be doing too much work on its main thread.
11-12 15:20:38.073 1585-1585/com.infinidea.loginapplication I/Choreographer﹕ Skipped 61 frames! The application may be doing too much work on its main thread.
11-12 15:20:38.363 1585-1585/com.infinidea.loginapplication I/Choreographer﹕ Skipped 50 frames! The application may be doing too much work on its main thread.
11-12 15:20:38.493 1585-1585/com.infinidea.loginapplication I/Choreographer﹕ Skipped 33 frames! The application may be doing too much work on its main thread.
11-12 15:20:38.743 1585-1585/com.infinidea.loginapplication I/Choreographer﹕ Skipped 31 frames! The application may be doing too much work on its main thread.
11-12 15:20:38.873 1585-1585/com.infinidea.loginapplication I/Choreographer﹕ Skipped 52 frames! The application may be doing too much work on its main thread.
11-12 15:20:38.933 1585-1585/com.infinidea.loginapplication I/Choreographer﹕ Skipped 32 frames! The application may be doing too much work on its main thread.
11-12 15:20:39.243 1585-1585/com.infinidea.loginapplication I/Choreographer﹕ Skipped 37 frames! The application may be doing too much work on its main thread.
11-12 15:20:39.433 1585-1585/com.infinidea.loginapplication I/Choreographer﹕ Skipped 40 frames! The application may be doing too much work on its main thread.
11-12 15:20:39.843 1585-1585/com.infinidea.loginapplication I/Choreographer﹕ Skipped 42 frames! The application may be doing too much work on its main thread.
11-12 15:20:40.593 1585-1585/com.infinidea.loginapplication I/Choreographer﹕ Skipped 44 frames! The application may be doing too much work on its main thread.
11-12 15:20:40.743 1585-1585/com.infinidea.loginapplication I/Choreographer﹕ Skipped 42 frames! The application may be doing too much work on its main thread.
11-12 15:20:41.663 1585-1585/com.infinidea.loginapplication I/Choreographer﹕ Skipped 38 frames! The application may be doing too much work on its main thread.
11-12 15:20:42.493 1585-1585/com.infinidea.loginapplication I/Choreographer﹕ Skipped 42 frames! The application may be doing too much work on its main thread.
11-12 15:20:42.813 1585-1585/com.infinidea.loginapplication I/Choreographer﹕ Skipped 39 frames! The application may be doing too much work on its main thread.
11-12 15:20:42.923 1585-1585/com.infinidea.loginapplication I/Choreographer﹕ Skipped 32 frames! The application may be doing too much work on its main thread.
11-12 15:20:43.233 1585-1598/com.infinidea.loginapplication D/dalvikvm﹕ GC_FOR_ALLOC freed 126K, 6% free 3390K/3588K, paused 80ms, total 80ms
11-12 15:20:43.243 1585-1585/com.infinidea.loginapplication I/Choreographer﹕ Skipped 53 frames! The application may be doing too much work on its main thread.
11-12 15:20:43.693 1585-1585/com.infinidea.loginapplication I/Choreographer﹕ Skipped 33 frames! The application may be doing too much work on its main thread.
11-12 15:20:44.263 1585-1585/com.infinidea.loginapplication I/Choreographer﹕ Skipped 58 frames! The application may be doing too much work on its main thread.
11-12 15:20:44.563 1585-1585/com.infinidea.loginapplication I/Choreographer﹕ Skipped 45 frames! The application may be doing too much work on its main thread.
11-12 15:20:44.803 1585-1598/com.infinidea.loginapplication W/System.err﹕ org.apache.http.conn.HttpHostConnectException: Connection to http://localhost:8090 refused
11-12 15:20:44.803 1585-1598/com.infinidea.loginapplication W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
11-12 15:20:44.803 1585-1598/com.infinidea.loginapplication W/System.err﹕ at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
11-12 15:20:44.813 1585-1598/com.infinidea.loginapplication W/System.err﹕ at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11-12 15:20:44.813 1585-1598/com.infinidea.loginapplication W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
11-12 15:20:44.813 1585-1598/com.infinidea.loginapplication W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-12 15:20:44.813 1585-1598/com.infinidea.loginapplication W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-12 15:20:44.863 1585-1585/com.infinidea.loginapplication I/Choreographer﹕ Skipped 49 frames! The application may be doing too much work on its main thread.
11-12 15:20:45.173 1585-1598/com.infinidea.loginapplication W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
11-12 15:20:45.173 1585-1598/com.infinidea.loginapplication W/System.err﹕ at com.infinidea.loginapplication.JSONParser.makeHttpRequest(JSONParser.java:106)
11-12 15:20:45.183 1585-1598/com.infinidea.loginapplication W/System.err﹕ at com.infinidea.loginapplication.MainActivity$AttemptLogin.doInBackground(MainActivity.java:84)
11-12 15:20:45.183 1585-1598/com.infinidea.loginapplication W/System.err﹕ at com.infinidea.loginapplication.MainActivity$AttemptLogin.doInBackground(MainActivity.java:53)
11-12 15:20:45.183 1585-1598/com.infinidea.loginapplication W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
11-12 15:20:45.223 1585-1598/com.infinidea.loginapplication W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
11-12 15:20:45.223 1585-1598/com.infinidea.loginapplication W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
11-12 15:20:45.233 1585-1598/com.infinidea.loginapplication W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-12 15:20:45.233 1585-1598/com.infinidea.loginapplication W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-12 15:20:45.233 1585-1598/com.infinidea.loginapplication W/System.err﹕ at java.lang.Thread.run(Thread.java:841)
11-12 15:20:45.233 1585-1598/com.infinidea.loginapplication W/System.err﹕ Caused by: java.net.ConnectException: failed to connect to /127.0.0.1 (port 8090): connect failed: ECONNREFUSED (Connection refused)
11-12 15:20:45.243 1585-1598/com.infinidea.loginapplication W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:114)
11-12 15:20:45.243 1585-1598/com.infinidea.loginapplication W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
11-12 15:20:45.243 1585-1598/com.infinidea.loginapplication W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
11-12 15:20:45.243 1585-1598/com.infinidea.loginapplication W/System.err﹕ at java.net.Socket.connect(Socket.java:843)
11-12 15:20:45.243 1585-1598/com.infinidea.loginapplication W/System.err﹕ at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
11-12 15:20:45.243 1585-1598/com.infinidea.loginapplication W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
11-12 15:20:45.243 1585-1598/com.infinidea.loginapplication W/System.err﹕ ... 15 more
11-12 15:20:45.243 1585-1598/com.infinidea.loginapplication W/System.err﹕ Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
11-12 15:20:45.293 1585-1585/com.infinidea.loginapplication I/Choreographer﹕ Skipped 75 frames! The application may be doing too much work on its main thread.
11-12 15:20:45.463 1585-1598/com.infinidea.loginapplication W/System.err﹕ at libcore.io.Posix.connect(Native Method)
11-12 15:20:45.533 1585-1598/com.infinidea.loginapplication W/System.err﹕ at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
11-12 15:20:45.563 1585-1598/com.infinidea.loginapplication W/System.err﹕ at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
11-12 15:20:45.623 1585-1598/com.infinidea.loginapplication W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:112)
11-12 15:20:45.623 1585-1598/com.infinidea.loginapplication W/System.err﹕ ... 20 more
11-12 15:20:45.823 1585-1585/com.infinidea.loginapplication I/Choreographer﹕ Skipped 68 frames! The application may be doing too much work on its main thread.
11-12 15:20:45.913 1585-1585/com.infinidea.loginapplication I/Choreographer﹕ Skipped 48 frames! The application may be doing too much work on its main thread.
这是源代码。
public class MainActivity extends Activity implements OnClickListener{
private EditText user, pass;
private Button bLogin;
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
private static final String LOGIN_URL = "http://localhost/xampp/android_api/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.username);
pass = (EditText)findViewById(R.id.password);
bLogin = (Button)findViewById(R.id.login);
bLogin.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.login:
new AttemptLogin().execute();
// here we have used, switch case, because on login activity you may //also want to show registration button, so if the user is new ! we can go the //registration activity , other than this we could also do this without switch //case.
default:
break;
}
}
class AttemptLogin extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
boolean failure = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Attempting for login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// here Check for success tag
int success;
String username = user.getText().toString();
String password = pass.getText().toString();
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Log.d("request!", "starting");
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "POST", params);
if(json!=null){
// checking log for json response
Log.d("Login attempt", json.toString());
// success tag for json
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Successfully Login!", json.toString());
Intent ii = new Intent(MainActivity.this,Otheractivity.class);
finish();
// this finish() method is used to tell android os that we are done with current //activity now! Moving to other activity
startActivity(ii);
return json.getString(TAG_MESSAGE);
}else{
return json.getString(TAG_MESSAGE);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* Once the background process is done we need to Dismiss the progress dialog asap
* **/
protected void onPostExecute(String message) {
pDialog.dismiss();
if (message != null){
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
}
}
}
}
答案 0 :(得分:3)
W/System.err﹕ org.apache.http.conn.HttpHostConnectException: Connection to http://localhost:8090 refused
您无法在Android模拟器中使用localhost://
访问PC上的内容,您需要使用端口转发的IP地址10.0.2.2
而不是