001.
package
com.androidtutorialpoint.androidlogin;
002.
003.
import
android.app.Activity;
004.
import
android.content.Context;
005.
import
android.content.Intent;
006.
import
android.content.SharedPreferences;
007.
import
android.os.AsyncTask;
008.
import
android.os.Bundle;
009.
import
android.preference.PreferenceManager;
010.
import
android.support.v4.widget.SwipeRefreshLayout;
011.
import
android.util.Log;
012.
import
android.view.View;
013.
import
android.view.Window;
014.
import
android.widget.ListView;
015.
import
android.widget.ProgressBar;
016.
import
android.widget.TextView;
017.
import
android.widget.Toast;
018.
019.
import
org.apache.http.HttpEntity;
020.
import
org.apache.http.HttpResponse;
021.
import
org.apache.http.NameValuePair;
022.
import
org.apache.http.client.ClientProtocolException;
023.
import
org.apache.http.client.HttpClient;
024.
import
org.apache.http.client.entity.UrlEncodedFormEntity;
025.
import
org.apache.http.client.methods.HttpGet;
026.
import
org.apache.http.client.methods.HttpPost;
027.
import
org.apache.http.client.methods.HttpUriRequest;
028.
import
org.apache.http.impl.client.DefaultHttpClient;
029.
import
org.apache.http.message.BasicNameValuePair;
030.
import
org.apache.http.params.BasicHttpParams;
031.
import
org.apache.http.params.HttpConnectionParams;
032.
import
org.apache.http.params.HttpParams;
033.
import
org.apache.http.protocol.HTTP;
034.
import
org.json.JSONArray;
035.
import
org.json.JSONException;
036.
import
org.json.JSONObject;
037.
038.
import
java.io.BufferedReader;
039.
import
java.io.IOException;
040.
import
java.io.InputStream;
041.
import
java.io.InputStreamReader;
042.
import
java.net.URLEncoder;
043.
import
java.util.ArrayList;
044.
import
java.util.List;
045.
046.
047.
public
class
TopicActivity
extends
Activity {
048.
String GetUser;
049.
private
SwipeRefreshLayout refresh;
050.
private
TextView userTextView;
051.
private
TextView idTextView;
052.
ListView listCollege;
053.
ProgressBar proCollageList;
054.
055.
@Override
056.
protected
void
onCreate(Bundle savedInstanceState)
057.
{
058.
super
.onCreate(savedInstanceState);
059.
requestWindowFeature(Window.FEATURE_NO_TITLE);
060.
setContentView(R.layout.activity_topic);
061.
062.
refresh = (SwipeRefreshLayout)
063.
findViewById(R.id.swipe_refresh_layout);
064.
065.
refresh.setOnRefreshListener(
new
SwipeRefreshLayout.OnRefreshListener() {
066.
@Override
067.
public
void
onRefresh() {
068.
Intent intent = getIntent();
069.
finish();
070.
overridePendingTransition(
0
,
0
);
071.
startActivity(intent);
072.
overridePendingTransition(
0
,
0
);
073.
}
074.
});
075.
076.
Bundle bundle = getIntent().getExtras();
077.
078.
listCollege = (ListView)findViewById(R.id.listCollege);
079.
proCollageList = (ProgressBar)findViewById(R.id.proCollageList);
080.
081.
final
String user = bundle.getString(
"username"
);
082.
final
String id = bundle.getString(
"userid"
);
083.
084.
userTextView = (TextView) findViewById(R.id.username_text);
085.
userTextView.setText(user);
086.
idTextView = (TextView) findViewById(R.id.userid_text);
087.
idTextView.setText(id);
088.
089.
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(
this
);
090.
SharedPreferences.Editor editor = prefs.edit();
091.
editor.putString(
"username"
, user);
092.
editor.putString(
"userid"
, id);
093.
editor.commit();
094.
095.
new
GetHttpResponse(
this
).execute();
096.
097.
}
098.
099.
100.
private
class
GetHttpResponse
extends
AsyncTask<Void, Void, Void>
101.
{
102.
private
Context context;
103.
String result;
104.
List<cources> collegeList;
105.
public
GetHttpResponse(Context context)
106.
{
107.
this
.context = context;
108.
}
109.
110.
@Override
111.
protected
void
onPreExecute()
112.
{
113.
super
.onPreExecute();
114.
}
115.
116.
@Override
117.
protected
Void doInBackground(Void... arg0)
118.
{
120.
try
121.
{
122.
httpService.ExecutePostRequest();
123.
124.
if
(httpService.getResponseCode() ==
200
)
125.
{
126.
result = httpService.getResponse();
127.
Log.d(
"Result"
, result);
128.
if
(result !=
null
)
129.
{
130.
JSONArray jsonArray =
null
;
131.
try
{
132.
jsonArray =
new
JSONArray(result);
133.
134.
JSONObject object;
135.
JSONArray array;
136.
cources college;
137.
collegeList =
new
ArrayList<cources>();
138.
for
(
int
i=
0
; i<jsonArray.length(); i++)
139.
{
140.
college =
new
cources();
141.
object = jsonArray.getJSONObject(i);
142.
143.
college.product_id = object.getString(
"sc_poker_product"
);
144.
college.cources_description = object.getString(
"sc_product_story"
);
145.
collegeList.add(college);
146.
}
147.
}
148.
catch
(JSONException e) {
149.
150.
e.printStackTrace();
151.
}
152.
}
153.
}
154.
else
155.
{
156.
Toast.makeText(context, httpService.getErrorMessage(), Toast.LENGTH_SHORT).show();
157.
}
158.
}
159.
catch
(Exception e)
160.
{
161.
162.
e.printStackTrace();
163.
}
164.
return
null
;
165.
}
166.
167.
@Override
168.
protected
void
onPostExecute(Void result)
169.
170.
{
171.
proCollageList.setVisibility(View.GONE);
172.
listCollege.setVisibility(View.VISIBLE);
173.
if
(collegeList !=
null
)
174.
{
175.
ListAdapter adapter =
new
ListAdapter(collegeList, context);
176.
listCollege.setAdapter(adapter);
177.
}
178.
}
179.
}
180.
181.
public
class
HttpService
182.
{
183.
private
ArrayList<NameValuePair> params;
184.
private
ArrayList<NameValuePair> headers;
185.
186.
private
String url;
187.
private
int
responseCode;
188.
private
String message;
189.
private
String response;
190.
191.
public
String getResponse()
192.
{
193.
return
response;
194.
}
195.
196.
public
String getErrorMessage()
197.
{
198.
return
message;
199.
}
200.
201.
public
int
getResponseCode()
202.
{
203.
return
responseCode;
204.
}
205.
206.
public
HttpService(String url)
207.
{
208.
this
.url = url;
209.
params =
new
ArrayList<NameValuePair>();
210.
headers =
new
ArrayList<NameValuePair>();
211.
}
212.
213.
public
void
AddParam(String name, String value)
214.
{
215.
params.add(
new
BasicNameValuePair(name, value));
216.
}
217.
218.
public
void
AddHeader(String name, String value)
219.
{
220.
headers.add(
new
BasicNameValuePair(name, value));
221.
}
222.
223.
public
void
ExecuteGetRequest()
throws
Exception
224.
{
225.
String combinedParams =
""
;
226.
if
(!params.isEmpty())
227.
{
228.
combinedParams +=
"?"
;
229.
for
(NameValuePair p : params)
230.
{
231.
String paramString = p.getName() +
"="
+ URLEncoder.encode(p.getValue(),
"UTF-8"
);
232.
if
(combinedParams.length() >
1
)
233.
{
234.
combinedParams +=
"&"
+ paramString;
235.
}
236.
else
237.
{
238.
combinedParams += paramString;
239.
}
240.
}
241.
}
242.
243.
HttpGet request =
new
HttpGet(url + combinedParams);
244.
for
(NameValuePair h : headers)
245.
{
246.
request.addHeader(h.getName(), h.getValue());
247.
}
248.
249.
executeRequest(request, url);
250.
}
251.
252.
public
void
ExecutePostRequest()
throws
Exception
253.
{
254.
HttpPost request =
new
HttpPost(url);
255.
for
(NameValuePair h : headers)
256.
{
257.
request.addHeader(h.getName(), h.getValue());
258.
}
259.
260.
if
(!params.isEmpty())
261.
{
262.
request.setEntity(
new
UrlEncodedFormEntity(params, HTTP.UTF_8));
263.
}
264.
265.
executeRequest(request, url);
266.
}
267.
268.
private
void
executeRequest(HttpUriRequest request, String url)
269.
{
270.
HttpParams httpParameters =
new
BasicHttpParams();
271.
int
timeoutConnection =
10000
;
272.
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
273.
int
timeoutSocket =
10000
;
274.
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
275.
276.
HttpClient client =
new
DefaultHttpClient(httpParameters);
277.
HttpResponse httpResponse;
278.
try
279.
{
280.
httpResponse = client.execute(request);
281.
responseCode = httpResponse.getStatusLine().getStatusCode();
282.
message = httpResponse.getStatusLine().getReasonPhrase();
283.
284.
HttpEntity entity = httpResponse.getEntity();
285.
if
(entity !=
null
)
286.
{
287.
InputStream instream = entity.getContent();
288.
response = convertStreamToString(instream);
289.
instream.close();
290.
}
291.
}
292.
catch
(ClientProtocolException e)
293.
{
294.
client.getConnectionManager().shutdown();
295.
e.printStackTrace();
296.
}
297.
catch
(IOException e)
298.
{
299.
client.getConnectionManager().shutdown();
300.
e.printStackTrace();
301.
}
302.
}
303.
304.
private
String convertStreamToString(InputStream is)
305.
{
306.
BufferedReader reader =
new
BufferedReader(
new
InputStreamReader(is));
307.
StringBuilder sb =
new
StringBuilder();
308.
309.
String line =
null
;
310.
try
311.
{
312.
while
((line = reader.readLine()) !=
null
)
313.
{
314.
sb.append(line +
"\n"
);
315.
}
316.
}
317.
catch
(IOException e)
318.
{
319.
e.printStackTrace();
320.
}
321.
finally
322.
{
323.
try
324.
{
325.
is.close();
326.
}
327.
catch
(IOException e)
328.
{
329.
e.printStackTrace();
330.
}
331.
}
332.
return
sb.toString();
333.
}
334.
}
335.
336.
}