单击登录按钮“尝试登录”消息仅显示,并且应用程序中没有其他活动,并且在单击按钮后,LOGCAT中显示异常“java.net.ConnectException:无法连接到/ 15000ms后127.0.0.1(端口80):isConnected失败:ECONNREFUSED(拒绝连接)“。可能只有preexecute正在运行而不是后台任务。但问题出在哪里?
Login.java --->
Title
............................................... ................................. JsonParser.java ---->
public class Login extends Activity {
AutoCompleteTextView UsernameEt;
EditText PasswordEt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
UsernameEt = (AutoCompleteTextView) findViewById(R.id.email);
PasswordEt = (EditText) findViewById(R.id.password);
}
class PostAsync extends AsyncTask<String, String, JSONObject> {
JSONParser jsonParser = new JSONParser();
private ProgressDialog pDialog;
// private static final String LOGIN_URL = "http://10.0.3.2:8080/login3.php";
// private static final String LOGIN_URL = "http://192.168.*.*/login3.php";
private static final String LOGIN_URL = "http://127.0.0.1/login3.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
protected void onPreExecute() {
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
try {
HashMap<String, String> params = new HashMap<>();
params.put("name", args[0]);
params.put("password", args[1]);
Log.d("request", "starting");
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "POST", params);
if (json != null) {
Log.d("JSON result", json.toString());
return json;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(JSONObject json) {
int success = 0;
String message = "";
if (pDialog != null && pDialog.isShowing()) {
pDialog.dismiss();
}
if (json != null) {
Toast.makeText(Login.this, json.toString(),
Toast.LENGTH_LONG).show();
try {
success = json.getInt(TAG_SUCCESS);
message = json.getString(TAG_MESSAGE);
} catch (JSONException e) {
e.printStackTrace();
}
}
if (success == 1) {
Log.d("Success!", message);
}else{
Log.d("Failure", message);
}
}
}
public void OnLogin(View view) {
String username = UsernameEt.getText().toString();
String password = PasswordEt.getText().toString();
new PostAsync().execute(username, password);
}
}
答案 0 :(得分:-1)
使用10.0.2.2而不是127.0.0.1或localhost