I am trying to get text from urls and display them as strings. Urls end with .txt as in www.gains.com/more.txt These text are long and they have a maximum size of 1MB. I am trying to it with AsyncTask. The problem is that the code sometimes works. It worked the first time I ran the code the second time it didn't display the text. Sometimes the app would display the text sometimes it wouldn't. What is going on here? Here is my code.
class RequestTask extends AsyncTask<String, String, String>{
#Override
// username, password, message, mobile
protected String doInBackground(String... url) {
// constants
int timeoutSocket = 5000;
int timeoutConnection = 5000;
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
HttpClient client = new DefaultHttpClient(httpParameters);
HttpGet httpget = new HttpGet(url[0]);
try {
HttpResponse getResponse = client.execute(httpget);
final int statusCode = getResponse.getStatusLine().getStatusCode();
if(statusCode != HttpStatus.SC_OK) {
Log.w("MyApp", "Download Error: " + statusCode + "| for URL: " + url);
return null;
}
String line = "";
StringBuilder total = new StringBuilder();
HttpEntity getResponseEntity = getResponse.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(getResponseEntity.getContent()));
while((line = reader.readLine()) != null) {
total.append(line);
}
line = total.toString();
story.add(line); //story is my array i use to display the text
return line;
} catch (Exception e) {
Log.w("MyApp", "Download Exception : " + e.toString());
}
return null;
}
#Override
protected void onPostExecute(String result) {
//This is empty i dont know what it does
}
}
Here is how i call it
new RequestTask().execute("www.gains.com/more.txt");
Also another smaller problem im having is that when the text is displayed I lose the format of the text as in i lose the spaces between paragraphs an get one huge paragraph. Is there a way to solve this? Should I use another method?
Http request don't always take the same exact time. Have you tried increasing the timeout? 5000 mil seconds is not a lot, especially if your files reach 1MB in size.
Call the display function that will display the text from the file saved after the file is downloaded.
Use onPostExecute
#Override
protected void onPostExecute(String result) {
//You add the code where you call the text from the file saved
}
You can call a function from the main activity that calls the thread.
This way, the view will only be displayed until the process of downloading and saving your text is finished.
Edit: Here's a sample
TextPage textPage; // the activity that calls the AsyncTask
List<String> story = new ArrayList<String>();
GetTextInfoTask(TextPage textPage) {
this.textPage = textPage;
}
... // your doInBackground function here
#Override
protected void onPostExecute(Object objR){
// A toast is displayed in the TextPage Activity once the data is finished downloading
Toast.makeText(textPage.getBaseContext(), story.get(0),
Toast.LENGTH_LONG).show();
}
Related
When I run the AsyncTask below, I can see the log messages. However progress bar wont change visibility.
Below, is my AsyncTask code.
class test extends AsyncTask<String, Void, String> {
private Context mContext;
public PostWav(Context context) {
mContext = context;
}
protected String doInBackground(String... params ) {
URL url= null;
try {
url = new URL(params[0]);
} catch (MalformedURLException e) {
e.printStackTrace();
}
InputStream inputStream;
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
JSONObject object = new JSONObject();
try {
object.put("provider_id",1);
String message = object.toString();
HttpPost httppost = new HttpPost(url.toString());
httppost.setEntity(new StringEntity(message, "UTF8"));
httppost.setHeader("Content-type", "application/json");
httppost.setHeader("Accept", "application/json");
String boundary = "-------------" + System.currentTimeMillis();
httppost.setHeader("Content-type", "multipart/form-data; boundary="+boundary);
File file = getDir("test", Context.MODE_PRIVATE);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
File wavFile = new File(file.getPath() + "/" + params[3]);
ContentType contentType = ContentType.create("audio/x-wav");
FileBody cbFile = new FileBody(wavFile,contentType,"sample");
builder.addPart("sample", cbFile);
builder.setBoundary(boundary);
httppost.setEntity(builder.build());
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
Header[] header = response.getHeaders("Location");
String location = String.valueOf(header[0].getValue());
inputStream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
return location;
} catch (JSONException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPreExecute() {
Log.i("Async-Example", "onPreExecute Called");
loading.setVisibility(View.VISIBLE);
record.setVisibility(View.INVISIBLE);
super.onPreExecute();
}
#Override
protected void onPostExecute(String feed) {
Log.i("Async-Example", "onPostExecute Called");
loading.setVisibility(View.INVISIBLE);
record.setVisibility(View.VISIBLE);
}
}
I am uploading a .wav file to a my API. I did not included whole code this is basically all that I'm doing.
#greenapps Thanks mate your hint was more than enough for me to understand the problem.
Basically when I imported this class from another project I completely forgot the reason she was programmed like that.
I just had to remove .get() from the execute command and passed the methods to the post execute on the Task and everything when smoothly.
I am sorry for taking everyone's time, I really appreciated the help!
Here is the result i was looking for:
You would like to show the progress bar while the task is being completed, so you need to work around this statement.
Try to do the work in the following method(s):
doInBackground(int Progress)
OnProgressUpdate() to update the progress bar according to your ideas
For more read the following documentation.
Normally AsyncTask runs on the UI Thread, but I have the strong assumption that you are calling this from somewhere else than the UI Thread since you are passing the Context as a Parameter.
So you can use that.
#Override
protected void onPostExecute(String feed) {
Log.i("Async-Example", "onPostExecute Called");
activity.runOnUiThread(new Runnable(){
loading.setVisibility(View.INVISIBLE);
record.setVisibility(View.VISIBLE);
});
}
Also remove super.onPreExecute(); you dont need that
You have to change your constructor parameter from Context to Activity or it will be a bit more complicated.
I am trying to get text from urls and display them as strings. Urls end with .txt as in www.gains.com/more.txt These text are long and they have a maximum size of 1MB. I am trying to get them it with AsyncTask. The problem is that the code sometimes works and sometimes doesn't. I have no idea why.
Here is the AsyncTask code.
class RequestTask extends AsyncTask<String, String, String>{
#Override
// username, password, message, mobile
protected String doInBackground(String... url) {
// constants
int timeoutSocket = 15000; //Is this enough for 1MB files?
int timeoutConnection = 15000;
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
HttpClient client = new DefaultHttpClient(httpParameters);
HttpGet httpget = new HttpGet(url[0]);
try {
HttpResponse getResponse = client.execute(httpget);
final int statusCode = getResponse.getStatusLine().getStatusCode();
if(statusCode != HttpStatus.SC_OK) {
Log.w("MyApp", "Download Error: " + statusCode + "| for URL: " + url);
return null;
}
line = "";
StringBuilder total = new StringBuilder();
HttpEntity getResponseEntity = getResponse.getEntity();
BufferedReader reader = new BufferedReader(new InputStreamReader(getResponseEntity.getContent()));
while((line = reader.readLine()) != null) {
total.append(line);
}
line = total.toString();
return line;
} catch (Exception e) {
Log.w("MyApp", "Download Exception : " + e.toString());
}
return null;
}
#Override
protected void onPostExecute(String result) {
story.add(result); //story is the array which I use to display the downloaded text
}
}
And here is how i call it. I call it inside a method which is not OnCreate and I even do it inside a new thread to avoid the NetworkOnMainThreadException error.
new Thread() {
#Override
public void run() {
new RequestTask().doInBackground("www.gains.com/more.txt");
new RequestTask().onPostExecute(line);
}
}.start();
it's not the right way to call asynctask. You can t call doinbackground direclty, neither postexecute.
What you have to do is just to write :
RequestTask MyAsynchTask = new RequestTask().
MyAsynchTask.execute("www.gains.com/more.txt");
it s the public execute method who will call all the method for you : preexecute, doinbackground and postexecute.
and you won t have ANR problem ( Application Not Responding ) with this solution.
tell me if it's ok for you
you said : sometimes it works sometimes no ? when you write :
requestTask().doInBackground("www.gains.com/more.txt");
new RequestTask().onPostExecute(line);
java can t' know in postexecute if doinbackground is finished or not ! so sometimes you will have your result, and sometimes no, it depends of how much time java takes to work in the doinbackgroundmethod.
the only way to implement Asynchtask is to do as i write above. And if you havent the data you wish to have in the result it because of something else.
IT CAN RUN JUST ONCE : if you want to run it again you have to create antother instance of RequestTask
Instead of the following code :
new Thread() {
#Override
public void run() {
new RequestTask().doInBackground("www.gains.com/more.txt");
new RequestTask().onPostExecute(line);
}
}.start();
Use just :
new RequestTask().execute("www.gains.com/more.txt");
I've created basic android apps in various programming classes that I have taken before using Eclipse and the Java Android SDK.
The app that I'd like to create would require users to enter information that would later be analyzed. I want people to be able to compare this data with other people's data so I'd like every entry that users make to be submitted to a database to later be queried when a person attempts to compare their data.
I'd like direction for how to accomplish this. Should I find a free hosting site and set up a Sql server or is there a better way to accomplish this?
Edit: Just for fun.
I am a very beginner android developer, and I have found that using cloud-stored online database like mongolab.com is very friendly for user submitted data. The communication between database and server will have to be done through JSON parsing and URI requests.
Here is example of code you can bind to a button that will send object stored in field tempData:
public void send(View view) {
String apiURI = "https://api.mongolab.com/api/1/databases/MYDATABASE/collections/USERSUBMITTEDDATA?apiKey="
+ apiKey;
try {
// make web service connection
final HttpPost request = new HttpPost(apiURI);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
// Build JSON string with GSON library
Gson gson = new Gson();
JsonElement jsonElement = gson.toJsonTree(tempData);
String json = gson.toJson(jsonElement);
StringEntity entity = new StringEntity(json);
Log.d("****Parameter Input****", "Testing:" + json);
request.setEntity(entity);
// Send request to WCF service
final DefaultHttpClient httpClient = new DefaultHttpClient();
new AsyncTask<Void, Void, Void>() {
#Override
public Void doInBackground(Void... arg) {
try {
HttpResponse response = httpClient.execute(request);
Log.d("WebInvoke", "Saving: "
+ response.getStatusLine().toString());
// Get the status of web service
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity()
.getContent()));
// print status in log
String line = "";
while ((line = rd.readLine()) != null) {
Log.d("****Status Line***", "Webservice: " + line);
}
} catch (Exception e) {
Log.e("SendMail", e.getMessage(), e);
}
return null;
}
}.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
And here is an example of code used to retrieve elements in the database:
public void load() {
String apiURI = "https://api.mongolab.com/api/1/databases/MYDATABASE/collections/USERSUBMITTEDDATA"
+ "?apiKey=" + apiKey;
Log.d("****Status Line***", "" + apiURI);
try {
// make web service connection
final StringBuilder builder = new StringBuilder();
final HttpGet request = new HttpGet(apiURI);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
final DefaultHttpClient httpClient = new DefaultHttpClient();
new AsyncTask<Void, Void, String>() {
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
doSomethingWithReceivedData(result); //THIS METHOD IS DEFINED IN BODY OF YOUR ACTIVITY
}
#Override
public String doInBackground(Void... arg) {
try {
HttpResponse response = httpClient.execute(request);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
Log.d("****Status Line***", "Success");
return builder.toString();
} else {
Log.d("****Status Line***",
"Failed to download file");
}
} catch (Exception e) {
Log.e("SendMail", e.getMessage(), e);
}
return null;
}
}.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
You should have a data base to store the data. Like mentioned above, the data base is good to be in MySQL (SQL). Your application should have a method that can POST the results to the server, where the server will read the string send and retrieve and store the data.
A good start is to read about JSON
and read also about Asynctask
Also you need to know how to build your sever part. A good idea is to start with PHP, but I am not an expert on that field.
I hope this helps you start your project.
Simple, no DB required.
Usergrid by Apigee is exactly what you are looking for!
You can store each user's details
Retrieve stored data
Send events and receive event callbacks across devices
Best of all - no server side code. Only APIs
FYI This is the direction you should be heading even if you know how to code a server.
PS: I don't work for apigee or usergrid.
I use this code below, it works perfectly in Android 2.3.3. However, in 4.0+ it can't connect to database somehow. I saw some posts about you need to get it in a asynch class. I also tried that, but I can't seems it to work. I probably use it wrong, but it is hard for me to understand.
public class connector extends Activity {
/** Called when the activity is first created. */
TextView txt;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getServerData(null);
}
//i use my real ip here
public String getServerData(String returnString) {
System.out.println("going to connector class");
InputStream is = null;
final String KEY_121 = "http://10.0.0.128/connector.php";
String result = "";
//the year data to send
// ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
// nameValuePairs.add(new BasicNameValuePair("year","1970"));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(KEY_121);
// httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
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());
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","ID: "+json_data.getInt("ID")+
", \nActara: "+json_data.getString("Actara")
);
//Get an output to the screen
returnString += "\n\t" + jArray.getJSONObject(i);
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return returnString;
}
}
Logcat error (on 4.0+):
11-12 12:02:35.658: E/log_tag(14083): Error in http connection android.os.NetworkOnMainThreadException
11-12 12:02:35.658: E/log_tag(14083): Error converting result java.lang.NullPointerException
11-12 12:02:35.663: E/log_tag(14083): Error parsing data org.json.JSONException: End of input at character 0 of
Only the first error line is important, because it can't connect to a database, it gives a nullPointer (2nd and 3rd error).
This is what I tried in Asynch:
public class connector extends Activity {
/** Called when the activity is first created. */
TextView txt;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new BackgroundAsyncTask().execute();
}
public class BackgroundAsyncTask extends
AsyncTask<Void, Integer, Void> {
InputStream is = null;
final String KEY_121 = "http://10.0.0.128/connector.php";
String result = "";
String returnString = "";
protected void onPostExecute(Void result) {
}
#Override
protected void onPreExecute() {
System.out.println("onPreExecute");
}
protected Void doInBackground(String... params) {
try{
System.out.println("background in progress");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(KEY_121);
// httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
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());
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","ID: "+json_data.getInt("ID")+
", \nActara: "+json_data.getString("Actara")
);
//Get an output to the screen
returnString += "\n\t" + jArray.getJSONObject(i);
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return null;
}
protected void onProgressUpdate(Integer... values) {
}
#Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
return null;
}
}
}
Someone that can help me? I don't know for sure what the real cause is why it isn't working for 4.0+.
If you need more info, just say it, and I will post it.
Code can be a bit messy, I didn't really "clean" it up yet properly.
Since Android 3.0 you are not allowed to do network stuff on the main thread. Why? because network problems will lead to a slow ui. So you have to do all the http stuff in a new thread. You are on the right path but you made a mistake in your AsyncTask. Delete the empty doInBackground method in you async task and write #Override over your method.
android.os.NetworkOnMainThreadException
this eror comes With HoneyComb(3.0 or Later). you can not perform a networking operation on its main thread as documentation says. to getting ride of this you must use handler or asynctask. AFAIK There is no another way to do it.
you can See this for More Details WHY ICS Crashes your App
Try Using Below Code Snippet
new Thread(){
public void run(){
//do your Code Here
}
}.start();
Ok right...
After searching for few hours, making this question, then 10 minutes later, you find a solution...
Option 1:
I added this line:
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
But I reccomend NOT to use option 1, this is a bad solution for real. Use option 2!
//===========================================================================
Option 2:
Used this tutorial to make a proper ASyncTask: http://www.elvenware.com/charlie/development/android/SimpleHttpGetThread.html
//===========================================================================
Used ASyncTask as final (option 2).
why you are passing null in function of web connection and web service .?
getServerData(null);
I want my android app to get data from an online database. Here are the two scenarios:
When I create my db with xampp and I am using the httpost function with my local machines' ip as argument I see as output what I expect to see (the database at logcat).
My question is: if I run the application from my phone, will it connect to my local machine server or not?
I also have a site (lets say mysite.com) and in order not to buy another server I am placing the php file and the database on that server. But then my android app connects (or so I think) to the server, but it prints out at logcat the whole html site. I am thinking that this is because the server requires a username and a password and I do not know if I provided them or not?
So, what do you suggest to do? I want my database being sent to my app (so as to use it later).
My code is shown below (I have in comments the only that changes between 2 scenarios)
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setImageClickListener();
}
private void setImageClickListener() {
ImageView map_image=(ImageView)findViewById(R.id.map_icon);
map_image.setOnTouchListener(new ImageView.OnTouchListener() {
//OnTouchListener listener = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(!(event.getAction() == MotionEvent.ACTION_DOWN))
return false; //If the touch event was not putting the finger down on the screen, return false(Actions may be move, up, and so on)
final float x = event.getX();
final float y = event.getY();
//System.out.println("Coordinates of button pressed are: X is %d"+x+" and Y is %d"+ y);
if(x>335 && x<395 && y>225 && y< 235)
DoFirst();
return true;
}
});
}
#SuppressWarnings("null")
private void DoFirst() {
Log.d("SnowReportApp","Do first thing");
setContentView(R.layout.layout_1);
String result = "";
InputStream is = null;
StringBuilder sb=null;
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();//() before
nameValuePairs.add(new BasicNameValuePair("year","1980"));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("192.168.1.67/test.php"); // only this changes to my server url : mysite.com/httpdocs/test.php
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
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);
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());
}
//parse JSON data
try{
//JSONObject json_data_1 = new JSONObject(result);
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","id: "+json_data.getInt("id")+
", name: "+json_data.getString("name")+
", sex: "+json_data.getInt("sex")+
", birthyear: "+json_data.getInt("birthyear")
);
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
My php file located on either c:\xampp\htdocs or on mysite server is this:
<?php
mysql_connect("127.0.0.1","root","");
mysql_select_db("peopledata");
$q=mysql_query("SELECT * FROM people WHERE birthyear>'".$_REQUEST['year']."'");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
mysql_close();?>
My question is: if I run the application from my phone, will it
connect to my local machine server or not?
The answer is probably not. It really all depends on:
Whether you're using Wifi or Carrier data (3G, etc)
Whether your DB ports are open (PC firewall)
If Carrier data, is your PC reachable from the Internet (static IP)
You're better off using mysite.com for your DB and whatever backend you need.
As for your other questions, I cannot answer them as they're quite vague. Consider researching your problem some more and perhaps come back with a targeted set of questions.