 |
Android มีปัญหาในการคิวรี่ข้อมูลกลับมาเป็น json จาก Mysql เมื่อส่งค่าเป็น BasicNameValuePair("tid",Integer.toString(tID)) ค่ะ |
|
 |
|
|
 |
 |
|
โค๊ดจาวาค่ะ
Code (Android-Java)
package app.classattendance;
import java.io.BufferedReader;
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.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.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class TeacherHomeActivity extends Activity {
TextView txtsemester;
TextView txtnameUser;
String semester = "0";
//private static final String name = "name";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_teacher_home);
final Button btnLogout = (Button) findViewById(R.id.btn_logout);
final Button btnHome = (Button) findViewById(R.id.btn_home);
btnLogout.setOnClickListener(new view.OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent(TeacherHomeActivity.this,LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
});
btnHome.setOnClickListener(new view.OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent(TeacherHomeActivity.this,TeacherHomeActivity.class);
startActivity(intent);
}
});
txtnameUser = (TextView)findViewById(R.id.name);
txtsemester = (TextView)findViewById(R.id.semester);
String url = "http://10.0.2.2/phpFile/teacherMenu.php";
Bundle extras = getIntent().getExtras();
int tID = extras.getInt("userID");
Intent intent= getIntent();
String username = intent.getStringExtra("username");
txtnameUser.setText(username);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tid",Integer.toString(tID)));
String resultServer = getHttpPost(url,params);
try {
JSONObject c= new JSONObject(resultServer);
semester = c.getString("name");
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
txtsemester.setText(semester);
}
public String getHttpPost(String url, List<NameValuePair> params) {
String result = "";
InputStream is = null;
//ส่วนของการเชื่อมต่อกับ http เพื่อดึงข้อมูล
try {
HttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
HttpResponse response = client.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
//ส่วนของการแปลงผลลัพธ์ให้อยู่ในรูปแบบของ String
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-11"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
return result;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_teacher_home, menu);
return true;
}
}
ส่วนคิวรี่ข้อมูลจาก database ค่ะ
Code (SQL)
<?php
$hostname_localhost ="localhost";
$database_localhost ="classattendance";
$username_localhost ="****";
$password_localhost ="****";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost) or
trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($database_localhost, $localhost);
$tid = "9";
$query_semster = "SELECT s.name as name
FROM tbl_user u
JOIN tbl_teacher t ON u.id = t.userId
JOIN tbl_courseinfo cinfo ON cinfo.teacherId = t.id
JOIN tbl_course c ON cinfo.courseId = c.id
JOIN tbl_semester s ON c.semesterId = s.id
WHERE s.active = 1 AND u.id = '".$tid."' GROUP BY s.name ";
$exec_semster = mysql_query($query_semster) or die(mysql_error());
if($result =mysql_fetch_assoc($exec_semster)){
$arr['name'] = $result["name"];
}
echo json_encode($arr);
mysql_close($localhost);
?>
มันไม่โชว์ค่าของ txtsemester.setText(semester) เป็น 0 เสมอเลยค่ะ
ซึ่งเป็นค่าที่ประกาศตัวแปรไว้ String semester = "0"
ไม่แน่ใจว่าเป็นเพราะการส่งค่า BasicNameValuePair("tid",Integer.toString(tID)) ส่วนนี้หรือป่าวคะ
หรือการคิวรี่ข้อมูลผิดกลับมา แต่เช็กแล้วไม่ผิด (ค่าที่คืวรี่มาเป็นภาษาไทย)
เหมือนว่านำข้อมูลเข้าไปทำฟังก์ชั่นนี้ public String getHttpPost(String url, List<NameValuePair> params)
ไม่มีค่ากลับมาเลยค่ะ
รบกวนด้วยนะคะ หาทางแก้หลายทางแล้วทำไม่ได้ซักที
Tag : Mobile, MySQL, Android, JAVA
|
|
 |
 |
 |
 |
Date :
2013-02-07 11:20:06 |
By :
pooh |
View :
1519 |
Reply :
6 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่ได้ค่าอะไรเลยค่ะ T-T

แต่ถ้าดูโดย php จะได้ค่าตามนี้ค่ะ
{"name":"\u0e20\u0e32\u0e04\u0e40\u0e23\u0e35\u0e22\u0e19\u0e17\u0e35\u0e48 1 \u0e1b\u0e35\u0e01\u0e32\u0e23\u0e28\u0e36\u0e01\u0e29\u0e32 2555"}
|
 |
 |
 |
 |
Date :
2013-02-08 14:52:30 |
By :
pooh |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ได้แล้วค่ะ เพราะมันติด error ในการเชื่อมต่อ
พอดีไปอ่านเว็บนอกเขาบอกทำนองว่าเกี่ยวกับการเรียกใช้มันซ้ำซ้อนหรือไงนี้ละคะ (อ่านแล้วงงอยู่)
เอาเป็นว่าเขาให้เรียกใช้โดยเป็น Thread แทนค่ะ เลยได้
Code (Android-Java)
new Thread(new Runnable() {
public void run() {
mainTeacher();
}
}).start();
|
 |
 |
 |
 |
Date :
2013-02-08 22:40:05 |
By :
pooh |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ในตัวอย่างผมก็ทำได้ปกติดีน่ะ แต่ปกติแล้วควรจะใช้ Thread จริง ๆ ครับ พวกนี้
|
 |
 |
 |
 |
Date :
2013-02-08 22:46:28 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ค่ะ ทำตามตัวอย่างคุณวิน มันรันไม่ได้เลยค่ะ
คิดว่าเป็นที่โปรแกรมที่ลงอะคะ เอาตัวที่เขา set ค่ามาให้หมด
ก๊อปปี้ลงเลย (แต่ไม่รู้ว่าเกี่ยวป่าว)
ขอบคุณคุณวินนะคะที่แนะให้ทำ Debug ให้ดูปกติ ไม่ค่อยสนใจส่วนนั้นเลยค่ะ
เวลาเกิด error เลยไม่ทราบเลย แหะๆ :)
|
 |
 |
 |
 |
Date :
2013-02-08 23:28:59 |
By :
Pooh |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|