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 เชื่อมต่อฐานข้อมูล MySQL แล้วมีปัญหา รับค่าเป็น json



 

Android เชื่อมต่อฐานข้อมูล MySQL แล้วมีปัญหา รับค่าเป็น json

 



Topic : 115062



โพสกระทู้ ( 6 )
บทความ ( 0 )



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




อ้างอิงขั้นตอนการทำมาจากลิ่งนี้ ปรากฏว่าใช้ไม่ได้ https://www.thaicreate.com/mobile/android-connect-web-server-php-mysql.html

ไม่รู้ว่าเกิดจากอะไร เพิ่ม INTERNET ใน androidmanifest แล้วด้วย

Code ผม

Code
package com.example.csit.tourismjapan;

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.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.StrictMode;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;



public class SubIntroActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub_intro);

        TextView txtRegionName = (TextView)findViewById(R.id.txt_region);
        TextView txtRegionDetail = (TextView)findViewById(R.id.txt_detail);

        // Input Intro Activity
        Bundle bundle = getIntent().getExtras();
        String regionId = bundle.getString("region_id");

        // Value send to php
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("region_id", regionId));

        // URL Load json data
        String url = "http://xxxxxx/xxxxxx/getRegion.php";

        String resultServer  = getHttpPost(url, params);
        String RegionID = "";
        String RegionName = "";
        String Detail = "";
        String Latitude = "";
        String Longitude = "";
        String RegionImage = "";


        JSONObject c;

        try {
            c = new JSONObject(resultServer);
            RegionID = c.getString("RegionID");
            RegionName = c.getString("RegionName");
            Detail = c.getString("Detail");
            Latitude = c.getString("Latitude");
            Longitude = c.getString("Longitude");
            RegionImage = c.getString("RegionImage");


            if(!RegionID.equals(""))
            {
                txtRegionName.setText(RegionName);
                txtRegionDetail.setText(Detail);
            }
            else
            {
                txtRegionName.setText("-");
                txtRegionDetail.setText("-");
            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }

    public String getHttpPost(String url,List<NameValuePair> params) {
        StringBuilder str = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        try {
            httpPost.setEntity(new UrlEncodedFormEntity(params));
            HttpResponse response = client.execute(httpPost);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) { // Status 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 result..");
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str.toString();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

}



PHP

Code
<?php
	include "config.php";

	$strRegionId = 1;
	
	//$strRegionId = $_POST["region_id"];
	$strSQL = "SELECT * FROM dimregion WHERE RegionID = '".$strRegionId."'  ";
	mysql_query("SET NAMES UTF8");
	$objQuery = mysql_query($strSQL);
	$obResult = mysql_fetch_array($objQuery);
	if($obResult)
	{
		$arr["RegionID"] = $obResult["RegionID"];
		$arr["RegionName"] = $obResult["RegionName"];
		$arr["Detail"] = $obResult["Detail"];
		$arr["Latitude"] = $obResult["Latitude"];
		$arr["Longitude"] = $obResult["Longitude"];
		$arr["RegionImage"] = $obResult["RegionImage"];
	}

	
	mysql_close($objConnect);

	
	echo json_encode($arr);
?>


Error ครับ รบกวนช่วยดูให้ที่ครับ

Android mysql error



Tag : Mobile, MySQL, Android, JAVA







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2015-03-12 22:36:03 By : icecslox View : 1555 Reply : 1
 

 

No. 1



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

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

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

Quote:
resultServer


ลอง Debug ตัวนี้ดูหน่อยครับ ว่ามันส่งอะไรกลับมาให้ครับ ซึ่งจากตัวอย่างก็น่าจะเขียนถูกแล้วครับ

Code (Android-Java)
    public void showInfo()
    {
    	// txtMemberID,txtUsername,txtPassword,txtConPassword,txtName,txtEmail,txtTel
    	final TextView tMemberID = (TextView)findViewById(R.id.txtMemberID);
    	final TextView tUsername = (TextView)findViewById(R.id.txtUsername);
    	final TextView tPassword = (TextView)findViewById(R.id.txtPassword);
    	final TextView tConPassword = (TextView)findViewById(R.id.txtConPassword);
    	final TextView tName = (TextView)findViewById(R.id.txtName);
    	final TextView tEmail = (TextView)findViewById(R.id.txtEmail);
    	final TextView tTel = (TextView)findViewById(R.id.txtTel);
    	
    	Button btnSave = (Button) findViewById(R.id.btnSave);
    	Button btnCancel = (Button) findViewById(R.id.btnCancel);
    	
    	String url = "https://www.thaicreate.com/android/getByMemberID.php";
    	
    	Intent intent= getIntent();
    	final String MemberID = intent.getStringExtra("MemberID"); 

		List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("sMemberID", MemberID));

        /** Get result from Server (Return the JSON Code)
         * 
         * {"MemberID":"2","Username":"adisorn","Password":"adisorn@2","Name":"Adisorn Bunsong","Tel":"021978032","Email":"[email protected]"}
         */
        
    	String resultServer  = getHttpPost(url,params);
    	
        String strMemberID = "";
    	String strUsername = "";
    	String strPassword = "";
    	String strName = "";
    	String strEmail = "";
    	String strTel = "";
    	
    	JSONObject c;
		try {
			c = new JSONObject(resultServer);
			strMemberID = c.getString("MemberID");
			strUsername = c.getString("Username");
			strPassword = c.getString("Password");
			strName = c.getString("Name");
			strEmail = c.getString("Email");
			strTel = c.getString("Tel");
			
			if(!strMemberID.equals(""))
			{
				tMemberID.setText(strMemberID);
				tUsername.setText(strUsername);
				tPassword.setText(strPassword);
				tConPassword.setText(strPassword);
				tName.setText(strName);
				tEmail.setText(strEmail);
				tTel.setText(strTel);
			}
			else
			{
				tMemberID.setText("-");
				tUsername.setText("-");
				tName.setText("-");
				tEmail.setText("-");
				tTel.setText("-");
				btnSave.setEnabled(false);
				btnCancel.requestFocus();
			}
        	
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
    }







แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2015-03-14 07:07:43 By : mr.win
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : Android เชื่อมต่อฐานข้อมูล MySQL แล้วมีปัญหา รับค่าเป็น 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 01
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 อัตราราคา คลิกที่นี่