|
|
|
ใช้ $_POST[] เพื่อดึงค่าจาก Server แล้วไม่ขึ้นบน android ค่ะ |
|
|
|
|
|
|
|
Code.java
Intent intent= this.getIntent();
userId = intent.getStringExtra("userId");
Toast.makeText(this, userId, Toast.LENGTH_LONG).show();
// Permission StrictMode
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
// listView1
final ListView lstView1 = (ListView)findViewById(R.id.listViewGrade);
TextView txtgrade = (TextView) findViewById(R.id.tgpa);
String url = DatabaseIP.ipServer+"/studentsu/showAllDataSubject.php";
// Paste Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("userId",userId));
try {
JSONArray data = new JSONArray(getJSONUrl(url,params));
final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);
map = new HashMap<String, String>();
map.put("idsubject", c.getString("idsubject"));
map.put("subj_Tname", c.getString("subj_Tname"));
map.put("subj_unit", c.getString("subj_unit"));
map.put("grade", c.getString("grade"));
MyArrList.add(map);
}
lstView1.setAdapter(new ImageAdapter(this, MyArrList));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class ImageAdapter extends BaseAdapter
{
private Context context;
private ArrayList<HashMap<String, String>> MyArr = new ArrayList<HashMap<String, String>>();
public ImageAdapter(Context c, ArrayList<HashMap<String, String>> list)
{
// TODO Auto-generated method stub
context = c;
MyArr = list;
}
public int getCount() {
// TODO Auto-generated method stub
return MyArr.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.grade_list, null);
}
//
TextView txtId= (TextView) convertView.findViewById(R.id.gradeIdTv);
txtId.setPadding(5, 0, 0, 0);
txtId.setText("" + MyArr.get(position).get("idsubject"));
TextView txtSub = (TextView) convertView.findViewById(R.id.gradeSubTv);
txtSub.setPadding(5, 0, 0, 0);
txtSub.setText("" + MyArr.get(position).get("subj_Tname"));
TextView txtunit = (TextView) convertView.findViewById(R.id.gradeUnitTv);
txtunit.setPadding(5, 0, 0, 0);
txtunit.setText("" + MyArr.get(position).get("subj_unit"));
TextView txtgrade = (TextView) convertView.findViewById(R.id.gradeTv);
txtgrade.setPadding(5, 0, 0, 0);
txtgrade.setText("" + MyArr.get(position).get("grade"));
return convertView;
}
}
/*** Get JSON Code from URL
* @param params ***/
public String getJSONUrl(String url, List<NameValuePair> params) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { // Download 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 file..");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
}
ไฟล์ ShowAllDataSubject.php
$userId = $_POST["userId"];
$strSQL = "SELECT * FROM `register` WHERE userId='".$userId."' ";
$objQuery = mysql_query($strSQL);
$intNumField = mysql_num_fields($objQuery);
$resultArray = array();
while($obResult = mysql_fetch_array($objQuery))
{
$arrCol = array();
for($i=0;$i<$intNumField;$i++)
{
$arrCol[mysql_field_name($objQuery,$i)] = $obResult[$i];
}
array_push($resultArray,$arrCol);
}
mysql_close($objConnect);
echo json_encode($resultArray);
ส่งค่า POST userId ให้ select userId ที่ได้ getIntent ขึ้นมา
ส่วนของ android ไม่รับค่าขึ้นมาบน listView ให้ค่ะ
ขอบคุณทุกท่าน มากๆค่ะ
Tag : Mobile, Android, JAVA, Mobile, Web Service
|
ประวัติการแก้ไข 2013-12-20 21:43:16
|
|
|
|
|
Date :
2013-12-20 21:41:15 |
By :
mook0285 |
View :
1100 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Android and PHP / MySQL
ลองอ่านดูครับ เอามาจากไหน ก็ตามแก้จากบทความนั้น ๆ ครับ
|
|
|
|
|
Date :
2013-12-23 10:23:08 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|