-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTPPostTask.java
More file actions
107 lines (94 loc) · 3.35 KB
/
HTTPPostTask.java
File metadata and controls
107 lines (94 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.texx.simplehttppostrequest;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.AsyncTask;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Luca on 05/12/2014.
*
* This is a simple way to get result from a site using an async http post request
* Visit https://github.com/texx00/SimpleHTTPPostRequest for more info
*
* Remember to add INTERNET and ACCESS_NETWORK_STATE permissions to manifest
*/
public class HTTPPostTask extends AsyncTask <String, Integer, String> {
private String url;
private Context context;
private HTTPPostTaskListener l;
private List<NameValuePair> nvpl=new ArrayList<NameValuePair>();
public HTTPPostTask(Context context, String url, HTTPPostTaskListener listener){
this.url=url;
l=listener;
this.context=context;
}
/**
* used to check if a connection is available
* @return true if an internet connection is available, false if not
*/
public boolean checkConnection(){
ConnectivityManager cm=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
if(cm!=null && cm.getActiveNetworkInfo()!=null){
return cm.getActiveNetworkInfo().isConnectedOrConnecting();
}else return false;
}
/**
* Used to add params as post request
* @param name of the param
* @param value of the param
*/
public void addPostParam(String name, String value){
nvpl.add(new BasicNameValuePair(name, value));
}
@Override
protected String doInBackground(String[] params){
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httpPost=new HttpPost(url);
StringBuilder stringBuilder=new StringBuilder();
String buffStrChunk;
try{
httpPost.setEntity(new UrlEncodedFormEntity(nvpl));
HttpResponse httpResponse=client.execute(httpPost);
InputStream inputStream=httpResponse.getEntity().getContent();
InputStreamReader isr=new InputStreamReader(inputStream);
BufferedReader buffer=new BufferedReader(isr);
while((buffStrChunk=buffer.readLine())!=null){
stringBuilder.append(buffStrChunk);
}
}catch(Exception e){
e.printStackTrace();
if(l!=null)
l.onError(context);
}
return stringBuilder.toString();
}
@Override
protected void onPostExecute(String result){
if (l!=null)
l.onDataReceived(result);
}
/**
* listener class
*/
public interface HTTPPostTaskListener{
/**
* what to do when the result is available
*
* @param result: string with the text downloaded from requested page (null if something has gone wrong)
*/
public void onDataReceived(String result);
/**
* what to do if there is an error
*/
public void onError(Context c);
}
}