Gson.fromJson() not working as expected - android

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);

Related

POST request from android to Nodejs

im working on this program ,which i have an Android app that uses Nodejs server.the host is -https://rocky-thicket-82184.herokuapp.com.
now, im trying to just send and recieve a post request . but it seems to not
work.
in the server side i get an error like : cannot GET/.
and in the client side i get :error exeption https://rocky-thicket-82184.herokuapp.com.
I cannot find the problem ,i feel like i'm missing something ...
here is my Android mainActivity.java:
package com.example.shanijoffe.hungry_monkey;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.JsonReader;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.os.AsyncTask;
import android.widget.Toast;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.stormpath.sdk.Stormpath;
import com.stormpath.sdk.StormpathConfiguration;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.net.ssl.HttpsURLConnection;
import cz.msebera.android.httpclient.Header;
import cz.msebera.android.httpclient.HttpResponse;
import cz.msebera.android.httpclient.NameValuePair;
import cz.msebera.android.httpclient.client.ClientProtocolException;
import cz.msebera.android.httpclient.client.HttpClient;
import cz.msebera.android.httpclient.client.entity.UrlEncodedFormEntity;
import cz.msebera.android.httpclient.client.methods.HttpPost;
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient;
import cz.msebera.android.httpclient.message.BasicNameValuePair;
import static com.loopj.android.http.AsyncHttpClient.log;
public class MainActivity extends AppCompatActivity
{
EditText user_name_edit;
EditText password_edit;
Button loginButton;
String user;
String user_pass;
private static final String TAG = "MyActivity";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
user_name_edit=(EditText)findViewById(R.id.txt_usrname);
password_edit=(EditText)findViewById(R.id.txt_password);
loginButton=(Button)findViewById(R.id.btn_login);
Log.i(TAG,"before connection");
}
public void OnClick(final View view) {
view.setEnabled(false);
user=user_name_edit.getText().toString();
user_pass=password_edit.getText().toString();
new SendPostRequest().execute();
//
//
}
public class SendPostRequest extends AsyncTask<String, Void, String>
{
String user_name=user_name_edit.getText().toString();
String user_pass=password_edit.getText().toString();
protected void onPreExecute(){}
protected String doInBackground(String... arg0) {
try {
Log.e("MainActivity", "in SendPostRequest ");
URL url = new URL("https://rocky-thicket-82184.herokuapp.com");
InputStream inp = (InputStream) url.getContent();
Log.e("stream",inp.toString());
JSONObject postDataParams = new JSONObject();
postDataParams.put("username", user);
postDataParams.put("password", user_pass);
Log.e("params", postDataParams.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
int responseCode;
try (BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"))) {
String encoded_string = getPostDataString(postDataParams);
Log.e("encoded", encoded_string);
//writer.write(getPostDataString(postDataParams));
writer.write(postDataParams.toString());
writer.flush();
writer.close();
}
os.close();
responseCode = conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuffer sb = new StringBuffer("");
String line = "";
while ((line = in.readLine()) != null) {
sb.append(line);
break;
}
in.close();
Log.i("MainActivty",sb.toString());
return sb.toString();
} else {
return new String("false : " + responseCode);
}
}
catch (Exception e)
{
return new String("Exception: " + e.getMessage());
}
}
#Override
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(), result,
Toast.LENGTH_LONG).show();
}
public String getPostDataString(JSONObject params) throws Exception
{
StringBuilder result = new StringBuilder();
boolean first = true;
Iterator<String> itr = params.keys();
while(itr.hasNext()){
String key= itr.next();
Object value = params.get(key);
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(key, "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(value.toString(), "UTF-8"));
}
return result.toString();
}
}
}
and here is the nodejs code:
var express=require('express');
var bodyParser=require('body-parser');
var app=express();
var urlencodedParser = bodyParser.urlencoded({ extended: false });
app.get('/',function(req,res){
res.sendFile(__dirname + '/index.html');
});
app.post('/',function(req,res){
console.log(req.body);
if (!req.body)
return res.sendStatus(400)
res.send('welcome, ' + req.body.user)
});
app.listen(process.env.PORT || 3000)
can anyone take a look and see if they can help ?
Thanks alot :)

Send data to server and delete the data that are successfully sent

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

(Priority) How do i use setListAdapter for a specific ListView in FragmentActivity

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.

Phasing Json in android

I have tried to use Gson and another method to get the Json from my localhost and , but still can't done it. One of the method that I have tried is as follow which I see the code from web , it is divided into two java, it fianlly comes out that
" Error parsing data org.json.JSONException: Value [{"Age":18},{"Age":18},{"Age":18}] of type org.json.JSONArray cannot be converted to JSONObject " which [{"Age":18},{"Age":18},{"Age":18}] is the test data that I want to output what is the problem behind?
package com.pxr.tutorial.json;
import java.io.BufferedReader;
import java.io.InputStream;
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;
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();
}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;
}
}
package com.pxr.tutorial.json;
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 com.pxr.tutorial.xmltest.R;
import android.app.ListActivity;
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 Main extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
JSONObject json = JSONfunctions.getJSONfromURL("http://10.0.2.2:28750/User/JsonIndex");
try{
JSONArray earthquakes = json.getJSONArray("Age");
for(int i=0;i<age.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = earthquakes.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("name", "Earthquake name:" + e.getString("eqid"));
map.put("magnitude", "Magnitude: " + e.getString("magnitude"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main,
new String[] { "name", "magnitude" },
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(Main.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
}
});
}
}`

IllegalStateException: Content has been consumed

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

Categories

Resources