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,038

HOME > Mobile > Mobile Forum > Android อยากจะเพิ่มฟังชั่นให้ส่งค่าไป Select Database ก่อน แล้วดึงค่าที่ Select ออกมาแสดงครับ


[Mobile] Android อยากจะเพิ่มฟังชั่นให้ส่งค่าไป Select Database ก่อน แล้วดึงค่าที่ Select ออกมาแสดงครับ

 
Topic : 127650



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



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



อันนี้ตัวอย่างที่ผมเอามาดัดแปลงครับ

Code (Android)
001.package com.androidtutorialpoint.androidlogin;
002. 
003.import android.app.Activity;
004.import android.content.Context;
005.import android.content.Intent;
006.import android.content.SharedPreferences;
007.import android.os.AsyncTask;
008.import android.os.Bundle;
009.import android.preference.PreferenceManager;
010.import android.support.v4.widget.SwipeRefreshLayout;
011.import android.util.Log;
012.import android.view.View;
013.import android.view.Window;
014.import android.widget.ListView;
015.import android.widget.ProgressBar;
016.import android.widget.TextView;
017.import android.widget.Toast;
018. 
019.import org.apache.http.HttpEntity;
020.import org.apache.http.HttpResponse;
021.import org.apache.http.NameValuePair;
022.import org.apache.http.client.ClientProtocolException;
023.import org.apache.http.client.HttpClient;
024.import org.apache.http.client.entity.UrlEncodedFormEntity;
025.import org.apache.http.client.methods.HttpGet;
026.import org.apache.http.client.methods.HttpPost;
027.import org.apache.http.client.methods.HttpUriRequest;
028.import org.apache.http.impl.client.DefaultHttpClient;
029.import org.apache.http.message.BasicNameValuePair;
030.import org.apache.http.params.BasicHttpParams;
031.import org.apache.http.params.HttpConnectionParams;
032.import org.apache.http.params.HttpParams;
033.import org.apache.http.protocol.HTTP;
034.import org.json.JSONArray;
035.import org.json.JSONException;
036.import org.json.JSONObject;
037. 
038.import java.io.BufferedReader;
039.import java.io.IOException;
040.import java.io.InputStream;
041.import java.io.InputStreamReader;
042.import java.net.URLEncoder;
043.import java.util.ArrayList;
044.import java.util.List;
045. 
046. 
047.public class TopicActivity extends Activity {
048.    String GetUser;
049.    private SwipeRefreshLayout refresh;
050.    private TextView userTextView;
051.    private TextView idTextView;
052.    ListView listCollege;
053.    ProgressBar proCollageList;
054. 
055.    @Override
056.    protected void onCreate(Bundle savedInstanceState)
057.    {
058.        super.onCreate(savedInstanceState);
059.        requestWindowFeature(Window.FEATURE_NO_TITLE);
060.        setContentView(R.layout.activity_topic);
061. 
062.        refresh = (SwipeRefreshLayout)
063.                findViewById(R.id.swipe_refresh_layout);
064. 
065.        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
066.            @Override
067.            public void onRefresh() {
068.                Intent intent = getIntent();
069.                finish();
070.                overridePendingTransition( 0, 0);
071.                startActivity(intent);
072.                overridePendingTransition( 0, 0);
073.            }
074.        });
075. 
076.        Bundle bundle = getIntent().getExtras();
077. 
078.        listCollege = (ListView)findViewById(R.id.listCollege);
079.        proCollageList = (ProgressBar)findViewById(R.id.proCollageList);
080. 
081.        final String user = bundle.getString("username");
082.        final String id = bundle.getString("userid");
083. 
084.        userTextView = (TextView) findViewById(R.id.username_text);
085.        userTextView.setText(user);
086.        idTextView = (TextView) findViewById(R.id.userid_text);
087.        idTextView.setText(id);
088. 
089.        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
090.        SharedPreferences.Editor editor = prefs.edit();
091.        editor.putString("username", user);
092.        editor.putString("userid", id);
093.        editor.commit();
094. 
095.    new GetHttpResponse(this).execute();
096. 
097.    }
098. 
099. 
100.    private class GetHttpResponse extends AsyncTask<Void, Void, Void>
101.    {
102.        private Context context;
103.        String result;
104.        List<cources> collegeList;
105.        public GetHttpResponse(Context context)
106.        {
107.            this.context = context;
108.        }
109. 
110.        @Override
111.        protected void onPreExecute()
112.        {
113.            super.onPreExecute();
114.        }
115. 
116.        @Override
117.        protected Void doInBackground(Void... arg0)
118.        {
119.            HttpService httpService = new HttpService("https://thongkumdee.000webhostapp.com/courses.php");
120.            try
121.            {
122.                httpService.ExecutePostRequest();
123. 
124.                if(httpService.getResponseCode() == 200)
125.                {
126.                    result = httpService.getResponse();
127.                    Log.d("Result", result);
128.                    if(result != null)
129.                    {
130.                        JSONArray jsonArray = null;
131.                        try {
132.                            jsonArray = new JSONArray(result);
133. 
134.                            JSONObject object;
135.                            JSONArray array;
136.                            cources college;
137.                            collegeList = new ArrayList<cources>();
138.                            for(int i=0; i<jsonArray.length(); i++)
139.                            {
140.                                college = new cources();
141.                                object = jsonArray.getJSONObject(i);
142. 
143.                                college.product_id = object.getString("sc_poker_product");
144.                                college.cources_description = object.getString("sc_product_story");
145.                                collegeList.add(college);
146.                            }
147.                        }
148.                        catch (JSONException e) {
149.                            // TODO Auto-generated catch block
150.                            e.printStackTrace();
151.                        }
152.                    }
153.                }
154.                else
155.                {
156.                    Toast.makeText(context, httpService.getErrorMessage(), Toast.LENGTH_SHORT).show();
157.                }
158.            }
159.            catch (Exception e)
160.            {
161.                // TODO Auto-generated catch block
162.                e.printStackTrace();
163.            }
164.            return null;
165.        }
166. 
167.        @Override
168.        protected void onPostExecute(Void result)
169. 
170.        {
171.            proCollageList.setVisibility(View.GONE);
172.            listCollege.setVisibility(View.VISIBLE);
173.            if(collegeList != null)
174.            {
175.            ListAdapter adapter = new ListAdapter(collegeList, context);
176.            listCollege.setAdapter(adapter);
177.            }
178.        }
179.    }
180. 
181.    public class HttpService
182.    {
183.        private ArrayList<NameValuePair> params;
184.        private ArrayList<NameValuePair> headers;
185. 
186.        private String url;
187.        private int responseCode;
188.        private String message;
189.        private String response;
190. 
191.        public String getResponse()
192.        {
193.            return response;
194.        }
195. 
196.        public String getErrorMessage()
197.        {
198.            return message;
199.        }
200. 
201.        public int getResponseCode()
202.        {
203.            return responseCode;
204.        }
205. 
206.        public HttpService(String url)
207.        {
208.            this.url = url;
209.            params = new ArrayList<NameValuePair>();
210.            headers = new ArrayList<NameValuePair>();
211.        }
212. 
213.        public void AddParam(String name, String value)
214.        {
215.            params.add(new BasicNameValuePair(name, value));
216.        }
217. 
218.        public void AddHeader(String name, String value)
219.        {
220.            headers.add(new BasicNameValuePair(name, value));
221.        }
222. 
223.        public void ExecuteGetRequest() throws Exception
224.        {
225.            String combinedParams = "";
226.            if(!params.isEmpty())
227.            {
228.                combinedParams += "?";
229.                for(NameValuePair p : params)
230.                {
231.                    String paramString = p.getName() + "=" + URLEncoder.encode(p.getValue(),"UTF-8");
232.                    if(combinedParams.length() > 1)
233.                    {
234.                        combinedParams  +=  "&" + paramString;
235.                    }
236.                    else
237.                    {
238.                        combinedParams += paramString;
239.                    }
240.                }
241.            }
242. 
243.            HttpGet request = new HttpGet(url + combinedParams);
244.            for(NameValuePair h : headers)
245.            {
246.                request.addHeader(h.getName(), h.getValue());
247.            }
248. 
249.            executeRequest(request, url);
250.        }
251. 
252.        public void ExecutePostRequest() throws Exception
253.        {
254.            HttpPost request = new HttpPost(url);
255.            for(NameValuePair h : headers)
256.            {
257.                request.addHeader(h.getName(), h.getValue());
258.            }
259. 
260.            if(!params.isEmpty())
261.            {
262.                request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
263.            }
264. 
265.            executeRequest(request, url);
266.        }
267. 
268.        private void executeRequest(HttpUriRequest request, String url)
269.        {
270.            HttpParams httpParameters = new BasicHttpParams();
271.            int timeoutConnection = 10000;
272.            HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
273.            int timeoutSocket = 10000;
274.            HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
275.             
276.            HttpClient client = new DefaultHttpClient(httpParameters);
277.            HttpResponse httpResponse;
278.            try
279.            {
280.                httpResponse = client.execute(request);
281.                responseCode = httpResponse.getStatusLine().getStatusCode();
282.                message = httpResponse.getStatusLine().getReasonPhrase();
283. 
284.                HttpEntity entity = httpResponse.getEntity();
285.                if (entity != null)
286.                {
287.                    InputStream instream = entity.getContent();
288.                    response = convertStreamToString(instream);
289.                    instream.close();
290.                }
291.            }
292.            catch (ClientProtocolException e)
293.            {
294.                client.getConnectionManager().shutdown();
295.                e.printStackTrace();
296.            }
297.            catch (IOException e)
298.            {
299.                client.getConnectionManager().shutdown();
300.                e.printStackTrace();
301.            }
302.        }
303. 
304.        private String convertStreamToString(InputStream is)
305.        {
306.            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
307.            StringBuilder sb = new StringBuilder();
308. 
309.            String line = null;
310.            try
311.            {
312.                while ((line = reader.readLine()) != null)
313.                {
314.                    sb.append(line + "\n");
315.                }
316.            }
317.            catch (IOException e)
318.            {
319.                e.printStackTrace();
320.            }
321.            finally
322.            {
323.                try
324.                {
325.                    is.close();
326.                }
327.                catch (IOException e)
328.                {
329.                    e.printStackTrace();
330.                }
331.            }
332.            return sb.toString();
333.        }
334.    }
335. 
336.}


