Android post to html - java

I use this code in my app to post:
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
my problem is that when I do 10 posts some time its slow and some times it's not recive answer from the server.
Their is a way to do a manager for the posts or some way to do it fater and to make sure that after this code run I know that it dose all it's needs to recive answer from the server?
Thanks

Related

Android Post data with CustomHttpClient.executeHttpGet

I am calling a url by following method
try {
urlcont= URLEncoder.encode(urlcont, "utf-8");
response = CustomHttpClient.executeHttpGet("http://www.myurlhere");
} catch (Exception e) {
e.printStackTrace();
}
URL called successfully but I want to post some data (in String urlcont ) so i can get it by $_POST['urlcont'];
Thanks
try this
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("name", "your name"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}

Android sending data to restful webservices

I am trying to send data from android smartphone to a restful webservice made in java using jersey library.
I saw the following answer on how to do it:
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
Although this seems about right i have a doubt in the nameValuePairs variable.
Particularly in this part:
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
If i have a webservice that has the following signature:
#POST
#Path("/post/location")
#Consumes(MediaType.APPLICATION_JSON)
public Response createLocation(Loc location)
what would be the "id" part in the nameValuePairs variable, it would be location or Loc?.
try this
//
String url = 'url_to_you_server_api.dev/postservice'
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(url);
JSONObject params = new JSONObject();
params.put("id","id");
params.put("hi","hi");
StringEntity jsonEntity = new StringEntity( params.toString() );
HttpPost request = new HttpPost(url);
request.addHeader("Content-Type","application/json");
request.setEntity(jsonEntity);
response = client.execute(request);
if you service receive a entity it might be a json or something not just key-value params that correspond to form-data.

Android Executing HTTP Post

So I am trying to connect my chat application to a server, I am using this code, but the problem with this code is that it is using hardcoded message such as "And Dev is cool" but I have a text field where I can enter my message and send that to the server, How do I change the code so it takes input from user and not hardcoded messages?
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
You need a to add an EditText to your xml view. then do
EditText yourText = (EditText) findViewById(R.id.'your edit text id');
You should have a button that the user clicks when they've entered their text that then launches the thread for internet and gets editText.getText() to send into the HTTP

Android post request using Httppost without urlencode parameter

That's my code... is it possible to remove the urlencode?
I want to pass post parameters without urlencode them.
Thanks
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "Hi"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
You can try something like this
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
BasicHttpEntity filter = new BasicHttpEntity();
filter.setContent(new ByteArrayInputStream("Filter=id :EQUALS: 12345 :AND: stringdata :EQUALS: hi".getBytes("UTF-8")));
httppost.setEntity(filter);
Thanks

postData cannot be resolved to a type

I'm new to Android programing. I'm trying to post some data to a server using post. I googled it up and came up with this:
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
My problem is I'm getting errors on the first line of this code:
postData cannot be resolved to a type
syntax error on token "{", delete this token
syntax error on token "void", # expected
I'm using Eclipse and I used Shift+Ctrl+o to get all the imports.
You problem (based on the information you've given so far) is that you're declaring the function postData outside of a class.
Functions in Java need to be declared in a class. Either, you've accidently close off the previous class by having one too many } (in which case you should have an error at the extra }), or you haven't declared a class.
The class could look something like this:
public class MyPoster {
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}

Categories

Resources