Note that there are some explanatory texts on larger screens.

plurals
  1. POApplication says: Unfortunately stopped. (on adding JsonObject from google)
    primarykey
    data
    text
    <p>I am trying to push data to a server after taking values from the accelerometer.</p> <p>In that I am using JsonObject from google.</p> <p>There seems to be some problem with this object. As soon as I put instantiate one object from this my application throws an error: Unfortunately appname has stopped.</p> <p>My MainActivity code:</p> <pre><code>package com.example.accelerometerdemo; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; import java.net.HttpURLConnection; import java.net.URL; import android.app.Activity; import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.widget.RelativeLayout; import android.widget.TextView; import com.google.gson.JsonIOException; import com.google.gson.JsonObject; public class MainActivity extends Activity implements SensorEventListener { private static final String MSG_TAG_1 = "MainActivity"; private static final String MSG_TAG_2 = "place_holder"; private SensorManager mSensorManager; private Sensor mAccelerometer; TextView title,tv,tv1,tv2, test; RelativeLayout layout; int i = 0; //For preparing file String[] paramName = { "device_id", "timestamp", "sensor_type", "sensor_value" }; String URLStr = "http://209.129.244.7/sensors"; java.util.Date date = new java.util.Date(); @Override public final void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //refer layout file code below //get the sensor service mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); //get the accelerometer sensor mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); //If sensor not available if (mAccelerometer == null){ System.out.println("no temperature sensor"); } //get layout layout = (RelativeLayout)findViewById(R.id.relative); //get textviews title=(TextView)findViewById(R.id.name); tv=(TextView)findViewById(R.id.xval); tv1=(TextView)findViewById(R.id.yval); tv2=(TextView)findViewById(R.id.zval); test = (TextView)findViewById(R.id.testval); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public final void onAccuracyChanged(Sensor sensor, int accuracy) { // Do something here if sensor accuracy changes. } @Override public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { i++; if (i &lt; 5) { getAccelerometer(event); } else { onPause(); } } } public final void getAccelerometer(SensorEvent event) { // Many sensors return 3 values, one for each axis. float x = event.values[0]; float y = event.values[1]; float z = event.values[2]; String t = "This is a test value"; //display values using TextView title.setText(R.string.app_name); tv.setText("X axis" +"\t\t"+x); tv1.setText("Y axis" + "\t\t" +y); tv2.setText("Z axis" +"\t\t" +z); test.setText("Testing" +"\t\t" +event.values[2]); JsonObject jo = new JsonObject(); try { //formatting the file jsonObject_x.put("sensor_value", 78); jo.addProperty("device_id", "nexus_test_dev"); //Long type jo.addProperty("timestamp", date.getTime()); //Long type jo.addProperty("sensor_type", "Accelerometer_x"); //String type jo.addProperty("sensor_value", 78); //Double type //String[] paramName = { "device_id", "timestamp", "sensor_type", "sensor_value" }; //{"device_id":"test", "timestamp": 1373566899100, "temp": 123} String[] paramVal = { "aeron_test_p", String.valueOf(date.getTime()), "temp", "121" }; //Displaying readings in LOGCAT for(String s : paramVal){ Log.d(MSG_TAG_1, s); } try { httpPostSensorReading(URLStr, jo.toString()); Log.d(MSG_TAG_2, "Test"); } catch (Exception e) {e.printStackTrace(); } } catch (JsonIOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static String httpPostSensorReading(String urlStr, String jsonString) throws Exception { URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("Accept", "application/json"); conn.setDoOutput(true); // Create the form content OutputStream out = conn.getOutputStream(); Writer writer = new OutputStreamWriter(out, "UTF-8"); writer.write(jsonString); writer.close(); out.close(); if (conn.getResponseCode() != 200) { throw new IOException(conn.getResponseMessage()); } // Buffer the result into a string BufferedReader rd = new BufferedReader(new InputStreamReader( conn.getInputStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = rd.readLine()) != null) { sb.append(line); } rd.close(); conn.disconnect(); return sb.toString(); } @Override protected void onResume() { super.onResume(); mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL); } @Override protected void onPause() { super.onPause(); mSensorManager.unregisterListener(this); } } </code></pre> <p>The moment I add this</p> <pre><code> JsonObject jo = new JsonObject(); </code></pre> <p>The application throws an error "Unfortunately appname hsa stopped.</p> <p>SOme issue with JsonObject. I used JSONObject then it was working fine.</p> <p>Thanks,</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload