I want to create an Application, the description of it as follows
Perform search
Display the results.
Basically, I want to create an application for an existing web-site which does this, but my need is to create for a Mobile.
Based on the website link, it refers to some ASP pages. for e.g. http://test.com/query.asp. The website allows the user to enter various input for search.
I want to create and then send the same request to the server and get the result. How can I know what all parameters/headers it is taking and what format is the response (XML or JSON).
Below is my sample code for the Android Device
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
List nameValuePairs = new ArrayList(3);
//this is where you add your data to the post method
nameValuePairs.add(new BasicNameValuePair("sel", "1"));
nameValuePairs.add(new BasicNameValuePair("txt", "2466"));
nameValuePairs.add(new BasicNameValuePair("selr", "2011"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httpPost);
content = response.getEntity().getContent();
return content;
// Update
Ok, now I am able to get the desired output from the web query.
I printed the result for my Http Post request and I saw some HTML tags in it. In short, it prints the source of the HTML page that has to be displayed.
Is there a way to display the HTML view in my Android Application? Or a way to fetch only the relavant data from the response and ignore the HTML tags etc..?
First line of the response looks like
06-17 16:35:21.756: DEBUG/(30307): <script language="javascript" type=text/css>
//
Regards,
Nirav
You need to speak with the owner/developer of the web site / service that you want to access. Only they can reliably tell you what their APIs are if you are unable to determine them yourself from WireShark captures.
Related
Currently I am connecting from android to a .net WEB API using HttpClient and I have been able to do a GET and POST to read/write data. However I want to do an Update and a Delete.
I tried to do this using a POST, but it simple creates more records. Here is my code for the POST, how would I change it to do a PUT or DELETE instead?
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://mywebsite.net/api/employees/6");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
nameValuePairs.add(new BasicNameValuePair("firstName", "UpdatedHello"));
nameValuePairs.add(new BasicNameValuePair("lastName", "World"));
nameValuePairs.add(new BasicNameValuePair("employee_name", "UpdatedHello World"));
nameValuePairs.add(new BasicNameValuePair("password", "xxx"));
nameValuePairs.add(new BasicNameValuePair("isActive", "1"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
yeah! the document of httpClient http://hc.apache.org/httpclient-3.x/methods.html
You have PutMethod and DeleteMethod API for performing PUT and DELETE Http requests. The sample usage as follows as per doc
PUT Request - The put method is very simple, it takes a URL to put to and requires that the body of the request method be set to the data to upload. The body can be set with an input stream or a string.This method is generally disabled on publicly available servers because it is generally undesireable to allow clients to put new files on the server or to replace existing files.
PutMethod put = new PutMethod("http://jakarta.apache.org");
put.setRequestBody(new FileInputStream("UploadMe.gif"));
// execute the method and handle any error responses.
...
// Handle the response. Note that a successful response may not be
// 200, but may also be 201 Created, 204 No Content or any of the other
// 2xx range responses.
DELETE Request - The delete method is used by supplying a URL to delete the resource at and reading the response from the server.This method is also generally disabled on publicly available servers because it is generally undesireable to allow clients to delete files on the server.
DeleteMethod delete = new DeleteMethod("http://jakarata.apache.org");
// execute the method and handle any error responses.
...
// Ensure that if there is a response body it is read, then release the
// connection.
...
delete.releaseConnection();
I want to send data from my app to http://studentssp.wit.ie/Timetables/POSTT.aspx that page and I'm not sure how to do that. All that I have tried atm is adding some of the form values to the url to see if that changes anything, but it hasn't. e.g. I put cboSchool=EP (the name value of the select is cboSchool and the value of the School of Adult Education is EP) into the url to see if it would only load information regarding that school. http://studentssp.wit.ie/Timetables/POSTT.aspx?cboSchool=EP
I'm knew to android and know nothing about aspx.
This should get your started:
HttpClient httpClient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), TIMEOUT_MS);
HttpConnectionParams.setSoTimeout(httpClient.getParams(), TIMEOUT_MS);
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name1", "value1"));
nameValuePairs.add(new BasicNameValuePair("name2", "value2"));
nameValuePairs.add(new BasicNameValuePair("name3", "value3"));
// etc...
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
I think that should work for what you're trying to do. I have TIMEOUT_MS set to 10000 (so, 10 seconds)
Then you can read out the server's response using something like this:
BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent
1- you talk about mobile application based on web , so if you need to interact with server side , you need web service .
please check the web service you need to integrate with site you need .
2- most of web service will be (Restful format ,SOAP).
Restful is easier , you can parse data with any open source parse SAX , DOM for XML format ,
or Json Parser for json .
i will give you simple example :
this is a currency converter API , and we will sent the parameters in URL this is mean http Get request ,
Like :
http://www.google.com/ig/calculator?hl=en&q=100GBP=?USD
there is "?hl=en&q=1GBP=?USD "
any parameter have 2 parts is 1-key 2- value .
h1 is key , en is value .
q is key , 1GEP=?USD is value .
and the respone will be in json format Like
{lhs: "100 British pounds",rhs: "161.47 U.S. dollars",error: "",icc: true}
you can see this is link for parsing json .
i hope this help you?
I recently came across strange problem when sending / recieving data thru http POST request on Android.
I had difficulties with setting Fiddler to monitor the traffic between my Android app and server so I created simple web form to simulate the POST request.
<form action="http://www.my.server.org/my_script.php" method="POST">
<input name="deviceID" type="text" width=30> Device ID <br>
<input name="lang" type="text" width=30> Language (en / cs) <br>
<input name="lastUpdated" type="text" width=30> Last Updated (yyyy-MM-dd hh:mm) <br>
<button type="submit">Send</button>
</form>
When I send the request using this form, a response is delivered back with 200 OK status code and desired XML file.
I thought it would be equivalent to Java code I have in my Android app.
private static final String POST_PARAM_LAST_UPDATED = "lastUpdated";
private static final String POST_PARAM_DEVICE_ID = "deviceId";
private static final String POST_PARAM_LANG = "lang";
...
// Create a POST Header and add POST Parameters
HttpPost httpPost = new HttpPost(URL_ARTICLES);
List<NameValuePair> postParameters = new ArrayList<NameValuePair>(3);
postParameters.add(new BasicNameValuePair(POST_PARAM_DEVICE_ID, deviceId));
postParameters.add(new BasicNameValuePair(POST_PARAM_LANG, lang));
postParameters.add(new BasicNameValuePair(POST_PARAM_LAST_UPDATED, lastUpdated));
httpPost.setEntity(new UrlEncodedFormEntity(postParameters));
// Create new HttpClient and execute HTTP Post Request
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(httpPost);
// Get and parse the response
List<Article> parsedArticles = new ArrayList<Article>();
HttpEntity entity = response.getEntity();
if (entity != null) {
parsedArticles = Parser.parseArticles(entity.getContent());
}
However even when I put the same parameter values (as those I put in the web form), the response in this case is 204 NO CONTENT and no XML file obviously.
Can somebody here please tell me how come these two methods are not equivalent and the responses are different? Is it something with encoding or what am I missing?
I unfortunately don't have access to the server and I'm not able to debug Android outgoing and incoming data because Fiddler and my emulator / device connected to PC refused to cooperate.
And I also wondered if I should use AndroidHttpClient instead of DefaultHttpClient but I think it's not going to change anything in this case.
Thanks in advance!
Due to Maxims comment I found out what's wrong.
It was one stupid lower case letter in the POST_PARAM_DEVICE_ID constant. It's value was "deviceId" (and should be "deviceID" as in web form).
Well, my fellow developers, pay attention when defining String keys - it's case sensitive!
I have a site that I would like to log into. Once there, I would parse the code to display member data (like any other login app). I have sort of pulled this code from somewhere and I'd like to know why it's crashing. Basically, I have two login inputs, username and password. I'll take them from the user but as of now I'm just inputting random credentials for testing. At the end, I want to get it to the login page (same url once logged in) and display the HTML, for now.
Here's my code so far:
HttpClient httpClient = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), TIMEOUT_MS);
HttpConnectionParams.setSoTimeout(httpClient.getParams(), TIMEOUT_MS);
HttpPost httpPost = new HttpPost("login url"); // Removed for StackOverflow question
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("sid", "name"));
nameValuePairs.add(new BasicNameValuePair("pin", "pass"));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()), 8096);
tvStatus.setText((CharSequence) br);
Are you running the POST request on the UI thread? This causes the UI to freeze and after a certain time period, the OS considers the app to be non-responsive and force closes it. Web Requests should always be an Async task
I guess this post on my blog will help you to understand, how to use AsyncTask, for downloading stuff.
Though if you tell us what is the Exception which is getting thrown up, I could've helped you more on this. To see the Exception, open the LogCat,identify the error text which will be in red, copy the whole red text and paste it into your question.
I'm trying to write a java code on an Android emulator that will send a string to a web service writen in c#.
Android code:
HttpPost httppost = new HttpPost("http://192.168.2.1:53811/WinnerSite/WebService.asm/MyMethod
try {
// Add your data
List nameValuePairs = new ArrayList(2);
nameValuePairs.add(new BasicNameValuePair("json", name));
// nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
Also tried:
HttpPost httppost = new HttpPost("http://192.168.2.1:53811/WinnerSite/WebService.asm/MyMethod
The web-service is on the same machine as the emulator. MyMethod is accessable through:
http://localhost:53811/WinnerSite/WebService.asmx/MyMethod
Does someine ahs an idea?
The code exits on the "httpclient.execute(httppost);" line
The eclipse shows:
"ActivityThread.prefo
Source not found."
I have already solve a persmission problem (adding a note to the emolator's xml)
Thanks,
When you want to use the network, you should add network access permission in your AndroidManifest.xml.
Your problem seems to be complex. Check your client app and web service separately to assure they are both correct.
Your codes posted seems to be correct. But your error message "ActivityThread.prefo Source not found." is too weak... Please provide more info.