Send HttpPost with Async and get string result - android

I am relatively a new Android developer and I am not able to understand how to do this. I have been looking through all the forums, I made some advance but still here I am.
So, what I want to do is a common function that send a POST request to a webpage (it only sends one POST argument) and returns the result as a string.
I have the main thread here
public class AppActivity extends Activity {
HTTPPostData PostData = new HTTPPostData("id");
PostData.execute();
txtLabel.setText(PostData.Result);
}
and I have my HTTPPostData asynchronous class
public class HTTPPostData extends AsyncTask<String, Long, Object> {
String Value = null;
String Result = null;
public HTTPPostData(String query) {
Value = query;
}
#Override
protected String doInBackground(String... params) {
byte[] Bresult = null;
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.mypage.com/script.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("cmd", Value));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
HttpResponse response = client.execute(post);
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpURLConnection.HTTP_OK){
Bresult = EntityUtils.toByteArray(response.getEntity());
Result = new String(Bresult, "UTF-8");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (Exception e) {
}
return Result;
}
}
I want to use this function several times (inside the same Activity or share it with other Activities of the same application). I am a little bit messed up at this moment so I need your help. What I understand is that I am asking for the result before the doInBackground() is done, and I get an empty result.
Thanks in advance for your help

Regarding this:
HTTPPostData PostData = new HTTPPostData("id");
PostData.execute();
txtLabel.setText(PostData.Result);
Your problem is that you're treating asynctask like it's just a regular function. It's good that you move webpage loading off the main thread, but if you depend on the result for the very next instruction, then it's not doing you much good, you're still blocking the main program waiting for the result. You need to think of AsyncTask like a 'fire and forget' operation, in which you don't know when, if ever, it will come back.
The better thing to do here would be something like:
HTTPPostData PostData = new HTTPPostData("id");
PostData.execute();
txtLabel.setText("Loading...");
and then in the asynctask:
protected void onPostExecute(String result) {
txtLabel.setText(result);
}
This lets your main thread get on with doing it's business without knowing the result of the asynctask, and then as soon as the data is available the asynctask will populate the text label with the result.

Related

Proper way to call web service using rest in android?

Please suggest any solution?
I'm new to android.I'm getting error while invoking web service
W/System.errīš• java.io.IOException: Method Not Allowed
Here is my activity which calls a web service(method) which takes one string parameter and gives output. I'd be glad if anybody posts code snippet using asynctask becos it is most preferred way to call service in android....
public class closingBalance extends ActionBarActivity {
protected final String NAMESPACE = "http://xxxxx/";
protected final String METHOD_NAME = "getReportDetails";
protected final String URL = "http://xxxxx?wsdl?shop_num=12345";
HttpClient client = new DefaultHttpClient();
try {
HttpResponse res = client.execute(new HttpGet(URL));
StatusLine line = res.getStatusLine();
if(line.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
res.getEntity().writeTo(out);
String response = out.toString();
System.out.println(response);
out.close();
}else{
res.getEntity().getContent().close();
throw new IOException(line.getReasonPhrase());
}
} catch (IOException e) {
e.printStackTrace();
}
}
I advise you to research the Volley tool can be very useful to you.
Transmitting Network Data Using Volley is very simple
https://developer.android.com/training/volley/index.html
Consider using Retrofit library.
Here is a comparison between AsyncTask, Volley and Retrofit

How to login and keep cookie for later use of a webpage

