Prepared By : Uday Shah - HOD (IT)
Contact No : 7600044051
Jdemo4Activity.class
public class Jdemo4Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
HttpClient hc = new DefaultHttpClient();
HttpPost hp = new HttpPost("http://localhost/food");
TextView t1 = (TextView)findViewById(R.id.textView2);
try
{
HttpResponse hr = hc.execute(hp);
String res = inputSteramToString(hr.getEntity().getContent()).toString();
JSONObject object = new JSONObject(res);
String id = object.getString("food_id");
String nm = object.getString("food_name");
t1.setText("Food id : " +id + "Food Name :"+nm);
}
catch(Exception e)
{
}
}
private StringBuilder inputSteramToString(InputStream is)
{
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
try
{
while((rLine = br.readLine())!= null)
{
answer.append(rLine);
}
}
catch(Exception e)
{
}
return answer;
}
}
-------------------------------------------------------------------------------------------------
Jdemo4Activity Manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jdemo4"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Jdemo4Activity"
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>
-------------------------------------------------------------------------------------------------
main.xml file :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/LinearLayout1" xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/TextView1" android:layout_alignParentLeft="true" android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView android:layout_height="wrap_content" android:id="@+id/textView2" android:text="" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_below="@+id/TextView1" android:layout_alignParentLeft="true" android:layout_marginLeft="66dp" android:layout_marginTop="78dp"></TextView>
</RelativeLayout>