  | 
              
	              
	                
 
 
  
    
        
        Android  - เมื่อกดให้เรียก spinner ที่ดึงข้อมูลจาก MySQL Database ให้แสดงแอพจะค้าง แก้ไขอย่างไรดีค่ะ     | 
   
  
    |   | 
   
 
 
 
	
		
			  | 
	   | 
	    | 
		
			  | 
	 
	
		
			  | 
		 		   | 
	  	    
          
            
			
	
			
			 
                Androidminifest.xml มีการอนุญาติอินเตอร์เน็ตแล้วค่ะ 
 
<uses-permission android:name="android.permission.INTERNET" />
  
 
โค้ดที่เขียนค่ะ 
Code (Android-Java) 
protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.adnews);
		setupActionBar();
		
		if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
            
        }
	
public void clickr11(View v){
    showspinner();
}
private void showspinner(){
		// spinner1
        
	//servername is ip address my computer + name folder 
	addinsql ss= new addinsql();	
 	String url = ss.getserverName()+"getJSON_spinnlight.php";
		try {
			
			JSONArray data = new JSONArray(getJSONUrl(url));
			Spinner spin1 =(Spinner)findViewById(R.id.spinner1);			
			
			final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
			HashMap<String, String> map;
			
			for(int i = 0; i < data.length(); i++){
                JSONObject c = data.getJSONObject(i);
                
    			map = new HashMap<String, String>();
    			map.put("light_detail", c.getString("light_detail"));
    			MyArrList.add(map);
    			
			}
			
			HashMap<String, String> m = MyArrList.get(0);//it will get the first HashMap Stored in array list 
			spinlight =new String[m.size()];
			int i = 0;
			for (HashMap<String, String> hash : MyArrList) {
			    for (String current : hash.values()) {
			        spinlight[i] = current;
			        i++;
			    }
			}
			
			
			ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>
			(this,android.R.layout.simple_spinner_item,spinlight);
			
			spin1.setAdapter(dataAdapter1);
			spin1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				final EditText e4 = (EditText)findViewById(R.id.e_51);
				
				if(position==5){
					e4.setVisibility(View.VISIBLE);
					e4.setText("");
				}
				else {
					e4.setVisibility(View.INVISIBLE);
					e4.setText("-");
				}
				// TODO Auto-generated method stub
				
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				Log.e(TAG,"มาแล้ว2");
				Toast.makeText(getApplicationContext(),"not", Toast.LENGTH_SHORT).show();
				
			}});
			
                        } catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	     
	}
	public String getJSONUrl(String url) {
		StringBuilder str = new StringBuilder();
		HttpClient client = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(url);
		try {
			HttpResponse response = client.execute(httpGet);
			StatusLine statusLine = response.getStatusLine();
			int statusCode = statusLine.getStatusCode();
			if (statusCode == 200) { // Download 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();
	}
 
 
 
 
โค้ดไฟล์php 
 
Code (PHP) 
<?php
	$objConnect = mysql_connect("localhost","root","****");
	$objDB = mysql_select_db("visit");
	$strSQL = "SELECT light_detail FROM light";
	$objQuery = mysql_query($strSQL);
	$intNumField = mysql_num_fields($objQuery);
	$resultArray = array();
	while($obResult = mysql_fetch_array($objQuery))
	{
		$arrCol = array();
		for($i=0;$i<$intNumField;$i++)
		{
			$arrCol[mysql_field_name($objQuery,$i)] = $obResult[$i];
		}
		array_push($resultArray,$arrCol);
	}
	
	mysql_close($objConnect);
	
	echo json_encode($resultArray);
?>
 
 
Logcat 
06-16 11:01:54.863: D/TextLayoutCache(7649): Enable myanmar Zawgyi converter 
06-16 11:01:54.893: W/System.err(7649): [DEBUG] GbaRequest - GbaRequest: Constructor Called 222 userAgent Apache-HttpClient/UNAVAILABLE (java 1.4) 
06-16 11:01:54.893: W/System.err(7649): [DEBUG] NafRequest - NafRequest: NafRequest constructor===useragent Apache-HttpClient/UNAVAILABLE (java 1.4) 
06-16 11:01:55.173: I/System.out(7649): Thread-1(ApacheHTTPLog):Reading from variable values from setDefaultValuesToVariables 
06-16 11:01:55.193: I/System.out(7649): Thread-1(ApacheHTTPLog):isShipBuild true 
06-16 11:01:55.193: I/System.out(7649): Thread-1(ApacheHTTPLog):SmartBonding Enabling is true, SHIP_BUILD is true, log to file is false, DBG is false 
06-16 11:02:10.113: I/System.out(7649): main calls detatch() 
06-16 11:02:10.113: W/System.err(7649): java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer) 
06-16 11:02:10.143: W/System.err(7649): 	at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:545) 
06-16 11:02:10.143: W/System.err(7649): 	at libcore.io.IoBridge.recvfrom(IoBridge.java:509) 
06-16 11:02:10.143: W/System.err(7649): 	at java.net.PlainSocketImpl.read(PlainSocketImpl.java:489) 
06-16 11:02:10.153: W/System.err(7649): 	at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46) 
06-16 11:02:10.153: W/System.err(7649): 	at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:241) 
06-16 11:02:10.153: W/System.err(7649): 	at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103) 
06-16 11:02:10.153: W/System.err(7649): 	at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191) 
06-16 11:02:10.153: W/System.err(7649): 	at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:82) 
06-16 11:02:10.153: W/System.err(7649): 	at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174) 
06-16 11:02:10.153: W/System.err(7649): 	at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:180) 
06-16 11:02:10.153: W/System.err(7649): 	at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235) 
06-16 11:02:10.153: W/System.err(7649): 	at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259) 
06-16 11:02:10.153: W/System.err(7649): 	at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279) 
06-16 11:02:10.153: W/System.err(7649): 	at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121) 
06-16 11:02:10.153: W/System.err(7649): 	at org.apache.http.impl.client.DefaultRequestDirector.executeSB(DefaultRequestDirector.java:875) 
06-16 11:02:10.153: W/System.err(7649): 	at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:675) 
06-16 11:02:10.153: W/System.err(7649): 	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:567) 
06-16 11:02:10.153: W/System.err(7649): 	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:491) 
06-16 11:02:10.153: W/System.err(7649): 	at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:469) 
06-16 11:02:10.163: W/System.err(7649): 	at com.example.q1.AdnewsActivity.getJSONUrl(AdnewsActivity.java:213) 
06-16 11:02:10.163: W/System.err(7649): 	at com.example.q1.AdnewsActivity.showspinner(AdnewsActivity.java:140) 
06-16 11:02:10.163: W/System.err(7649): 	at com.example.q1.AdnewsActivity.clickr11(AdnewsActivity.java:114) 
06-16 11:02:10.163: W/System.err(7649): 	at java.lang.reflect.Method.invokeNative(Native Method) 
06-16 11:02:10.163: W/System.err(7649): 	at java.lang.reflect.Method.invoke(Method.java:515) 
06-16 11:02:10.163: W/System.err(7649): 	at android.view.View$1.onClick(View.java:3964) 
06-16 11:02:10.163: W/System.err(7649): 	at android.view.View.performClick(View.java:4640) 
06-16 11:02:10.163: W/System.err(7649): 	at android.widget.CompoundButton.performClick(CompoundButton.java:104) 
06-16 11:02:10.163: W/System.err(7649): 	at android.view.View$PerformClick.run(View.java:19421) 
06-16 11:02:10.163: W/System.err(7649): 	at android.os.Handler.handleCallback(Handler.java:733) 
06-16 11:02:10.163: W/System.err(7649): 	at android.os.Handler.dispatchMessage(Handler.java:95) 
06-16 11:02:10.163: W/System.err(7649): 	at android.os.Looper.loop(Looper.java:136) 
06-16 11:02:10.163: W/System.err(7649): 	at android.app.ActivityThread.main(ActivityThread.java:5579) 
06-16 11:02:10.163: W/System.err(7649): 	at java.lang.reflect.Method.invokeNative(Native Method) 
06-16 11:02:10.163: W/System.err(7649): 	at java.lang.reflect.Method.invoke(Method.java:515) 
06-16 11:02:10.173: W/System.err(7649): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
06-16 11:02:10.173: W/System.err(7649): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
06-16 11:02:10.173: W/System.err(7649): 	at dalvik.system.NativeStart.main(Native Method) 
06-16 11:02:10.173: W/System.err(7649): Caused by: libcore.io.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer) 
06-16 11:02:10.183: W/System.err(7649): 	at libcore.io.Posix.recvfromBytes(Native Method) 
06-16 11:02:10.183: W/System.err(7649): 	at libcore.io.Posix.recvfrom(Posix.java:141) 
06-16 11:02:10.183: W/System.err(7649): 	at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164) 
06-16 11:02:10.183: W/System.err(7649): 	at libcore.io.IoBridge.recvfrom(IoBridge.java:506) 
06-16 11:02:10.183: W/System.err(7649): 	... 35 more 
06-16 11:02:10.183: W/System.err(7649): org.json.JSONException: End of input at character 0 of  
06-16 11:02:10.183: W/System.err(7649): 	at org.json.JSONTokener.syntaxError(JSONTokener.java:450) 
06-16 11:02:10.193: W/System.err(7649): 	at org.json.JSONTokener.nextValue(JSONTokener.java:97) 
06-16 11:02:10.193: W/System.err(7649): 	at org.json.JSONArray.<init>(JSONArray.java:92) 
06-16 11:02:10.193: W/System.err(7649): 	at org.json.JSONArray.<init>(JSONArray.java:108) 
06-16 11:02:10.193: W/System.err(7649): 	at com.example.q1.AdnewsActivity.showspinner(AdnewsActivity.java:140) 
06-16 11:02:10.193: W/System.err(7649): 	at com.example.q1.AdnewsActivity.clickr11(AdnewsActivity.java:114) 
06-16 11:02:10.193: W/System.err(7649): 	at java.lang.reflect.Method.invokeNative(Native Method) 
06-16 11:02:10.193: W/System.err(7649): 	at java.lang.reflect.Method.invoke(Method.java:515) 
06-16 11:02:10.193: W/System.err(7649): 	at android.view.View$1.onClick(View.java:3964) 
06-16 11:02:10.193: W/System.err(7649): 	at android.view.View.performClick(View.java:4640) 
06-16 11:02:10.193: W/System.err(7649): 	at android.widget.CompoundButton.performClick(CompoundButton.java:104) 
06-16 11:02:10.193: W/System.err(7649): 	at android.view.View$PerformClick.run(View.java:19421) 
06-16 11:02:10.193: W/System.err(7649): 	at android.os.Handler.handleCallback(Handler.java:733) 
06-16 11:02:10.193: W/System.err(7649): 	at android.os.Handler.dispatchMessage(Handler.java:95) 
06-16 11:02:10.193: W/System.err(7649): 	at android.os.Looper.loop(Looper.java:136) 
06-16 11:02:10.193: W/System.err(7649): 	at android.app.ActivityThread.main(ActivityThread.java:5579) 
06-16 11:02:10.193: W/System.err(7649): 	at java.lang.reflect.Method.invokeNative(Native Method) 
06-16 11:02:10.193: W/System.err(7649): 	at java.lang.reflect.Method.invoke(Method.java:515) 
06-16 11:02:10.193: W/System.err(7649): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
06-16 11:02:10.193: W/System.err(7649): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
06-16 11:02:10.193: W/System.err(7649): 	at dalvik.system.NativeStart.main(Native Method) 
06-16 11:02:10.263: I/Choreographer(7649): Skipped 925 frames!  The application may be doing too much work on its main thread. 
06-16 11:02:10.283: D/skia(7649): GFXPNG PNG bitmap created width:12 height:12 bitmap id is 283  
06-16 11:02:10.283: D/AbsListView(7649): Get MotionRecognitionManager 
06-16 11:02:10.323: D/AbsListView(7649): unregisterIRListener() is called  
06-16 11:02:10.353: D/AbsListView(7649): unregisterIRListener() is called  
06-16 11:02:10.353: D/AbsListView(7649): unregisterIRListener() is called  
06-16 11:02:10.363: D/AbsListView(7649): unregisterIRListener() is called 
 
 
  Tag : Mobile, MySQL, Android, Mobile               
                        | 
           
          
            
		
  ประวัติการแก้ไข 2014-06-16 11:16:18 2014-06-16 11:35:47	
                             | 
           
          
            
              
                   | 
                   | 
                   | 
               
              
                   | 
                
                    
                      | Date :
                          2014-06-16 11:09:37 | 
                      By :
                          boom159 | 
                      View :
                          2175 | 
                      Reply :
                          6 | 
                     
                  | 
                   | 
               
              
                   | 
                   | 
                   | 
               
              | 
           
          
            | 
			 | 
           
         
	    
		             | 
		
			  | 
	 
	
		
			  | 
		  | 
		
			  | 
		
			  | 
	 
 
              
  
          
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 Code (Android-Java) 
org.json.JSONException: End of input at character 0 of 
  
 
JSONArray ที่เรียกเมธอด getJSONUrl() ได้ค่าอะไรอะครับ format มันถูกหรือเปล่า หรือมัน empty?                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2014-06-16 14:03:05 | 
                        By :
                            devahoy | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	     
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ต้องลอง Debug หรือเรียกไฟล์ PHP โดยตรงดูครับ                        
               
               | 
             
            
              
			  			  
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2014-06-16 15:10:57 | 
                        By :
                            mr.win | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 เรียกไฟล์ php จาก http://127.0.0.1/visit/getJSON_spinlight แล้วขึ้นแบบนี้ค่ะ 
 
