001.
package com.Schoolmenu;
002.
003.
import android.app.Activity;
004.
import android.content.Intent;
005.
import android.os.Bundle;
006.
import android.view.View;
007.
import android.view.View.OnClickListener;
008.
import android.widget.Button;
009.
import android.util.Log;
010.
import android.widget.LinearLayout;
011.
import android.widget.TextView;
012.
013.
014.
import java.io.BufferedReader;
015.
import java.io.InputStream;
016.
import java.io.InputStreamReader;
017.
import java.net.URI;
018.
import java.util.ArrayList;
019.
020.
import org.apache.http.HttpEntity;
021.
import org.apache.http.HttpResponse;
022.
import org.apache.http.NameValuePair;
023.
import org.apache.http.client.HttpClient;
024.
import org.apache.http.client.entity.UrlEncodedFormEntity;
025.
import org.apache.http.client.methods.HttpPost;
026.
import org.apache.http.impl.client.DefaultHttpClient;
027.
import org.apache.http.message.BasicNameValuePair;
028.
import org.json.JSONArray;
029.
import org.json.JSONException;
030.
import org.json.JSONObject;
031.
032.
033.
034.
public class schoolnew extends Activity{
035.
036.
037.
protected void onCreate(Bundle savedInstanceState) {
038.
super
.onCreate(savedInstanceState);
039.
setContentView(R.layout.schoolnew);
040.
041.
042.
Button btnback=(Button)findViewById(R.id.btnback);
043.
btnback.setOnClickListener(
new
OnClickListener(){
044.
045.
public void onClick(View v) {
046.
Intent main=
new
Intent (schoolnew.
this
,SchoolmenuActivity.class);
047.
startActivity(main);
048.
049.
}});
050.
051.
052.
053.
TextView textresult = (TextView ) findViewById(R.id.txtresult);
054.
textresult = (TextView) findViewById(R.id.txtresult);
055.
056.
057.
058.
textresult.setText(
""
+ getServerData());
059.
}
060.
061.
private String getServerData() {
062.
String returnString =
""
;
063.
InputStream is =
null
;
064.
String result =
""
;
065.
String utf8String =
new
String(result.getBytes(
"iso-8859-1"
),
"utf-8"
);
066.
067.
068.
try
{
069.
070.
HttpClient httpclient =
new
DefaultHttpClient();
072.
073.
HttpResponse response = httpclient.execute(httppost);
074.
HttpEntity entity = response.getEntity();
075.
is = entity.getContent();
076.
}
catch
(Exception e) {
077.
Log.e(
"log_tag"
,
"Error in http connection "
+ e.toString());
078.
}
079.
080.
081.
try
{
082.
BufferedReader reader =
new
BufferedReader(
new
InputStreamReader(
083.
is,
"ISO-8859-11"
), 8);
084.
StringBuilder sb =
new
StringBuilder();
085.
String line =
null
;
086.
087.
088.
089.
090.
while
((line = reader.readLine()) !=
null
) {
091.
sb.append(line +
"\n"
);
092.
}
093.
is.close();
094.
result = sb.toString();
095.
}
catch
(Exception e) {
096.
Log.e(
"log_tag"
,
"Error converting result "
+ e.toString());
097.
}
098.
099.
100.
try
{
101.
102.
103.
JSONArray jArray =
new
JSONArray(result);
104.
for
(int i = 0; i < jArray.length(); i++) {
105.
JSONObject json_data = jArray.getJSONObject(i);
106.
107.
108.
109.
returnString +=
"\nผู้ตั้งเรื่อง : "
+ json_data.getString(
"poster"
)+
"\n"
110.
+
"\nหัวข้อ : "
+ json_data.getString(
"title"
)+
"\n"
111.
+
"\nรายละเอียด :"
+ json_data.getString(
"body"
)+
"\n"
112.
+
"\n----------------------------------------------------"
113.
;
114.
}
115.
}
catch
(JSONException e) {
116.
Log.e(
"log_tag"
,
"Error parsing data "
+ e.toString());
117.
}
118.
119.
120.
return
returnString;
121.
122.
123.
124.
125.
}
126.
127.
}