So, I have this webpage which I want to access, but first I have to login from another webpage. I want to keep the cookies and then use it for later automatic login. So far what I did:
First, this is the login webpage: https://autenticacao.uvanet.br/autenticacao/pages/login.jsf
It's my university's student's area.
public class Consulta extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... urls) {
StringBuilder builder = new StringBuilder(100000);
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(urls[0]);
try {
List<NameValuePair> val = new ArrayList<NameValuePair>(2);
val.add(new BasicNameValuePair("form:usuario", "myusername"));
val.add(new BasicNameValuePair("form:senha", "mypass"));
httpPost.setEntity(new UrlEncodedFormEntity(val));
HttpResponse response = client.execute(httpPost);
InputStream content = response.getEntity().getContent();
BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
builder.append(s);
}
} catch (Exception e) {
e.printStackTrace();
}
return builder.toString();
}
}
This is the class I use to make the HttpPost and this is how I call it:
#Override
public void onClick(View v) {
try{
String html = new Consulta().execute("https://autenticacao.uvanet.br/autenticacao/pages/login.jsf").get();
Document doc = Jsoup.parse(html);
Element link = doc.select("title").first();
String t = link.text();
tv1.setText(t);
}catch(Exception e){
e.printStackTrace();
}
}
I believed it would work this way:
I send the webpage to login to Consulta.java
The class would get the fields "form:usuario" and "form:senha" and fill them with myusername and mypassword and then login
The class would return me html code of the second webpage as string
But what happens is that it returns me the first webpage (the login one). I'm pretty sure I'm doing something wrong, but I don't know what, could someone help me? Also, sorry for my english, it's not my main language.
When you do the login (in https://autenticacao.uvanet.br/autenticacao/pages/login.jsf), I don't think the response is the html code of the second webpage. Are you sure about this?
I think the normal behavior for a login page is to respond with the same page (the login one) but adding the session cookie and the header to do a redirect to the second webpage, but not the second page itself.
In this case, you have to read the http header response to extract these parameters: the cookies and the URL of the second webpage.
Using the object HttpResponse:
Header[] h = response.getAllHeaders();
But I recommend you to use HttpURLConnection class instead of DefaultHttpClient.

When running 2 http AsyncTasks in parallel sometimes results are exchanged

I have a strange problem in my application.
I have an activity that fetches 2 or 3 things in parallel using AsyncTasks
When I simply do the following
new getMessages().execute("someID");
new getNotifications().execute("someID");
and the both AsyncTasks has code as follows:
(Both of them are identical except that the URL requested by each method is different).
Notice: I modified this code a little bit just to remove any unneeded extras like extra parameters sent in the http request
// in the other Async task "notifications" is changed with "messages"
public class getNotifications extends AsyncTask<String, Void, String> {
ProgressDialog dialog;
Integer verified = 0;
protected void onPreExecute() {
}
#Override
protected String doInBackground(String... args) {
// getRequestsCount just perfomrs http request and grabs JSON data
String result = getRequestsCount(args[0] , "notifications");
return result;
}
protected void onPostExecute(String result) {
// This is just a method that handles the result
// When I log result I found that results are exchanged.
displayResults(result);
}
}
public String getRequestsCount(String id, String type){
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost(GlobalSettings.apiURL + "/getcount/" + type );
Log.i("will contact",GlobalSettings.apiURL + "/getcount/" + type);
HttpResponse response = httpclient.execute(httppost);
BufferedReader reader = new BufferedReader(new InputStreamReader( response.getEntity().getContent(), "UTF-8"));
String responseMessage = reader.readLine().toString().trim();
// Response is always 1 line of JSON data
Log.i("Response for "+type,responseMessage);
return responseMessage;
}
Now my problem is that sometimes results are exchanged.
i.e. getMessages recives the JSON data requested from getNotifications
and vice versa.

Android issues with AsyncTask and InputStream

I've been trying to figure this out on my own for quite a while.. by trial/error as well as research, but I just can't seem to get it figured out. I'm pretty bad with Async and network connections and stuch, so it might be something simple that I'm over looking. Anyway... I'll paste some relevant code samples and explanations.
Quick background of my problem. I'm working with the Rotten Tomatoes API for my app, and am using GSON for the parsing of their data. I was initially targeting 2.3, and this worked fine. Then I decided to have support for ICS, and of course ran into the "no network operation on the UI thread" - so I started to delve into AsyncTask.
Here is my InputStream method:
private InputStream retrieveStream(String url) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(url);
try {
HttpResponse getResponse = client.execute(getRequest);
final int statusCode = getResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w(getClass().getSimpleName(),
"Error " + statusCode + " for URL " + url);
return null;
}
HttpEntity getResponseEntity = getResponse.getEntity();
return getResponseEntity.getContent();
}
catch (IOException e) {
getRequest.abort();
Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
}
return null;
}
Which was working fine in my main activity, and now is giving me issues when trying to 'convert' it into AsyncTask. I've been calling it like this:
InputStream source = retrieveStream( url parameter );
Then I tried moving that method into my AsyncTask class, and calling it like this:
private PerformMovieSearch performSearch = new PerformMovieSearch(this);
InputStream source = performSearch.retrieveStream(movieQueryUrl);
But that doesn't cut it, still get the error about performing network actions on the UI. What I need to figure out is how to call 'retrieveStream' from the AsyncTask I guess. Currently that class looks like this:
package net.neonlotus.ucritic;
[imports]
public class PerformMovieSearch extends AsyncTask<String, Void, String> {
private final Context context;
private ProgressDialog progressDialog;
public PerformMovieSearch(Context context){
this.context = context;
}
#Override
protected String doInBackground(String... urls) {
retrieveStream(urls[0]);
return null;
}
#Override
protected void onPreExecute() {
progressDialog= ProgressDialog.show(context, "Please Wait","Searching movies", true);
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progressDialog.dismiss();
MyActivity.mListAdapter.notifyDataSetChanged();
}
public InputStream retrieveStream(String url) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(url);
try {
HttpResponse getResponse = client.execute(getRequest);
final int statusCode = getResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w(getClass().getSimpleName(),
"Error " + statusCode + " for URL " + url);
return null;
}
HttpEntity getResponseEntity = getResponse.getEntity();
return getResponseEntity.getContent();
} catch (IOException e) {
getRequest.abort();
Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
}
return null;
}
}
The "doinbackground" is what needs to be changed... but I can't seem to find a straight way to get that working properly. I was executing using
new PerformMovieSearch(this).execute(movieQueryUrl);
I know that is a lot of stuff, potentially confusing... but if anybody knows how to essentially do the retrieveStream method asynchronously, that would be great. Like I said, Ive tried many things, did plenty of research, just could not come up with anything useful.
the point is, you didn't understand how asynctask works!
You MUST read the guide Processes and Threads: http://developer.android.com/guide/components/processes-and-threads.html
But ok, let me try help you.
On doInBackground you are correctly calling the method retrieveStream, but you are doing nothing with the stream. So, you have to process the stream and then, return it. As you said you are expecting an JSON, I'm assuming you will receive a String, so the code of your retrieveStream should like this:
public String retrieveStream(String url) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(url);
try {
HttpResponse getResponse = client.execute(getRequest);
final int statusCode = getResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w(getClass().getSimpleName(),
"Error " + statusCode + " for URL " + url);
return null;
}
HttpEntity getResponseEntity = getResponse.getEntity();
String jsonString = EntityUtils.toString(getResponseEntity);
return jsonString;
} catch (IOException e) {
getRequest.abort();
Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
}
return null;
}
Look that I changed the return type to String. And maybe, you should change the name to retrieveMoviesJSON or something like this.
And you should change your AsyncTask to something like this:
class PerformMovieSearch AsyncTask<String, Void, ArrayList<Movie>>() {
#Override
protected void onPreExecute() {
progressDialog= ProgressDialog.show(context, "Please Wait","Searching movies", true);
}
#Override
protected ArrayList<Movie> doInBackground(String... params) {
String moviesJson = retrieveStream[params[0]];
JSONObject moviesJson = new JSONObject(moviesJson);
ArrayList<Movie> movies = new ArrayList<Movie>();
/*
* Do your code to process the JSON and create an ArrayList of films.
* It's just a suggestion how to store the data.
*/
return movies;
}
protected void onPostExecute(ArrayList<Movie> result) {
progressDialog.dismiss();
//create a method to set an ArrayList in your adapter and set it here.
MyActivity.mListAdapter.setMovies(result);
MyActivity.mListAdapter.notifyDataSetChanged();
}
}
And you can call as the same way you were doing.
Is it clear? Need more explanation?
[]s
Neto
What sort of behavior are you seeing that is unexpected. From scanning your code, it looks like it probably compiles and runs but I would guess that your ListAdapter never gets updated with fresh data (i.e. you're probably trying to display the results in a ListView or GridView but nothing is showing up). Is that correct? Or are you still getting the Network on Main Thread error?
You are retrieving data using your HTTP Client and then not doing anything with it. One way you could solve it is to structure your code such that:
1) Your class that extends AsyncTask has a constructor that takes a ListAdapter object
2) Your main Activity would create an instance of the AsyncTask and pass in a reference to its ListAdapter object
3) Your doInBackground method would handle all the network activity and return the result (the data you pulled from the web service) so that it gets passed down to the onPostExecute method
4) In onPostExecute, you will have the data that was returned from doInBackground, and you'll have the ListAdapter that was provided in the constructor, so parse the data, populate the ListAdapter, and invalidate it so that the list gets redrawn.
Keep in mind that AsyncTask lets you interact with the UI thread in both the onPreExecute and onPostExecute methods, so those are the only places that you can draw to the screen (i.e. populating the adapter and invalidating it so that it will redraw)

