Issue
I created a class below which goes to internet and sends a request to php script. I created it like AsyncTask and not in main thread in order to work on 4.0.4, but when I test it, it doesn't work, although it works fine on 2.2. Do you know what is the problem?
class download extends AsyncTask<String, Integer, String> {
protected String doInBackground(String s1, String s2) {
String result = "";
//http post
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("Vreme", s1));
nameValuePairs.add(new BasicNameValuePair("Datum", s2));
InputStream is = null;
try {
String adresa = "http://senzori.open.telekom.rs/script.php";
HttpPost httppost = new HttpPost(adresa);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
//convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
return result;
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
return null;
}
}
Solution
It's probably not working because you have overloaded doInBackground, but not called the overloaded method.
Change it so the original method is like this:
@Override
protected String doInBackground(String... params) {
return doInBackground (params[0], params[1]);
}
Note that it now makes the overload useless, move the code back into the overriden doInBackground (String... params), and you also need to make sure that when you call execute(), you supply two Strings as the arguments.
Answered By - A--C
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.