>
> If you wish... Here is an Network Class that I made that I use for all of
> my requests, Create a new Class, and import the following, use it as
> follows:
Network network = new Network();
String response = network.getResponse("http://www.google.com", postName1,
postData1);
Code:
/******************************************************************************/
/*********** tinyClark Development 2011 (Matt
Clark)**********/
/*********** http://www.tinyclark.com
**********/
/******************************************************************************/
package tinyClark.libs;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
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 android.util.Log;
public class Network {
boolean Logging_LogCat = true;
public Network(boolean EnableLogging) {
Logging_LogCat = EnableLogging;
}
public Network(){
Logging_LogCat = false;
}
public String getResponse(String URL) {
return (getFinalResponse(URL, null, null, null, null, null, null, null,
null));
}
public String getResponse(String URL, String postName1, String postData1) {
return (getFinalResponse(URL, postName1, postData1, null, null, null, null,
null, null));
}
public String getResponse(String URL, String postName1, String postData1,
String postName2, String postData2) {
return (getFinalResponse(URL, postName1, postData1, postName2, postData2,
null, null, null, null));
}
public String getResponse(String URL, String postName1, String postData1,
String postName2, String postData2, String postName3, String postData3) {
return (getFinalResponse(URL, postName1, postData1, postName2, postData2,
postName3, postData3, null, null));
}
public String getResponse(String URL, String postName1, String postData1,
String postName2, String postData2, String postName3, String postData3,
String postName4,
String postData4) {
return (getFinalResponse(URL, postName1, postData1, postName2, postData2,
postName3, postData3, postName4, postData4));
}
private String getFinalResponse(String URL, String postName1, String
postData1, String postName2, String postData2, String postName3, String
postData3, String postName4,
String postData4) {
HttpResponse response;
HttpClient httpclient;
HttpPost httppost;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
String finalReturn = "err";
if (Logging_LogCat) {
Log.d("tinyClark.lib.Network", "Network Request");
}
try {
httpclient = new DefaultHttpClient();
httppost = new HttpPost(URL);
if (postName1 != null) {
nameValuePairs.add(new BasicNameValuePair(postName1, postData1));
}
if (postName2 != null) {
nameValuePairs.add(new BasicNameValuePair(postName2, postData2));
}
if (postName3 != null) {
nameValuePairs.add(new BasicNameValuePair(postName3, postData3));
}
if (postName4 != null) {
nameValuePairs.add(new BasicNameValuePair(postName4, postData4));
}
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
finalReturn =
(inputStreamToString(response.getEntity().getContent()).toString());
if (Logging_LogCat) {
Log.d("tinyClark.lib.Network", finalReturn);
}
return finalReturn;
} catch (Exception e) {
if(Logging_LogCat){
Log.d("tinyCLark.lib.Network.Error", e.toString());
}
return "err";
}
}
private StringBuilder inputStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (Exception e) {
}
return total;
}
}
>
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en