|
|
|
[AndroidStudio]ทำไมหลังจากที่ Login เข้าไปที่หน้า main แล้ว พอจะกดจาก หน้า main ไปหน้าอื่นมัน Error อะครับ |
|
|
|
|
|
|
|
หลังจากที่ Login แล้ว พอไปถึงหน้า Main ต้องการที่จะกดไปหน้าอื่น App เด้งออกเลยอะครับ
โค๊ด Login
Code (Android-Java)
package com.example.namwanta.demover7;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
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.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Login extends AppCompatActivity {
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// Permission StrictMode
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
final AlertDialog.Builder ad = new AlertDialog.Builder(this);
// txtUsername & txtPassword
final EditText txtUser = (EditText) findViewById(R.id.etEmail);
final EditText txtPass = (EditText) findViewById(R.id.etPassword);
// btnLogin
final Button btnLogin = (Button) findViewById(R.id.btLogin);
final TextView tvregister = (TextView) findViewById(R.id.tvregister);
// Action on click Login
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String url = ("http://202.28.94.32/ict7/php/checkLogin.php") +
"";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("strUser", txtUser.getText().toString()));
params.add(new BasicNameValuePair("strPass", txtPass.getText().toString()));
/** Get result from Server (Return the JSON Code)
* StatusID = ? [0=Failed,1=Complete]
* MemberID = ? [Eg : 1]
* Error = ? [On case error return custom error message]
*
* Eg Login Failed = {"StatusID":"0","MemberID":"0","Error":"Incorrect Username and Password"}
* Eg Login Complete = {"StatusID":"1","MemberID":"2","Error":""}
*/
String resultServer = getHttpPost(url, params);
/*** Default Value ***/
String strStatusID = "0";
String strMemberID = "0";
String strError = "Unknow Status!";
JSONObject c;
try {
c = new JSONObject(resultServer);
strStatusID = c.getString("StatusID");
strMemberID = c.getString("MemberID");
strError = c.getString("Error");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Prepare Login
if (strStatusID.equals("0")) {
// Dialog
ad.setTitle("Error! ");
ad.setIcon(android.R.drawable.btn_star_big_on);
ad.setPositiveButton("Close", null);
ad.setMessage(strError);
ad.show();
txtUser.setText("");
txtPass.setText("");
} else {
Toast.makeText(Login.this, "Login OK", Toast.LENGTH_SHORT).show();
Intent newActivity = new Intent(Login.this, MainActivity.class);
newActivity.putExtra("MemberID", strMemberID);
startActivity(newActivity);
}
}
});
// Action on click Register
tvregister.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent newActivity = new Intent(Login.this, Register.class);
startActivity(newActivity);
}
});
// Action on click Forgot Password
}
public String getHttpPost(String url, List<NameValuePair> params) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { // Status OK
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
str.append(line);
}
} else {
Log.e("Log", "Failed to download result..");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
}
เป็นโค๊ดตามตัวอย่างที่มีให้ดูในนี้อะครับ
Tag : Mobile, MySQL, Android
|
|
|
|
|
|
Date :
2015-12-01 06:55:06 |
By :
nomzod |
View :
1874 |
Reply :
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ประกาศ Class ใน AndroidManifest.xml ด้วยครับ
AndroidManifest.xml
<activity
android:name="Register"
android:theme="@style/AppTheme"
android:screenOrientation="portrait"
android:label="@string/title_activity_main" />
Android กับ Intent ควบคุมการแสดงและซ่อน (Show/Hide) ของ Activity form ต่าง ๆ
|
|
|
|
|
Date :
2015-12-01 10:24:13 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ประกาศแล้วครับ เหมือนว่า มันมีค่าอะไรสักอย่างที่ส่งไปด้วยรึเปล่า เพราะถ้ารันโดนไม่ล็อคอินก่อน ไม่มีปัญหาอะครับ
|
|
|
|
|
Date :
2015-12-01 12:30:54 |
By :
nomzod |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Date :
2015-12-01 12:35:17 |
By :
nomzod |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เหมือนบางที เปิดหลายๆหน้าแล้วมันเด้ง อันนี้เกี่ยวมั้ยครับ
|
|
|
|
|
Date :
2015-12-01 12:58:09 |
By :
nomzod |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|