android ช่วยด้วยค่ะ ต้องการแสดงรายการสินค้าจากฐานข้อมูล MySQL มาแสดงเป็น ListView ในหน้าโค้ดไม่แจ้งเออเร่อ แต่ว่าพอรันแล้วมันบอกให้ปิดโปรแกรม เข้าไปหน้านั้นไม่ได้เลย
Code (PHP)
session_start();
include("connect_db.php");
connect_db();
$result = mysql_query("SELECT p_id, p_name, p_type, p_price1, p_price2, p_price3, p_sale FROM product ")or die(mysql_error());
$row = mysql_num_rows($result);
echo "[ ";
$i =1;
while(list($p_id,$p_name,$p_type,$p_price1,$p_price2, $p_price3, $p_sale) = mysql_fetch_row($result)){
echo " { ";
echo json_encode("p_id").":";
echo json_encode($p_id);
echo " , ";
echo json_encode("p_name").":";
echo json_encode($p_name);
echo " , ";
echo json_encode("p_type").":";
echo json_encode($p_type);
echo " , ";
echo json_encode("p_price1").":";
echo json_encode($p_price1);
echo " , ";
echo json_encode("p_price2").":";
echo json_encode($p_price2);
echo " , ";
echo json_encode("p_price3").":";
echo json_encode($p_price3);
echo " , ";
echo json_encode("p_sale").":";
echo json_encode($p_sale);
echo "} ";
if($i < $row){
echo " , ";
}
$i++;
}
echo " ] ";
mysql_close();
Code (Java)
package com.BT.buytrash;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
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.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
public class price extends Activity{
private EditText search;
private ImageButton type_iron;
private ImageButton type_plastic;
private ImageButton type_paper;
private ImageButton type_kew;
private ImageButton type_loha;
private ImageButton type_fifa;
private ImageButton type_other;
private ListView listview_product;
private ArrayList<String> data_name;
String url = "http://10.0.2.2/BuyTrash/price_product.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
//String[][] data_listview;
//----------------------------------------------------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.price);
search = (EditText) findViewById(R.id.EditText_search);
type_iron = (ImageButton) findViewById(R.id.Button_type_iron);
type_plastic = (ImageButton) findViewById(R.id.Button_type_plastic);
type_paper = (ImageButton) findViewById(R.id.Button_type_paper);
type_kew = (ImageButton) findViewById(R.id.Button_type_kew);
type_loha = (ImageButton) findViewById(R.id.Button_type_loha);
type_fifa = (ImageButton) findViewById(R.id.Button_type_fifa);
type_other = (ImageButton) findViewById(R.id.Button_type_other);
listview_product = (ListView) findViewById(R.id.listview_price);
params.add(new BasicNameValuePair("keyword", search.getText().toString()));
ArrayList<HashMap<String, String>> re = new ArrayList<HashMap<String, String>>(); ;
re = getHttpPost(url,params);
//data_listview = new String[re.size()][7];
// Adapter
listview_product.setAdapter(new MyAdapter());
// ListView
//data_listview = new ArrayList<String>();
data_name = new ArrayList<String>();
for(int i=0 ; i< re.size() ; i++){
data_name.add(re.get(i).get("p_name").toString());
/* data_listview[i][0] = re.get(i).get("p_id").toString();
data_listview[i][1] = re.get(i).get("p_name").toString();
data_listview[i][2] = re.get(i).get("p_type").toString();
data_listview[i][3] = re.get(i).get("p_price1").toString();
data_listview[i][4] = re.get(i).get("p_price2").toString();
data_listview[i][5] = re.get(i).get("p_price3").toString();
data_listview[i][6] = re.get(i).get("p_sale").toString();
*/
}
}
//-------------------------------------------------------------------------------------
private class MyAdapter extends BaseAdapter{
private Holder holder;
public int getCount() {
return data_name.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View view, ViewGroup parent) {
if( view == null){
view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.list_price_item, null);
holder = new Holder();
holder.p_name = (TextView) view.findViewById(R.id.product_name);
holder.p_price1 = (TextView) view.findViewById(R.id.price1);
holder.p_price2 = (TextView) view.findViewById(R.id.price2);
holder.p_price3 = (TextView) view.findViewById(R.id.price3);
holder.p_sale = (TextView) view.findViewById(R.id.price_sale);
view.setTag(holder);
}else{
holder = (Holder) view.getTag();
}
holder.p_name.setText(data_name.get(position));
//holder.p_price1.setText(data_listview[position][3]);
//holder.p_price2.setText(data_listview[position][4]);
//holder.p_price3.setText(data_listview[position][5]);
//holder.p_sale.setText(data_listview[position][6]);
return view;
}
private class Holder{ //จะบอกว่า view แต่ละรายการมีอะไรบ้าง
public TextView p_name;
public TextView p_price1;
public TextView p_price2;
public TextView p_price3;
public TextView p_sale;
}
}
//----------------------------------------------------------------------------------------
public ArrayList<HashMap<String, String>> getHttpPost(String url, List<NameValuePair> params) {
ArrayList<HashMap<String, String>> myArrList = new ArrayList<HashMap<String, String>>();
//String[][] re ;
InputStream is = null;
String result = "";
// -------------------------------connect----------------------------------------------------
try {
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
// -------------------------------set UTF8 --------------------------------------------------
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());
}
// -------------------------------to 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());
}
// -------------------------------JSON to JAVA --------------------------------------------------
try {
JSONArray jArray = new JSONArray(result);
//re = new String[jArray.length()][7];
HashMap<String, String> map;
for (int i = 0; i < jArray.length(); i++) {
/* JSONObject json_data = jArray.getJSONObject(i);
re[i][0] = json_data.getString("p_id");
re[i][1] = json_data.getString("p_name");
re[i][2] = json_data.getString("p_type");
re[i][3] = json_data.getString("p_price1");
re[i][4] = json_data.getString("p_price2");
re[i][5] = json_data.getString("p_price3");
re[i][6] = json_data.getString("p_sale");
*/
//----------------------------------------------------------
JSONObject json_data = jArray.getJSONObject(i);
map = new HashMap<String, String>();
map.put("p_id",json_data.getString("p_id") );
map.put("p_name", json_data.getString("p_name") );
map.put("p_type", json_data.getString("p_type") );
map.put("p_price1", json_data.getString("p_price1") );
map.put("p_price2", json_data.getString("p_price2") );
map.put("p_price3", json_data.getString("p_price3") );
map.put("p_sale", json_data.getString("p_sale") );
myArrList.add(map);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return myArrList;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
}
Tag : Mobile, MySQL, Android, JAVA
Date :
2012-11-10 00:05:09
By :
amiamika
View :
1320
Reply :
4
ในหน้าโค้ดมันไม่แจ้งเออเร่ออะคะ
แต่ว่าพอเข้าไปหน้านี้เพื่อแสดง listview มันบังคับให้ปิดโปรแกรมเลยค่ะ
Date :
2012-11-18 22:04:54
By :
amiamika
ต้องดูที่ LogCat ครับ
Date :
2012-11-19 05:45:36
By :
mr.win
Logcat ค่ะ ช่วยด้วยนะคะ ไม่รุ้จะไปแก้ตรงไหนอะคะ
11-19 13:56:02.592: W/KeyCharacterMap(306): No keyboard for id 0
11-19 13:56:02.592: W/KeyCharacterMap(306): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
11-19 13:56:12.921: D/dalvikvm(306): GC_FOR_MALLOC freed 4075 objects / 183976 bytes in 109ms
11-19 13:56:16.912: D/dalvikvm(306): GC_FOR_MALLOC freed 2590 objects / 161048 bytes in 95ms
11-19 13:56:17.863: D/dalvikvm(306): GC_FOR_MALLOC freed 3995 objects / 620328 bytes in 74ms
11-19 13:56:18.093: D/AndroidRuntime(306): Shutting down VM
11-19 13:56:18.093: W/dalvikvm(306): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-19 13:56:18.112: E/AndroidRuntime(306): FATAL EXCEPTION: main
11-19 13:56:18.112: E/AndroidRuntime(306): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.BT.buytrash/com.BT.buytrash.price}: java.lang.NullPointerException
11-19 13:56:18.112: E/AndroidRuntime(306): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-19 13:56:18.112: E/AndroidRuntime(306): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-19 13:56:18.112: E/AndroidRuntime(306): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-19 13:56:18.112: E/AndroidRuntime(306): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-19 13:56:18.112: E/AndroidRuntime(306): at android.os.Handler.dispatchMessage(Handler.java:99)
11-19 13:56:18.112: E/AndroidRuntime(306): at android.os.Looper.loop(Looper.java:123)
11-19 13:56:18.112: E/AndroidRuntime(306): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-19 13:56:18.112: E/AndroidRuntime(306): at java.lang.reflect.Method.invokeNative(Native Method)
11-19 13:56:18.112: E/AndroidRuntime(306): at java.lang.reflect.Method.invoke(Method.java:521)
11-19 13:56:18.112: E/AndroidRuntime(306): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-19 13:56:18.112: E/AndroidRuntime(306): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-19 13:56:18.112: E/AndroidRuntime(306): at dalvik.system.NativeStart.main(Native Method)
11-19 13:56:18.112: E/AndroidRuntime(306): Caused by: java.lang.NullPointerException
11-19 13:56:18.112: E/AndroidRuntime(306): at com.BT.buytrash.price$MyAdapter.getCount(price.java:115)
11-19 13:56:18.112: E/AndroidRuntime(306): at android.widget.ListView.setAdapter(ListView.java:436)
11-19 13:56:18.112: E/AndroidRuntime(306): at com.BT.buytrash.price.onCreate(price.java:84)
11-19 13:56:18.112: E/AndroidRuntime(306): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-19 13:56:18.112: E/AndroidRuntime(306): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-19 13:56:18.112: E/AndroidRuntime(306): ... 11 more
11-19 13:56:21.432: I/Process(306): Sending signal. PID: 306 SIG: 9
Code
Date :
2012-11-19 13:57:49
By :
amiamika
Load balance : Server 03