download: http://www.mediafire.com/?e498o8m1em423bk
index.php
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("Deal", $con);
$result = mysql_query("SELECT * FROM city");
while($row = mysql_fetch_assoc($result))
$output[]=$row;
print(json_encode($output)) ;
mysql_close($con);
?
index.php
--------------------------
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("Deal", $con);
$result = mysql_query("SELECT * FROM city");
while($row = mysql_fetch_assoc($result))
$output[]=$row;
print(json_encode($output)) ;
mysql_close($con);
?
City.java
---------------------
package com.androidtutorialbd.blogspot;
import java.io.BufferedReader;
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.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.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.net.ParseException;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class City extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
String result = null;
InputStream is = null;
StringBuilder sb = null;
ArrayList nameValuePairs = new ArrayList();
List r = new ArrayList();
String city_name;
Integer city_id;
try
{
// http post
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.244/sessionExample/");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch (Exception e)
{
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
.show();
}
// Convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "UTF-8"));
sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
}
catch (Exception e)
{
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
.show();
}
// END Convert response to string
try
{
JSONArray jArray = new JSONArray(result);
JSONObject json_data = null;
for (int i = 0; i < jArray.length(); i++)
{
json_data = jArray.getJSONObject(i);
//r.add(json_data.getString("CITY_NAME"));
city_id=json_data.getInt("CITY_ID");
city_name=json_data.getString("CITY_NAME");
//Log.d("-----"+json_data.getString("CITY_NAME")+"------", " " );
Log.d("-------"+"city_id="+city_id.toString()+" "+"CITY_NAME="+city_name+"-------", " ");
}
/*setListAdapter(new ArrayAdapter(this,
android.R.layout.simple_expandable_list_item_1, r)); */
}
catch (JSONException e1)
{
Toast.makeText(getBaseContext(), e1.toString(), Toast.LENGTH_LONG)
.show();
}
catch (ParseException e1)
{
Toast.makeText(getBaseContext(), e1.toString(), Toast.LENGTH_LONG)
.show();
}
}
}
AndroidManifest.xml
------------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidtutorialbd.blogspot"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".City"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidtutorialbd.blogspot"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".City"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
No comments:
Post a Comment