|
|
|
ระบบ Login Error 'Unknown Status' บนมือถือ Android สามารถแก้ไขได้ยังไงบ้างค่ัะ |
|
|
|
|
|
|
|
กำลังทำ ระบบ login เมื่อคีย์้ข้อมูล ปรากฏ Pop up 'Unknown Status'
Code ของ Android
Check login
Code (Android-Java)
package com.example.login;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import android.app.AlertDialog;
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 android.os.Bundle;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 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.txtname);
final EditText txtPass = (EditText)findViewById(R.id.txtpassword);
// btnLogin
final Button btnLogin = (Button) findViewById(R.id.btnLogin);
// Perform action on click
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String url = "http://10.0.2.2/appandroid/login.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("strUser", txtUser.getText().toString()));
params.add(new BasicNameValuePair("strPass", txtPass.getText().toString()));
String resultServer = getHttpPost(url,params);
String strStatusID = "0";
String strmember_id = "0";
String strError = "Unknow Status!";
JSONObject c;
try {
c = new JSONObject(resultServer);
strStatusID = c.getString("StatusID");
strmember_id = c.getString("member_id");
strError = c.getString("Error");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
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(MainActivity.this, "Login OK", Toast.LENGTH_SHORT).show();
Intent newActivity = new Intent(MainActivity.this,DetailActivity.class);
newActivity.putExtra("MemberID", strmember_id);
startActivity(newActivity);
}
}
});
}
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();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
getmember
Code (Android-Java)
package com.example.login;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
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 android.os.Bundle;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;
public class DetailActivity extends Activity {
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
// Permission StrictMode
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
showInfo();
// btnBack
final Button btnBack = (Button) findViewById(R.id.btnBack);
// Perform action on click
btnBack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent newActivity = new Intent(DetailActivity.this,MainActivity.class);
startActivity(newActivity);
}
});
}
public void showInfo()
{
final TextView tmember_id = (TextView)findViewById(R.id.txtmember_id);
final TextView tname = (TextView)findViewById(R.id.txtname);
final TextView tpassword = (TextView)findViewById(R.id.txtpassword);
String url = "http://10.0.2.2/appandroid/getmember.php";
Intent intent= getIntent();
final String member_id = intent.getStringExtra("member_id");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("smember_id", member_id));
String resultServer = getHttpPost(url,params);
String strmember_id = "";
String strname = "";
String strpassword = "";
JSONObject c;
try {
c = new JSONObject(resultServer);
strmember_id = c.getString("member_id");
strname = c.getString("name");
strpassword = c.getString("password");
if(!strmember_id.equals(""))
{
tmember_id.setText(strmember_id);
tname.setText(strname);
tpassword.setText(strpassword);
}
else
{
tmember_id.setText("-");
tname.setText("-");
tpassword.setText("-");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
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();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Tag : Mobile
|
|
|
|
|
|
Date :
2013-03-23 20:47:34 |
By :
Monkey_CONAN |
View :
2251 |
Reply :
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code ฝั่ง php น่ะค่ะ
login
Code (PHP)
<?
$objConnect = mysql_connect("localhost","root","root");
$objDB = mysql_select_db("newsextraction");
$strUsername = $_POST["strUser"];
$strPassword = $_POST["strPass"];
$strSQL = "SELECT * FROM member WHERE 1
AND Username = '".$strname."'
AND Password = '".$strpassword."'
";
$objQuery = mysql_query($strSQL);
$objResult = mysql_fetch_array($objQuery);
$intNumRows = mysql_num_rows($objQuery);
if($intNumRows==0)
{
$arr['StatusID'] = "0";
$arr['member_id'] = "0";
$arr['Error'] = "Incorrect Username and Password";
}
else
{
$arr['StatusID'] = "1";
$arr['member_id'] = $objResult["member_id"];
$arr['Error'] = "";
}
mysql_close($objConnect);
echo json_encode($arr);
?>
getmember
Code (PHP)
<?
$objConnect = mysql_connect("localhost","root","root");
$objDB = mysql_select_db("newsextraction);
$strmember_id = $_POST["smember_id"];
$strSQL = "SELECT * FROM member WHERE 1 AND member_id = '".$strmember_id."' ";
$objQuery = mysql_query($strSQL);
$obResult = mysql_fetch_array($objQuery);
if($obResult)
{
$arr["member_id"] = $obResult["member_id"];
$arr["name"] = $obResult["name"];
$arr["pssword"] = $obResult["password"];
}
mysql_close($objConnect);
echo json_encode($arr);
?>
รบกวนด้วยนะค่ะ
|
|
|
|
|
Date :
2013-03-23 20:48:35 |
By :
Monkey_CONAN |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
เจอปัญหาเดียวกันเลยค๊าบ ทำได้ยังอะครับ รบกวนบอกกันบ้างนะ ^^
|
|
|
|
|
Date :
2013-03-31 16:55:42 |
By :
eak302 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลองแล้ว ไม่หายเลยอะครบ พอมีวิธีแนะนำบ้างป่าวครับ
|
|
|
|
|
Date :
2013-04-01 23:35:23 |
By :
eak302 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
คงจะต้องใช้การ Debug ไล่ดูค่าแล้วครับ
|
|
|
|
|
Date :
2013-04-02 06:14:32 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ทำไม่ได้อะครับ Debug แล้วเหมือนมันจะไม่เข้า
Code (Android-Java)
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();
|
|
|
|
|
Date :
2014-01-22 15:53:49 |
By :
งงมาก |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ไส่ไอพีเครื่องคุณ
String url = "http://ไอพีเครื่องคุณ/appandroid/login.php";
|
|
|
|
|
Date :
2014-01-23 01:52:43 |
By :
khuttiyamanu |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
http://10.0.2.2/mu/checkLogin.php อันนี้ของผมอะครับ ใช้แล้วก็ยังไม่ได้
|
|
|
|
|
Date :
2014-01-23 16:18:02 |
By :
งงมากๆเบยย |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ใส่ <uses-permission android:name="android.permission.INTERNET" /> ลงใน AndroidManifest.xml แล้วยังครับ
|
|
|
|
|
Date :
2014-01-30 15:17:47 |
By :
mmc01 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ใส่ไปแล้วครับ ก็ยังขึ้นอยู่เลย
|
|
|
|
|
Date :
2014-02-05 08:16:02 |
By :
งงอยู่ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ใน Log cat มันขึ้น org.json.JSONException: Value of type java.lang.String cannot be converted to JSONArray อะครับ
|
|
|
|
|
Date :
2014-02-05 20:39:46 |
By :
งงต่อไป |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|