Android Web service

Web service call from VS C# database put into Android

MainActivity.java

package com.example.mywebserivce;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
}
public void OnWebservice(View view)
{
HttpClient hc=new DefaultHttpClient();
HttpGet hg=new HttpGet(
“http://172.16.28.61:8888/Students/getAllStudents”);
try {
HttpResponse hr =hc.execute(hg);
BufferedReader br=new BufferedReader(
new InputStreamReader(
hr.getEntity().getContent())
);
String data=br.readLine();
JSONArray ja=new JSONArray(data);
this.getContentResolver().delete(
Uri.parse(”content://rmutp.students/stu”),
null,null);
for(int i=0;i<ja.length();i++)
{
JSONObject jo=ja.getJSONObject(i);
//Toast.makeText(this,
// jo.getString(”studentid”),
// Toast.LENGTH_LONG).show();
ContentValues values=new ContentValues();
values.put(”studentid”, jo.getString(”studentid”));
values.put(”firstname”, jo.getString(”firstname”));
values.put(”lastname”, jo.getString(”lastname”));
values.put(”level”, jo.getInt(”level”));
this.getContentResolver().insert(Uri.parse(”content://rmutp.students/stu”),
values);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void OnView(View view)
{
Intent i=new Intent(this,StudentView.class);
this.startActivity(i);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

———————————————-
StudentView.java
package com.example.mywebserivce;
import android.app.ListActivity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.widget.SimpleCursorAdapter;
public class StudentView extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Cursor output=this.getContentResolver().query(
Uri.parse(”content://rmutp.students/stu”),
null, null,null, null);
SimpleCursorAdapter sca=
new SimpleCursorAdapter(
this,
R.layout.listlayout,
output,
new String[]{”studentid”,
“firstname”,”lastname”},
new int[]{R.id.textView1,R.id.textView2,
R.id.textView3}
);
this.setListAdapter(sca);
}
}
—————————————————-
listlayout.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent”>
<ImageView
android:id=”@+id/imageView2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:src=”@drawable/ic_launcher” />
<TextView
android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”TextView”
android:layout_weight=”1″/>
<TextView
android:id=”@+id/textView2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”TextView”
android:layout_weight=”1″/>
<TextView
android:id=”@+id/textView3″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”TextView”
android:layout_weight=”1″ />
<TextView
android:id=”@+id/textView4″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”TextView”
android:layout_weight=”1″ />
</LinearLayout>
———————————————–
36
35