How and where to call my Task when extending AsyncTask

I am having difficulties understanding AsyncTask, even after reading everything about it on Developer.Android. I am looking for some insight in how I should proceed. This is the situation :
I have an Activity which, on an onClick event calls the LoginCheck() method of an underlying LoginController class. The LoginController class then proceeds to fetch whatever information is nescesarry from a UserInfo class or from the Activity(User and Password) and creates an instance of a RestClient which then makes the call to the web service and attempts to log in. RestClient has a private class CallServiceTask that extends AsyncTask.
I have a few design problems here that I hope you can be of assistance with.
Am I doing it right? Is this a proper way to make sure that any calls to the web service are being done asynchronously?
How do use onProgressUpdate or whatever to notify the user that the application is in the process of logging in?
How would I go about getting the data that is saved in DoinBackground() ?
Below you'll find snippets of the project in question :
RestClient
// From the constructor...
rtnData = new Object[]{ new JSONObject() , Boolean.TRUE };
public void ExecuteCall(RequestMethod method) throws Exception
{
Object[] parameters = new Object[]{ new HttpGet() , new String("") };
switch(method) {
case GET:
{
//add parameters
String combinedParams = "";
if(!params.isEmpty()){
combinedParams += "?";
for(NameValuePair p : params)
{
String paramString = p.getName() + "=" + URLEncoder.encode(p.getValue());
if(combinedParams.length() > 1)
{
combinedParams += "&" + paramString;
}
else
{
combinedParams += paramString;
}
}
}
HttpGet request = new HttpGet(url + combinedParams);
//add headers
for(NameValuePair h : headers)
{
request.addHeader(h.getName(), h.getValue());
}
parameters[0] = request;
parameters[1] = url;
new CallServiceTask().execute(request, url);
jsonData = ((JSONObject) rtnData[0]).optJSONObject("data");
connError = (Boolean) rtnData[1];
break;
}
case POST: ....
}
}
private Object[] executeRequest(HttpUriRequest request, String url)
{
HttpClient client = new DefaultHttpClient();
client = getNewHttpClient();
HttpResponse httpResponse;
try {
httpResponse = client.execute(request);
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
String response = convertStreamToString(instream);
try {
rtnData[0] = new JSONObject(response);
rtnData[1] = false;
} catch (JSONException e1) {
rtnData[1] = true;
e1.printStackTrace();
}
// Closing the input stream will trigger connection release
instream.close();
}
} catch (ClientProtocolException e) {
client.getConnectionManager().shutdown();
e.printStackTrace();
} catch (IOException e) {
client.getConnectionManager().shutdown();
e.printStackTrace();
}
return rtnData;
}
CallServiceTask
private class CallServiceTask extends AsyncTask<Object, Void, Object[]>
{
protected Object[] doInBackground(Object... params)
{
HttpUriRequest req = (HttpUriRequest) params[0];
String url = (String) params[1];
return executeRequest(req, url);
}
#Override
protected void onPostExecute(Object[] result)
{
rtnData = result;
}
}
It's absolutely right that any possibly long running operations should be executed in separate threads. And the AsyncTask is a good way to solve this kind of problems, since it also gives you an easy way to synchronize your task with the UI thread. This is the answer to your first question.
Now, concerning the UI thread updating to show your users that your application is not stuck. Since an AsyncTask's onPreExecute() and onPostExecute() methods are running inside the UI thread, you can easily create, run and stop ProgressDialogs or ProgressBars there. If you want to show the current progress of the task, you should call publishProgress(int) method inside the doInBackground(), and then make use of it inside the AsyncTask's onProgressUpdate() method. There you can, for example, update your ProgressDialog.
And to get the result out of your AsyncTask you can either call its get() method (this a synchronous call), or implement some kind of callback interface that will tell the activity that the task has finished.
I hope the answer is clear enough, if no - feel free to ask more questions. Hope this helps.
EDIT
Create an interface called, for example, onFetchFinishedListener with one method - void onFetchFinished(String). Your activity, that starts the AsyncTask, must implement this interface. Now create a constructor inside your AsyncTask that takes an OnFetchFinishedListener object as an argument, and when instantiating the AsyncTask inside your activity send a reference to the Activity as the argument (since it implements OnFetchFinishedListener). Then when your task is finished inside doInBackground() call onFetchFinished() on the activity. Now inside the onFetchFinished(String) method of your Activity you can make use of the String (or another object) that's brought with the callback. Again, hope I was clear enough.

Categories

Resources