I want to send the data from the sqlite database in android to the server and delete those data from the database that have been reached to the server successfully. I have successfully written the code to send data to the server. But could not delete the data form database. How to know which data is reached to the server.
package com.example.income;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.*;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.IntentService;
import android.content.Intent;
import android.database.Cursor;
import android.util.*;
public class Background extends IntentService
{
public Background()
{
super("This is the simple background class");
}
#Override
protected void onHandleIntent(Intent intent)
{
Log.v("message","This is simple background service");
Db db =new Db(Background.this,"simple",null,4);
Cursor c= db.getData();
if( c.moveToFirst())
{
List<Map<String, String>> contacts = new ArrayList<Map<String, String>>();
do
{
String num,dater;
int integer;
integer=c.getInt (c.getColumnIndex(Base.Identifier));
num = c.getString(c.getColumnIndex(Base.CONTACTS));
dater =c.getString(c.getColumnIndex(Base.DATE));
Map<String, String> contact = new HashMap<String, String>();
contact.put("date", dater);
contact.put("contact", num);
contact.put("id",String.valueOf(integer));
contacts.add(contact);
}
while (c.moveToNext());
try
{
sendData(contacts);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e)
{
e.printStackTrace();
}
}
}
public void sendData(List<Map<String, String>> contacts) throws ClientProtocolException, IOException, JSONException
{
Log.v("Let's C","Will it go here?");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.10.115/android.php");
Log.v("p","this");
Log.d("Contacts", Integer.toString(contacts.size()));
JSONArray array= new JSONArray(contacts);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("forward",array.toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response= httpclient.execute(httppost);
HttpEntity entity=response.getEntity();
String result =EntityUtils.toString(entity);
JSONArray obj = new JSONArray(result);
for(int i =0;i<obj.length();i++)
{
JSONObject json=obj.getJSONObject(i);
String success =json.getString("id");
Log.i("success ",success);
}
/*
Runtime runtime1 = Runtime.getRuntime();
Process proc = runtime1.exec("ping -c 8 www.google.com");
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String input;
while((input=reader.readLine())!=null)
{
Log.i("message",input);
}*/
}
}
I want to implement this as soon as data is sent to server.
Make a class that compares the value of sqlite data you send to the server through JSON object/array and return a flag. If it is successful then delete local data, if not try resending your data again.
This link might help you achieve your goal.
how to send data to server from android when no internet is available
Related
in my application i am fetching json data from online server. and after that i am trying to display the data by toast. but the application stop working. if i commented the toast section then the application runs smoothly. so i think there is a problem in toast section. so guys plz help me to find out the reason for the problem
package com.example.getdata;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
public class MainActivity extends Activity {
EditText password,username;
String pass,user;
//TextView output;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onConnect(View v) {
new Thread(){
public void run(){
HttpClient myClient = new DefaultHttpClient();
HttpPost post = new HttpPost("http://tusharinfotech.com/debasish/get_data.php");
try {
List<NameValuePair> myArgs = new ArrayList<NameValuePair>();
// myArgs.add(new BasicNameValuePair("username", user));
// myArgs.add(new BasicNameValuePair("password", pass));
post.setEntity(new UrlEncodedFormEntity(myArgs));
HttpResponse myResponse = myClient.execute(post);
BufferedReader br = new BufferedReader( new InputStreamReader(myResponse.getEntity().getContent()));
String line = "";
String data1 ="";
while ((line = br.readLine()) != null)
{
try {
JSONArray myarray = new JSONArray(line);
for(int i=0;i<myarray.length();i++){
JSONObject jsonObject = myarray.getJSONObject(i);
int id = Integer.parseInt(jsonObject.optString("FOOD_ID").toString());
String name = jsonObject.optString("FOOD_NAME").toString();
data1 += "Node"+i+" : \n id= "+ id +" \n Name= "+ name +" \n ";
}
//Log.d("mytag",data);
//this application stop working for this toast part.. if i commented it then the application run smoothly
Toast.makeText(getApplicationContext(),data1, Toast.LENGTH_LONG).show();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//EditText output1 = (EditText) findViewById(R.id.editText1);
// output1.setText(data);
Log.d("mytag", line);
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
}
I guess you are trying to show toast on a background thread, which is not allowed in android. you can use this code to show toast in background thread :
activity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();
}
});
You must display Toast only from UI thread, to workaround this you can get use of runOnUiThread() method or create new Handler and pass in constructor Looper.getMainLoopper()
You could use AsyncTask Framework to make background processing easier http://developer.android.com/reference/android/os/AsyncTask.html.
The method onPostExecute(Result) will be run on your UI thread there for you can change your UI element here.
I've a huge json string that I want to use to retrieve objects.
That's why I used Gson instead of the usual JsonObject as said here.
Here is my code:
public List<ProductJavaBean> getProductsData()
{
url = "http://api.xxx/products.json";
String line = "";
Gson gson = new GsonBuilder().create();
List<ProductJavaBean> products = new ArrayList<ProductJavaBean>();
try {
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet();
URI website = new URI(url);
request.setURI(website);
HttpResponse response = httpclient.execute(request);
JsonReader reader = new JsonReader(new InputStreamReader(response.getEntity().getContent(),"UTF-8"));
reader.beginArray();
while(reader.hasNext())
{
ProductJavaBean product = gson.fromJson(reader ,ProductJavaBean.class);
products.add(product);
}
reader.endArray();
reader.close();
}
catch (Exception exc)
{
Log.e("Error retrieving products data:" , exc.getMessage());
exc.printStackTrace();
}
return products;
}
as I followed the API samples.
But I have a strange behaviour on the fromJson method:
The method fromJson(String, Class) in the type Gson is not
applicable for the arguments (JsonReader, Class)
Thanks.
EDIT:
here are my imports:
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.R;
import android.os.AsyncTask;
import android.util.JsonReader;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.gz.constancl.model.ProductJavaBean;
You can modify ur code from
JsonReader reader = new JsonReader(new InputStreamReader(response.getEntity().getContent(),"UTF-8"));
reader.beginArray();
while(reader.hasNext())
{
ProductJavaBean product = gson.fromJson(reader ,ProductJavaBean.class);
products.add(product);
}
reader.endArray();
reader.close();
to
Type listType = new TypeToken<ArrayList<ProductJavaBean>>() {}.getType();
products = new Gson().fromJson(EntityUtils.toString(response.getEntity()), listType);
I am just programming a data evaluation app for a hydropower station.
There i need to download the data from the server, which ist lying there - as a MySQL table, formatted to a JSON-array.
Now after umpteen hours of work i've done it to connect to the server, download the data and to output it in a auto-generated ListView Layout (with this.setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, results)); )
But this was just a testapplication for the connection.
In the main app i need the dataoutput from the server in a specifiv ListView in one of the Fragments. After again trying around some hours without any success I decided to ask you.
The setListAdapter is not working in a FragmentActivity anymore.
And another question: Is it possible to save those data in specific variables, after downloading it from our server?
Greetings from beatiful Austria,
Duned
package at.duned.hydroevaluation;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.os.StrictMode;
import android.os.StrictMode.ThreadPolicy;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
import java.util.List;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class Fragment2_DataOutput extends Fragment {
InputStream is;
ArrayList<String> results =new ArrayList<String>();
JSONObject json_data;
TextView tw09;
ListView list;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
super.onCreate(savedInstanceState);
View V=inflater.inflate(R.layout.fragment_2_dataoutput,container, false);
TextView tw09 = (TextView)V.findViewById(R.id.TextView09);
ListView list = (ListView)V.findViewById(R.id.list);
StrictMode.ThreadPolicy policy = new
StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
getData();
//tw09.setText((CharSequence) is);
return V;
}
private void getData() {
String result = "";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://patrickhartl.lima-city.de/eva1.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", "Fehler bei der http Verbindung "+e.toString());}
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());}
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
results.add((String) json_data.get("Messdaten") + " "+ json_data.get("Wert") + " " + json_data.get("Einheit"));
}
fillList();
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
public void fillList() {
this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));
}
}
The setListAdapter is not working in a FragmentActivity anymore
setListAdapter() is a method on ListActivity or ListFragment. You call setAdapter() on your ListView in other cases, such as this one.
Also, please get rid of your StrictMode stuff and fix your app to move your I/O into a background thread.
I am doing a project where I need to parse JSON from a URL through HttpClient. My code works fine for JSON object responses with a small amount of data. But when I use the same code to get a response with a huge amount of data (more than 3MB), I have a problem.
Here is my code:
JSONfunctions.java (function for json parsing)
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
//import java.util.HashMap;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
//import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
//import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
import android.widget.Toast;
#SuppressWarnings("unused")
public class JSONfunctions {
public static JSONObject getJSONfromURL(String url){
InputStream is = null;
String result = "";
JSONObject jArray = null;
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
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();
// Toast.makeText(getBaseContext, result, Toast.LENGTH_LONG).
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
jArray = new JSONObject(result);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return jArray;
}
}
ListJson.java
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
//import org.w3c.dom.Document;
//import org.w3c.dom.Element;
//import org.w3c.dom.NodeList;
import android.app.ListActivity;
import android.content.Intent;
//import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class ListJson extends ListActivity {
public static JSONObject json;
public static JSONArray data;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list1);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
// String semail = "partha#excoflare.com";
// String spassword = "partha123";
// Toast.makeText(getApplicationContext(), JSONExample2.strEmail, Toast.LENGTH_LONG).show();
//Toast.makeText(getApplicationContext(), JSONExample2.strPwd, Toast.LENGTH_LONG).show();
//
json = JSONfunctions.getJSONfromURL("url here");
try{
data = json.getJSONArray("server_list");
for(int i=0;i<data.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = data.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("name", "" + e.getString("ServUser"));
map.put("email", "" + e.getString("ServURL"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
// try {
// JSONObject newObject=json.getJSONObject("subscription");
// JSONArray data1 = newObject.getJSONArray("cust_product");
//
// Toast.makeText(getApplicationContext(), data1.toString(), Toast.LENGTH_LONG).show();
//
// for(int i=0;i<data1.length();i++){
// HashMap<String, String> map1 = new HashMap<String, String>();
// JSONObject e = data.getJSONObject(i);
//
// map1.put("id", String.valueOf(i));
// map1.put("name", "" + e.getString("ServUser"));
// map1.put("email", "" + e.getString("ServURL"));
// mylist.add(map1);
// }
//
// } catch (JSONException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.item_list,
new String[] { "name", "email" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//#SuppressWarnings("unchecked")
//HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
// Toast.makeText(ListJson.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), parent.getItemAtPosition(position).toString(),
Toast.LENGTH_LONG).show();
// String a= parent.getItemAtPosition(position).toString();
Intent intent2= new Intent(ListJson.this, ListJson2.class);
startActivity(intent2);
}
});
}
I am getting an OutOfMemoryException. I changed heap_size to 192MB and ram size to 32MB, but with no luck. How can I fix this?
Big amounts of data of JSON must be cut to smaller pieces. For example you have a 50000 products on your database. Then it's wise to paginate API requests - get this huge amount of products by 100-500 records on one query. That will solve your problem.
This approach solves one problem more - errors caused by internet and gprs connection loss etc.
If API is yours then you can change this. If not, then this is a big failure of API design and you can send change request.
EDIT:
Did a little reading and found that highly recommended for parsing huge load of JSON data is http://jackson.codehaus.org/ (Jackson Processor). Haven't tried it, so cannot comment about this library. Also recommend you to save this JSON stream into the file (don't load it to memory) and then parse it by chunks.
I got struck because of IllegalStateException in the following code. Can anybody please help me? Code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Login extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
Button bt = (Button) findViewById(R.id.logbt);
final EditText user = (EditText) findViewById(R.id.loguser);
final EditText pw = (EditText) findViewById(R.id.logpw);
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (user.getText().toString() != "" && pw.getText().toString() != "") {
Thread t = new Thread() {
public void run() {
try {
HttpClient client = new DefaultHttpClient();
String postURL = "http://surfkid.redio.de/login";
HttpPost post = new HttpPost(postURL);
ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", user.getText().toString()));
params.add(new BasicNameValuePair("password", md5(pw.getText().toString())));
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params, HTTP.UTF_8);
post.setEntity(ent);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
final JSONObject jObject = new JSONObject(EntityUtils.toString(resEntity));
Log.e("XXX", EntityUtils.toString(resEntity));
} catch (Exception e) {
Log.e("XXX", e.toString());
}
}
};
t.start();
// Log.e("XXX",s);
}
}
});
}
private String md5(String in) {
MessageDigest digest;
try {
digest = MessageDigest.getInstance("MD5");
digest.reset();
digest.update(in.getBytes());
byte[] a = digest.digest();
int len = a.length;
StringBuilder sb = new StringBuilder(len << 1);
for (int i = 0; i < len; i++) {
sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16));
sb.append(Character.forDigit(a[i] & 0x0f, 16));
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
}
Logcat message:
01-18 18:39:53.383: ERROR/XXX(7113):
java.lang.IllegalStateException:
Content has been consumed
You can consume Content only at once from an Entity
in the line :
final JSONObject jObject = new JSONObject(EntityUtils.toString(resEntity));
you have consumed content and again you are using the same at here:
Log.e("XXX",EntityUtils.toString(resEntity));
That why it is causing IllegalStateException: Content has been consumed
So the solution is here:
String _response=EntityUtils.toString(resEntity); // content will be consume only once
final JSONObject jObject=new JSONObject(_response);
Log.e("XXX",_response);
it's also happens if you are writing the consuming statement in the Expressions of the debugger!!!
(e.g if you are doing "watch" to something like EntityUtils.toString(resEntity))
First, this has to be a mistake that every single new android programmer makes and it's asked here every single day. You have
user.getText().toString()!= ""&& pw.getText().toString()!= ""
This doesn't do what you want it to. You need
!user.getText().toString().equals("")&& !pw.getText().toString().equals("")
Also, you need to print the stacktrace. In your exception, you need
e.printStackTrace()
instead of logging
e.toString()
I just dealt with a case of a null check on the entity causing it to be flagged as "consumed". Hope my headache will help someone else out there.
Vikas Patidar's answer helped me figure out the key to the riddle, so many thanks