SQL File
PHP File
<?php
$DB_USER='YOUR USER';
$DB_PASS='YOUR PASSWORD';
$DB_HOST='localhost';
$DB_NAME='YOUR DATABASE NAME';
$mysqli = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
/* check connection */
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$mysqli->query("SET NAMES 'utf8'");
$sql="SELECT iname FROM Spinnertest";
$result=$mysqli->query($sql);
while($e=mysqli_fetch_assoc($result))
{
$output[]=$e;
}
print(json_encode($output));
$mysqli->close();
?>
gradle Files
include android {....} brfore buildToolsVersion
useLibrary 'org.apache.http.legacy'
incclude dependencies{....}
compile 'com.loopj.android:android-async-http:1.4.5'
compile 'com.android.support:design:23.0.0'
Android Files
1. AndroidManifest.xml
Include user permission in your AndroidManifest File
<uses-permission android:name="android.permission.INTERNET" />
2. spinner_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent> <TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="30dp"
android:textColor="#000000" />
</LinearLayout>
3. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity"
android:weightSum="1"> <TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="Click Spinner"android:gravity="center"android:textSize="25dp" /> <Spinnerandroid:id="@+id/spinner"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_weight="0.07"> </Spinner></LinearLayout>
4. MainActivity.java
package com.spinnerexample; import android.app.ProgressDialog; import android.os.AsyncTask; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.Toast; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; public class MainActivity extends AppCompatActivity{ ArrayList<String> listItems=new ArrayList<>(); ArrayAdapter<String> adapter; Spinner sp; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sp=(Spinner)findViewById(R.id.spinner); adapter=new ArrayAdapter<String>(this,R.layout.spinner_layout,R.id.txt,listItems); sp.setAdapter(adapter); sp.setOnItemSelectedListener( new AdapterView.OnItemSelectedListener() { public void onItemSelected( AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getApplicationContext(),listItems.get(position),Toast.LENGTH_LONG).show(); } public void onNothingSelected(AdapterView<?> parent) { } }); } public void onStart(){ super.onStart(); BackTask bt=new BackTask(); bt.execute(); } private class BackTask extends AsyncTask<Void,Void,Void> { ProgressDialog loading; ArrayList<String> list; protected void onPreExecute(){ super.onPreExecute(); loading = ProgressDialog.show(MainActivity.this, "Please Wait",null, true, true); list=new ArrayList<>(); } protected Void doInBackground(Void...params){ InputStream is=null; String result=""; try{ HttpClient httpclient=new DefaultHttpClient(); HttpPost httppost= new HttpPost("http://krishscs.esy.es/Spinner_test.php"); HttpResponse response=httpclient.execute(httppost); HttpEntity entity = response.getEntity(); // Get our response as a String.
is = entity.getContent(); }catch(IOException e){ e.printStackTrace(); } //convert response to string try{ BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8")); String line = null; while ((line = reader.readLine()) != null) { result+=line; } is.close(); //result=sb.toString();
}catch(Exception e){ e.printStackTrace(); } // parse json data
try{ JSONArray jArray =new JSONArray(result); for(int i=0;i<jArray.length();i++){ JSONObject jsonObject=jArray.getJSONObject(i); // add interviewee name to arraylist
list.add(jsonObject.getString("iname")); } } catch(JSONException e){ e.printStackTrace(); } return null; } protected void onPostExecute(Void result){ listItems.addAll(list); adapter.notifyDataSetChanged(); loading.dismiss(); Toast.makeText(getApplicationContext(),"Loading Completed",Toast.LENGTH_LONG).show(); } } }
No comments:
Post a Comment