[{"light_detail":"??????????"},{"light_detail":"????????????"},{"light_detail":"????????????"},{"light_detail":"????stand????"},{"light_detail":"????stand????"},{"light_detail":"????? ????"}] 
 
ข้อมูลเป็นภาษาไทยค่ะ เกี่ยวไหมไม่รู้                        
               
               | 
             
            
              
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2014-06-16 15:25:17 | 
                        By :
                            boom159 | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 ถ้าภาษาไทยเป็นแบบที่ไปกำหนด Charset เป็น UTF-8 บน PHP/MySQL ครับ                        
               
               | 
             
            
              
			  			  
			                              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2014-06-16 15:29:15 | 
                        By :
                            mr.win | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
               
		
     
		
	  
        
             | 
            | 
            | 
             | 
         
        
             | 
                       | 
          
            
               
                 เรียกผ่าน php 
กลายเป็นแบบนี้แทน [{"light_detail":""},{"light_detail":""},{"light_detail":""},{"light_detail":""},{"light_detail":""},{"light_detail":""}] 
 
แต่ถ้าเลือกให้แสดง id แทนซึ่งเป็นตัวเลขมันจะขึ้นค่าใน php ให้ 
[{"id_light":"1"},{"id_light":"2"},{"id_light":"3"},{"id_light":"4"},{"id_light":"5"},{"id_light":"6"}] 
 
แต่ไม่ว่าจะยังไงก็ไม่ขึ้นใน spinner ในโปรแกรมอยู่ดี                        
               
               | 
             
            
              
			                
  ประวัติการแก้ไข 2014-06-16 17:25:53              
                              
              
                
                     | 
                     | 
                     | 
                 
                
                     | 
                  
                      
                        | Date :
                            2014-06-16 16:04:06 | 
                        By :
                            boom159 | 
                         
                    | 
                     | 
                 
                
                     | 
                     | 
                     | 
                 
                | 
             
           
			         | 
             | 
         
        
             | 
            | 
             | 
             | 
         
          
	    
     
      		  
	
     | 
   
 
                 |