Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Mobile > Mobile Forum > Android นำค่าในฐานข้อมูล sql โดยผ่านโค้ด JSON มาแสดงในแอนดรอย์ไม่ได้คับ ลองทำตามหลายกระทู้แล้วก็ยังไม่ค่อยเข้าใจ ช่วยดูให้หน่อยนะคับ



 

Android นำค่าในฐานข้อมูล sql โดยผ่านโค้ด JSON มาแสดงในแอนดรอย์ไม่ได้คับ ลองทำตามหลายกระทู้แล้วก็ยังไม่ค่อยเข้าใจ ช่วยดูให้หน่อยนะคับ

 



Topic : 085960

Guest




ผมทำโปรเจ็คส่งอ.อะคับ เกี่ยวกับการเรียกแสดง เพิ่ม ลบ แก้ไข ข้อมูลใน 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







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2012-10-27 15:10:54 By : laserman View : 1449 Reply : 1
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

ลองดูนี่เลยครับ ผมทำให้รันผ่านแล้ว



Android JSON Retrieving Data from URL Web Server (PHP MySQL and JSON)






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2012-10-27 15:51:05 By : mr.win
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : Android นำค่าในฐานข้อมูล sql โดยผ่านโค้ด JSON มาแสดงในแอนดรอย์ไม่ได้คับ ลองทำตามหลายกระทู้แล้วก็ยังไม่ค่อยเข้าใจ ช่วยดูให้หน่อยนะคับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่