 |
Android นำค่าในฐานข้อมูล sql โดยผ่านโค้ด JSON มาแสดงในแอนดรอย์ไม่ได้คับ ลองทำตามหลายกระทู้แล้วก็ยังไม่ค่อยเข้าใจ ช่วยดูให้หน่อยนะคับ |
|
 |
|
|
 |
 |
|
ผมทำโปรเจ็คส่งอ.อะคับ เกี่ยวกับการเรียกแสดง เพิ่ม ลบ แก้ไข ข้อมูลใน mysql บนแอนดรอย ทำเป็นแอ๊ปคับ
ตอนนี้กำลังศึกษา เลยลองเรียกข้อมูลมาแสดงดู แต่มันไม่ได้คับ งงเรื่อง การนำโค้ด json มาใช้ ดูหลายเว็บก็ยังไม่เข้าใจ
อยากให้ลองดูโค้ดให้หน่อย เหมือนว่ามันไม่ทำใน try อะคับ เวลารีเทรินกลับมามันไม่แสดงค่าอะไรเลย รบกวนด้วยคับ
ไฟล์ PHP
Code (PHP)
include("week1/book.php");
connect_db("bookdata","root","1234");
$sql1 = "SELECT book_id, book_title, book_writer, book_sale FROM book WHERE book_id = '00010' " ;
$result1=mysql_query($sql1);
list($book_id,$book_title, $book_writer, $book_sale) = mysql_fetch_row($result1);
echo "{";
echo json_encode("book_id").":".json_encode($book_id),",";
echo json_encode("book_title").":".json_encode($book_title),",";
echo json_encode("book_writer").":".json_encode($book_writer),",";
echo json_encode("book_sale").":".json_encode($book_sale);
echo "}";
mysql_close();
ไฟล์ .xml
Code (XML)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="@+id/input_book_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/input_book_id"
android:text="Submit" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button"
android:layout_marginLeft="16dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ID : " />
<TextView
android:id="@+id/text_book_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/linearLayout1"
android:layout_below="@+id/linearLayout1"
android:layout_marginTop="25dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title : " />
<TextView
android:id="@+id/text_book_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/linearLayout2"
android:layout_below="@+id/linearLayout2"
android:layout_marginTop="22dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Writer : " />
<TextView
android:id="@+id/text_book_writer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/linearLayout3"
android:layout_centerVertical="true"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price : " />
<TextView
android:id="@+id/text_book_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</RelativeLayout>
ใส่ <uses-permission android:name="android.permission.INTERNET" /> ใน AndroidManifest.xml แล้วคับ
ไฟล์ JAVA
Code (Java)
package com.Amika.bookamika;
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.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView t_book_id;
private TextView t_book_title;
private TextView t_book_writer;
private TextView t_book_price;
private EditText input_book_id;
private Button submit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t_book_id = (TextView) findViewById(R.id.text_book_id);
t_book_title = (TextView) findViewById(R.id.text_book_title);
t_book_writer = (TextView) findViewById(R.id.text_book_writer);
t_book_price = (TextView) findViewById(R.id.text_book_price);
input_book_id = (EditText) findViewById(R.id.input_book_id);
submit = (Button) findViewById(R.id.button);
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String url = "http://10.0.2.2/dbapp/t_pro.php";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("book_id", t_book_id.getText().toString()));
String[] re = new String[4];
re = getHttpPost(url,params);
t_book_id.setText(re[0]);
t_book_title.setText(re[1]);
t_book_writer.setText(re[2]);
t_book_price.setText(re[3]);
}
});
}
public String[] getHttpPost(String url, List<NameValuePair> params) {
String[] re = new String[4];
InputStream is = null;
String result = "";
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());
}
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());
}
try {
JSONArray jArray = new JSONArray(result);
JSONObject json_data = jArray.getJSONObject(0);
re[0] = json_data.getString("book_id");
re[1] = json_data.getString("book_title");
re[2] = json_data.getString("book_writer");
re[3] = json_data.getString("book_price");
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return re;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Tag : Mobile, MySQL, JavaScript, Android, JAVA
|
|
 |
 |
 |
 |
Date :
2012-10-27 15:10:54 |
By :
laserman |
View :
1532 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
|