Large data is not sending in android through webservice - java

I am using the following code in android to send data to a server through a web service
call.When i am sending small amount of data it is hitting the server.When i am sending large data it is not hitting the server.Simply it is httpClient.execute(httpPost); .But i am not getting any result.What might be the problem
HttpPost httpPost = new HttpPost(url+data);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
if (rsopnse != null)
System.out.println(httpPost.getMethod());
try
{
httpResponse= httpClient.execute(httpPost);
}catch(Exception e)
{
e.printStackTrace();
}
Thanks in advance...

You need to create a List<NameValuePair> for all the parameters that you want to pass in the Request. You should not append your parameters to the URL, which is more of the GET style of making a call.
The examples for HTTP Post are covered in the post here.

Related

Firebase Dynamic Link Rest API - 400 Bad Request

I am transitioning an existing service from using google url shortener api to try and use Firebase Dynamic Links. I have linked a project from the Google Cloud Platform, and setup a "dummy" android app so that I can have the app domain for the dynamic links. I am trying to use the REST API to shorten urls for very long urls that can't be handled by a third party. I have tried sending using:
ObjectMapper mapper = new ObjectMapper();
HttpPost httpPost = new HttpPost("https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=****");
FirebaseDynamicLinkInfo dynamicLinkRequest = new FirebaseDynamicLinkInfo();
dynamicLinkRequest.setDynamicLinkDomain("zw5yb.app.goo.gl");
dynamicLinkRequest.setLink(assetUrl);
httpPost.setEntity(new StringEntity(mapper.writeValueAsString(dynamicLinkRequest)));
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
responseBody = httpClient.execute(httpPost, responseHandler);
I am getting a 400 Bad Request when I post the request to the API (on the httpCLient.execute line. I have double checked my api-key. I have also tried using just the longDynamicLink parameter, and it gets the 400 Bad Request Response.
Any ideas of where I could be going wrong?
Thanks,
Ben
I contacted Google Support on this one, and I wasn't UrlEncoding my querystring parameters on the deep link. After encoding the link, the request was successful. I went back to using passing json that just had a longDynamicLink property (as opposed to the dynamicLinkInfo object in my original post). Here is what it looks like:
String myEscapedUrl = "https://zw5yb.app.goo.gl/?link=" + URLEncoder.encode(assetUrl, "UTF-8");
FirebaseDynamicLinkRequest dynamicLinkRequest = new FirebaseDynamicLinkRequest(myEscapedUrl);
httpPost.setEntity(new StringEntity(mapper.writeValueAsString(dynamicLinkRequest)));
// inform the server about the type of the content
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
responseBody = httpClient.execute(httpPost, responseHandler);

Does anyone have an updated example on posting a JSON request?

I am having a mess of a time finding up to date information on sending a JSON request to a local server. I keep coming across examples that use deprecated code, and I'd really like to do this with code that isn't.
I can at least say that I now have a working example, and I am not receiving any deprecated messages from NetBeans, but I would like to know if what I've put together is the right way:
public void sendUpdateRequest() {
String updateString =
"{\"jsonrpc\": \"2.0\", \"method\": \"VideoLibrary.Scan\"}" ;
StringEntity entity = new StringEntity(updateString, Consts.UTF_8);
HttpPost httpPost = new HttpPost(getURL()); // http://xbmc:xbmc#10.0.0.151:8080/jsonrpc
entity.setContentType("application/json");
httpPost.setEntity(entity);
try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
HttpResponse response = client.execute(httpPost);
System.out.println(response.getStatusLine()); // move to log
}
catch (IOException e) {
e.printStackTrace(); // move to log
}
}
This is something I'm working on to update XBMC with a JSON HTTP request
Edit
Changed the code to try with resources per the comment -- hopefully this will be useful for someone else dealing with JSON and Java
but I would like to know if what I've put together is the right way:
Yes, you are doing it correctly given the details you've posted.
The StringEntity contains the body of the request. You can set any appropriate headers there. Any other headers can be set directly on the HttpPost object.
As stated in the comments, don't take any chances, close() the CloseableHttpClient in a finally block.

Passing ArrayList from Java to PHP webservice

I have a java program that contains a username and passwords (strings) and an ArrayList of objects with 4 attributes (long, int, int int) and I want to pass these 3 things to a WebService (that I have yet to make). My host is Bluehost and it's a shared server so I won't have Java available server side it will need to be in PHP.
What is the best way of connecting to the webservice and passing this into php?
EDIT.
OK so I now have something like this:
public void upload(ArrayList<MyObject> myList) throws Exception{
//HTTP POST Service
try{
HttpClient httpclient = HttpClientBuilder.create().build();
URI uri = new URIBuilder()
.setScheme("http")
.setHost("www.myHost.com")
.setPath("/myWebservice.php")
.setUserInfo(userID, password)
.build();
HttpPost httppost = new HttpPost(uri);
httpclient.execute(httppost);
}catch (Exception e) {
e.printStackTrace();
}
}
But I'm still not sure how I can pass the ArrayList in a way that I'll be able to receive and split it into it's components on the PHP side?
You can use an HTTP client e.g. this one.
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/index.html
and send a GET/POST request to your WebService.

