HOME > Mobile > Mobile Forum > Android มีปัญหาในการคิวรี่ข้อมูลกลับมาเป็น json จาก Mysql เมื่อส่งค่าเป็น BasicNameValuePair("tid",Integer.toString(tID)) ค่ะ
Android มีปัญหาในการคิวรี่ข้อมูลกลับมาเป็น json จาก Mysql เมื่อส่งค่าเป็น BasicNameValuePair("tid",Integer.toString(tID)) ค่ะ
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);
?>