อันนี้เป็นส่วน PHP ที่ต่อกับ database
Code (PHP)
01.<?php
02.$hostname = "localhost";
03.$username = "id1479268_staporn";
04.$password = "054324696t";
05.$dbname = "id1479268_sc_db";
06. 
07.// Create connection
08.$conn = new mysqli($hostname, $username, $password, $dbname);
09. 
10.if ($conn->connect_error) {
11.    die("Connection failed: " . $conn->connect_error);
12.}
13. 
14.$id_user = 'us0005';
15. 
16.$sql = "SELECT * FROM sc_poker,sc_productbacklog WHERE sc_productbacklog.sc_product_id = sc_poker.sc_poker_product AND sc_poker_product NOT IN (SELECT sc_vote_product FROM sc_vote WHERE sc_vote_user = '$id_user')";
17.$result = $conn->query($sql);
18. 
19.if ($result->num_rows >0) {
20.    // output data of each row
21.    while($row[] = $result->fetch_assoc()) {
22.    $tem = $row;   
23.    $json = json_encode($tem);
24.    }
25.     
26.} else {
27.    echo "0 results";
28.}
29. echo $json;
30.$conn->close();
31.?>


ที่ผมทำตอนนี้มันแสดงค่าได้อย่างเดียว แต่ผมต้องการให้ android มันส่งค่า $id_user ไป select ด้วย แล้วค่อยนำข้อมูลที่ select มาแสดง ผมต้องเพิ่มฟังชั่นยังไงครับ



Tag : Mobile, Android

Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2017-05-18 14:26:52 By : Taiyark View : 1021 Reply : 1
 

 

No. 1



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

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

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

ดูวิธีการส่งค่าไปดึงมาใช้งานครับ



Android Edit/Update Data to Web Server Database (Web Server)

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-05-19 10:49:22 By : mr.win
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : Android อยากจะเพิ่มฟังชั่นให้ส่งค่าไป Select Database ก่อน แล้วดึงค่าที่ Select ออกมาแสดงครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ 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 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)





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