Calling Soap webservice from android

I am using the below process,
1)Create a String template for a SOAP request and substitute user-supplied values at runtime in this template to create a valid request.
2) Wrap this string in a StringEntity and set its content type as text/xml
3) Set this entity in the SOAP request.
and with the help of httppost I am posting the request,
I am using a demo webservice from w3schools.com
url--->
http://www.w3schools.com/webservices/tempconvert.asmx
What I have tried is,
HttpPost httppost = new HttpPost("http://www.w3schools.com/webservices/tempconvert.asmx");
StringEntity se;
try {
SOAPRequestXML="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\"><soapenv:Header/><soapenv:Body><tem:CelsiusToFahrenheit><!--Optional:--><tem:Celsius>30</tem:Celsius></tem:CelsiusToFahrenheit></soapenv:Body></soapenv:Envelope>";
Log.d("request is ", SOAPRequestXML+"!!!");
se = new StringEntity(SOAPRequestXML,HTTP.UTF_8);
se.setContentType("text/xml");
httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8");
httppost.setEntity(se);
HttpClient httpclient = new DefaultHttpClient();
BasicHttpResponse httpResponse =
(BasicHttpResponse) httpclient.execute(httppost);
HttpEntity resEntity = httpResponse.getEntity();
t.setText(EntityUtils.toString(resEntity));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I am able to get the response in soapui, so surely the code is wrong because in emulator I am getting the output,
"the server cannot service the request because the media type is unsupported".
Am I passing the correct parameter in the constructor of HttpPost or am I making the correct xml request.I tried a lot but could not figure it out.
Thanks
The only problem with your code is you are setting Header as,
httppost.setHeader("Content-Type","application/soap+xml;charset=UTF-8");
instead of,
httppost.setHeader("Content-Type", "text/xml;charset=UTF-8");
As you can see the request in the URL that is -> w3schoools, they are using,
Content-Type: text/xml; charset=utf-8
and you where not passing the same content type. So, it was giving you error as,
The server cannot service the request because the media type is unsupported.
So, just change the Header and you will get the desired response.
I have written an article on How to Call Web Service in Android Using SOAP at c-sharpcorner.com.
So many person get helped from that article. You can also download it and run. I will help you to understand how to use SOAP for web service.
Edit
Take a look at following links. It has complex data handling with ksoap.
Complex objects tutorial with sample code
http://bimbim.in/post/2010/10/08/Android-Calling-Web-Service-with-complex-types.aspx
http://seesharpgears.blogspot.in/2010/10/web-service-that-returns-array-of.html
I have a hunch that the emulator android version and the phone version are different.
But I have few suggestions. Use following:
httppost.setHeader("Accept-Charset","utf-8");
httppost.setHeader("Accept","text/xml,application/text+xml,application/soap+xml");
similarly, set content type as all of the above.
Have you tried using the ksoap2 library for Android ?
you can find it here, give it a shot :
https://code.google.com/p/ksoap2-android/
Hope this helps !
This way the html form is posting 123 celsius. No SOAP or envelops, just working:)
try {
HttpPost httppost = new HttpPost("http://www.w3schools.com/webservices/tempconvert.asmx/CelsiusToFahrenheit");
StringEntity se = new StringEntity("Celsius=123");
httppost.setEntity(se);
httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
HttpResponse httpResponse = new DefaultHttpClient().execute(httppost);
HttpEntity resEntity = httpResponse.getEntity();
return EntityUtils.toString(resEntity);
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}

Using HTTP Post on an android device to put data on google app engine's blob store

I am trying to post data to the Blob Store on google's app engine, this code runs without throwing any exceptions, but on the blobstore end there is no log on the post request at all. The server side stuff works when i post using a form (albeit with mime data). I have allowed my android app to use internet. This is a stab in the dark but if any of you folks might have had an issue like this before perhaps the problem i am having might ring a bell!
public void sendVideo() throws IOException {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://www.theurliampostingto.com/au813rsadjfaruh);
// Add your data
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("key1", "value1"));
pairs.add(new BasicNameValuePair("key2", "value2"));
httpPost.setEntity(new UrlEncodedFormEntity(pairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httpPost);
}
You can try to intercept the traffic between the emulator and the server i.e. with WireShark to see if the server is responding to your request at all.
Your code looks good for me.

Categories

Resources