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
}
}
Related
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
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
I am using Commons HttpClient to send a post request along with some string content as parameter. Following is my code:
// obtain the default httpclient
client = new DefaultHttpClient();
// obtain a http post request object
postRequest = new HttpPost(stanbolInstance);
postRequest.setHeader("Accept", "application/json");
// create an http param containing summary of article
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("data", content));
try {
// add the param to postRequest
postRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
// obtain the response
response = client.execute(postRequest);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Here, stanbolInstance is: http://dev.iks-project.eu:8081/enhancer
It does not work. Following is the exception:
Problem accessing /enhancer. Reason:
<pre> The parsed byte array MUST NOT be NULL!</pre></p><h3>Caused by:</h3><pre>java.lang.IllegalArgumentException: The parsed byte array MUST NOT be NULL!
Following is the cURL equivalent which works:
curl -X POST -H "Accept: text/turtle" -H "Content-type: text/plain" --data "The Stanbol enhancer can detect famous cities such as Paris and people such as Bob Marley." http://dev.iks-project.eu:8081/enhancer
Help!
I think you're putting the content in the wrong way.
Replace:
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("data", content));
try {
// add the param to postRequest
postRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
with
postRequest.setEntity(new StringEntity(content));
I'm new to http file transfer. I want to send a file from android sdcard to server. For that i tried the below code. I converted the bytes to json string and sent it to the server. But I'm unable to receive it on the server side. I'm using jsp on server side. But there should be some efficient way to do this. Please provide me some ideas.
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2:8084/httptest");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
String encodedString = convertURL(jsonString);
nameValuePairs.add(new BasicNameValuePair("wavfil", encodedString));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
String responseString = EntityUtils.toString(entity, "UTF-8");
Toast.makeText(this, responseString, Toast.LENGTH_SHORT).show();
tv.setText(responseString);
Log.d("HTTP LOG", responseString);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
jsp
<%
String value = request.getParameter("wavfil");
byte[] wavByte = value.getBytes();
FileOutputStream fos = new FileOutputStream("/TESTFILE.wav");
fos.write(wavByte, 0, wavByte.length);
if (wavByte != null) {
out.println("Success");
} else {
out.println("Failed");
}
%>
Add below lines
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.x.x.x:8084/httptest");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
String encodedString = convertURL(jsonString);
nameValuePairs.add(new BasicNameValuePair("wavfil", encodedString));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Create and attach file to the Post
File file = new File("pathto your file"); //replace with actual path
MultipartEntity entity = new MultipartEntity();
httppost.setEntity(entity);
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
entity.addPart("file", new FileBody(file));
if (entity != null) {
String responseString = EntityUtils.toString(entity, "UTF-8");
Toast.makeText(this, responseString, Toast.LENGTH_SHORT).show();
tv.setText(responseString);
Log.d("HTTP LOG", responseString);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
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
}
}
}