Sending Data To Server From Text File Using A Service
I am sending data from text file to server using service code given below. It sends data line by line to server. I want the service to run in background continuously.For this I hav
Solution 1:
I have Modify your code:
Check my all code this ll give you idea about send line one by one. Please do rest of logic this way. i have also remove some method.
This is not all code but it ll give you IDEA.
package com.example.getjson;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.http.HttpEntity;
import org.apache.http.entity.StringEntity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
publicclassbackserviceextendsService {
privatestaticfinalStringTAG="BackgroundService";
Stringline=null;
ContextmContext=null;
File file;
RandomAccessFilein=null;
StringEntity se;
HttpEntityentity=null;
finalstaticintSERVICE_NAME=1;
int WORK_TYPE;
String response;
Stringinput="";
publicbackservice() {
// super(TAG);
}
@Overridepublic IBinder onBind(Intent intent) {
// TODO Auto-generated method stubreturnnull;
}
@OverridepublicintonStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "in BackgroundService",
Toast.LENGTH_LONG).show();
mContext = getBaseContext();
WORK_TYPE = 2;
// new BackgroundTask(mContext).execute();switch (WORK_TYPE) {
case2:
Filefile=newFile(Environment.getExternalStorageDirectory(),
"/BPCLTracker/gpsdata.txt");
inti=0;
RandomAccessFilein=null;
try {
in = newRandomAccessFile(file, "rw");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
while ((line = in.readLine()) != null) {
input = line.toString();
// response = sendDataToServer(input);newBackgroundTask(mContext).execute();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
returnsuper.onStartCommand(intent, flags, startId);
}
publicclassBackgroundTaskextendsAsyncTask<String, String, Void> {
ContextmContext=null;
publicBackgroundTask(Context context) {
mContext = context;
}
protectedvoidonPreExecute() {
Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_LONG)
.show();
}
protected Void doInBackground(final String... args) {
response = sendDataToServer(input);
returnnull;
}
protectedvoidonPostExecute(final Void unused) {
}
}
publicvoidcallService(int work) {
WORK_TYPE = work;
newBackgroundTask(mContext).execute();
}
public String sendDataToServer(String data) {
StringBuffersb=newStringBuffer("");
StringserverUrl="http://67.23.166.35:80/android/insert.php";
try {
URLurl=newURL(serverUrl);
HttpURLConnectionconn= (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setConnectTimeout(6 * 10 * 1000);
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
conn.setRequestMethod("POST");
OutputStreamWriterwr=newOutputStreamWriter(
conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the responseBufferedReaderrd=newBufferedReader(newInputStreamReader(
conn.getInputStream()));
Stringline="";
while ((line = rd.readLine()) != null) {
// Process line...
sb.append(line);
}
wr.close();
rd.close();
return sb.toString();
} catch (Exception e) {
Log.d("Exception : ", e.getStackTrace().toString());
}
return sb.toString();
}
}
Post a Comment for "Sending Data To Server From Text File Using A Service"