2013-05-29 1 views
0

В настоящее время я разрабатываю приложение для Android, которое получает информацию из таблицы SQL Server. Спасибо всем, кто ранее помогал мне в понимании AsyncTasks и JSON, теперь я могу получить объекты. Однако моя попытка обновления таблиц с устройства Android не так гладко.Обновление таблиц в SQL Server из приложения для Android

Во-первых, я начну с таблицы: У меня есть таблица JobStatus, которая заполняется триггером, который запускается как новая запись, вводится в таблицу Jobs. Когда триггер запускается, эти 4 колонки заполняются, а именно: -

  • The JobStatusID
  • JobId
  • QLSJobID
  • JobType

Оставшиеся 4, однако, являются недействительными по по умолчанию и, как ожидается, будут обновлены через операции приложения Android. К ним относятся: -

  • Широта
  • Долгота
  • TimeComplete
  • DateComplete

Я приспособил веб-службы WCF, полученной от here делать операции. Это, по сути, операция обновления POST.

Теперь для кода Android. По сути, то, что происходит в приложении для Android, состоит в том, что запись JobStatus обновляется только при нажатии кнопки «Сохранить/Значок». Чтобы это произошло, пользователь должен ввести свое имя и знак для задания для всей информации, которая будет отправлена ​​обратно в таблицу JobStatus. Вот код:

package com.signonglass; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.Date; 
import java.util.TimeZone; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.client.methods.HttpPut; 
import org.apache.http.entity.StringEntity; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONArray; 
import org.json.JSONObject; 
import org.json.JSONStringer; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.content.ContextWrapper; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.DialogInterface.OnCancelListener; 
import android.graphics.Bitmap; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Path; 
import android.graphics.RectF; 
import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.os.Environment; 
import android.os.Handler; 
import android.provider.MediaStore.Images; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.view.Gravity; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
import android.widget.Toast; 

public class CaptureSignature extends Activity implements LocationListener 
{ 
    private final static String getJobURI = "http://192.168.0.105:8095/CentralMonitoring/CentralMonitor.svc/getJobStatus/"; 
    private final static String jobURI = "http://192.168.0.105:8095/CentralMonitoring/CentralMonitor.svc/UpdateJobStatus"; 

    final Date currentTime = new Date(); 
    final SimpleDateFormat sdf = new SimpleDateFormat("EEE, MMM d, yyyy HH:mm:ss a z"); 

    //Live Connections 
    //private final static String jobURI = "http://192.168.14.9:8092/CentralMonitoring/CentralMonitor.svc/UpdateJobStatus"; 
    //private final static String getJobURI = "http://192.168.14.9:8092/CentralMonitoring/CentralMonitor.svc/getJobStatus/"; 
    JSONStringer jobStatToUpdate; 
    LinearLayout mContent; 
    signature mSignature; 
    Button mClear, mGetSign, mCancel; 
    public static String tempDir; 
    public int count = 1; 
    public String current = null; 
    private Bitmap mBitmap; 
    View mView; 
    File mypath; 

    //additional variables for capturing details 
    Consignments cObj; 
    public Handler mHandler; 
    JobStatus js = new JobStatus(); 
    JobStatus jsUpdate; 
    double latitude; 
    double longitude; 
    String datetime; 
    TextView tvLoc; 
    Location location; 
    String transAdd; 

    private String uniqueId; 
    private EditText yourName; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.signature); 

     cObj = (Consignments)this.getIntent().getSerializableExtra("Consignment"); 
     //tempDir = Environment.getExternalStorageDirectory() + "/" + getResources().getString(R.string.external_dir) + "/"; 

     ContextWrapper cw = new ContextWrapper(getApplicationContext()); 
     File directory = cw.getDir(getResources().getString(R.string.external_dir), Context.MODE_PRIVATE); 

     prepareDirectory(); 

     Calendar c = Calendar.getInstance(); 
     c.setTimeZone(TimeZone.getDefault()); 

     uniqueId = cObj.getJobType() + Integer.toString(cObj.getConsignmentID()); 

     current = uniqueId + ".jpg"; 
     mypath = new File(directory, current); 

     //Control Properties 
     mContent = (LinearLayout) findViewById(R.id.signing); 
     mSignature = new signature(this, null); 
     mSignature.setBackgroundColor(Color.WHITE); 
     mContent.addView(mSignature, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
     mClear = (Button)findViewById(R.id.clear); 
     mGetSign = (Button)findViewById(R.id.getsign); 
     mGetSign.setEnabled(false); 
     mCancel = (Button)findViewById(R.id.cancel); 
     mView = mContent; 

     yourName = (EditText)findViewById(R.id.yourName); 

     /* Use the LocationManager class to obtain GPS locations */ 
     LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
     Criteria criteria = new Criteria(); 

     criteria.setAccuracy(Criteria.ACCURACY_FINE); 
     criteria.setPowerRequirement(Criteria.POWER_LOW); 
     String provider = mlocManager.getBestProvider(criteria, true); 
     location = mlocManager.getLastKnownLocation(provider); 

     mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, CaptureSignature.this); 
     tvLoc = (TextView)findViewById(R.id.textView1); 
     tvLoc.setText("Latitude: "+ location.getLatitude() +", Longitude: "+ location.getLongitude()); 



     mClear.setOnClickListener(new OnClickListener() 
     {   
      public void onClick(View v) 
      { 
       Log.v("log_tag", "Panel Cleared"); 
       mSignature.clear(); 
       mGetSign.setEnabled(false); 
      } 
     }); 

     mGetSign.setOnClickListener(new OnClickListener() 
     {   
      public void onClick(View v) 
      { 
       Log.v("log_tag", "Panel Saved"); 
       boolean error = captureSignature(); 
       if(!error) 
       { 
        new updateJobStatus().execute(js); 
        mView.setDrawingCacheEnabled(true); 
        mSignature.save(mView); 
        Bundle b = new Bundle(); 
        b.putString("status", "done"); 
        Intent intent = new Intent(); 
        intent.putExtras(b); 
        setResult(RESULT_OK,intent); 
        finish(); 
       } 
      } 
     }); 

     //Cancel method - to create pop up button 
     mCancel.setOnClickListener(new OnClickListener() 
     {   
      public void onClick(View v) 
      { 
       Log.v("log_tag", "Panel Canceled"); 
       Bundle b = new Bundle(); 
       b.putString("status", "cancel"); 
       Intent intent = new Intent(); 
       intent.putExtras(b); 
       setResult(RESULT_OK,intent); 
       finish(); 
      } 
     }); 

     new getJobStatus().execute(uniqueId); 
    } 

    public class getJobStatus extends AsyncTask<String, String, JobStatus> 
    { 
     private ProgressDialog progressDialog = new ProgressDialog(CaptureSignature.this); 
     InputStream inputStream = null; 
     String theString = ""; 
     StringBuilder builder; 

     protected void onPreExecute() 
     { 
      progressDialog.setMessage("Getting " + uniqueId +" to be updated..."); 
      progressDialog.show(); 
      progressDialog.setOnCancelListener(new OnCancelListener() 
      { 
       public void onCancel(DialogInterface arg0) 
       { 
        getJobStatus.this.cancel(true); 
       } 
      }); 
     } 

     @Override 
     protected JobStatus doInBackground(String... params) 
     { 
      try 
      { 
       DefaultHttpClient client = new DefaultHttpClient(); 
       String theString = new String(""); 
       //http get request 
       HttpGet request = new HttpGet(getJobURI + cObj.getJobType() + cObj.getConsignmentID()); 
       //set the hedear to get the data in JSON format 
       request.setHeader("Accept", "application/json"); 
       request.setHeader("Content-type", "application/json"); 

       //get the response 
       HttpResponse response = client.execute(request); 
       HttpEntity entity = response.getEntity(); 
       InputStream is = entity.getContent(); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 

       StringBuilder builder = new StringBuilder(); 
       String line; 
       while ((line = reader.readLine()) != null) 
       { 
        builder.append(line); 
       } 
       is.close(); 

       theString = builder.toString(); 

       JSONObject jsObj = new JSONObject(theString); 
       JSONArray jstat = jsObj.getJSONArray("getJobStatusResult"); 

       for(int i = 0; i < jstat.length(); i++) 
       { 
        JSONObject jst = jstat.getJSONObject(i); 
        js.JobStatusID = jst.getInt("JobStatusID"); 
        js.JobID = jst.getInt("JobID"); 
        js.JobType = jst.getString("jobType"); 
        js.QlsJobID = jst.getInt("qlsJobID"); 
        /*js.DateComplete = jst.getString("dateComplete"); 
        js.TimeComplete = jst.getString("timeComplete"); 
        js.Latitude = jst.getDouble("latitude"); 
        js.Longitude = jst.getDouble("longitude");*/ 
       } 
      } 
      catch(Exception e) 
      { 
       e.printStackTrace(); 
      } 
      return js; 
     } 

     protected void onPostExecute(JobStatus jobStatus) 
     { 
      this.progressDialog.dismiss(); 
      jsUpdate = js; 
     } 
    } 


    public class updateJobStatus extends AsyncTask<JobStatus, String, JSONStringer> 
    { 
     private ProgressDialog progressDialog = new ProgressDialog(CaptureSignature.this); 
     InputStream inputStream = null; 
     String theString = ""; 
     StringBuilder builder; 

     protected void onPreExecute() 
     { 
      progressDialog.setMessage("Updating " + uniqueId +"..."); 
      progressDialog.show(); 
      progressDialog.setOnCancelListener(new OnCancelListener() 
      { 
       public void onCancel(DialogInterface arg0) 
       { 
        updateJobStatus.this.cancel(true); 
       } 
      }); 
     } 

     @Override 
     protected JSONStringer doInBackground(JobStatus... arg0) 
     { 
      HttpPut request = new HttpPut(jobURI); 

      try 
      { 
        sdf.setTimeZone(TimeZone.getTimeZone("AEST")); 
        jobStatToUpdate = new JSONStringer() 
        .object() 
        .key("JobStatusID").value(js.getJobStatusID()) 
        .key("JobID").value(js.getJobID()) 
        .key("dateComplete").value(js.getDate()) 
        .key("latitude").value(js.getLat()) 
        .key("longitude").value(js.getClass()) 
        .key("timeComplete").value(js.getTime()) 
        .key("qlsjobID").value(js.getQLSID()) 
        .endObject(); 

        StringEntity entity = new StringEntity(jobStatToUpdate.toString()); 
        request.setEntity(entity); 

        DefaultHttpClient httpClient = new DefaultHttpClient(); 
        HttpResponse response = httpClient.execute(request); 

       Log.d("WebInvoke", "Saving : " + response.getStatusLine().getStatusCode()); 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 
      return jobStatToUpdate; 
     } 


    } 


    @Override 
    protected void onDestroy() 
    { 
     //Log.w("GetSignature", "onDestory"); 
     //super.onDestroy(); 
    } 

    private boolean captureSignature() 
    { 

     boolean error = false; 
     String errorMessage = ""; 


     if(yourName.getText().toString().equalsIgnoreCase("")) 
     { 
      errorMessage = errorMessage + "Please enter your Name\n"; 
      error = true; 
     } 

     if(error) 
     { 
      Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT); 
      toast.setGravity(Gravity.TOP, 105, 50); 
      toast.show(); 
     } 

     return error; 
    } 

    private boolean prepareDirectory() 
    { 
     try 
     { 
      if (makedirs()) 
      { 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
      Toast.makeText(this, "Could not initiate File System.. Is Sdcard mounted properly?", Toast.LENGTH_SHORT).show(); 
      return false; 
     } 
    } 

    private boolean makedirs() 
    { 
     File tempdir = new File(tempDir); 
     if (!tempdir.exists()) 
      tempdir.mkdirs(); 

     if (tempdir.isDirectory()) 
     { 
      File[] files = tempdir.listFiles(); 
      for (File file : files) 
      { 
       if (!file.delete()) 
       { 
        System.out.println("Failed to delete " + file); 
       } 
      } 
     } 
     return (tempdir.isDirectory()); 
    } 

    public class signature extends View 
    { 
     private static final float STROKE_WIDTH = 5f; 
     private static final float HALF_STROKE_WIDTH = STROKE_WIDTH/2; 
     private Paint paint = new Paint(); 
     private Path path = new Path(); 

     private float lastTouchX; 
     private float lastTouchY; 
     private final RectF dirtyRect = new RectF(); 

     public signature(Context context, AttributeSet attrs) 
     { 
      super(context, attrs); 
      paint.setAntiAlias(true); 
      paint.setColor(Color.BLACK); 
      paint.setStyle(Paint.Style.STROKE); 
      paint.setStrokeJoin(Paint.Join.ROUND); 
      paint.setStrokeWidth(STROKE_WIDTH); 
     } 

     public void save(View v) 
     { 
      Log.v("log_tag", "Width: " + v.getWidth()); 
      Log.v("log_tag", "Height: " + v.getHeight()); 
      if(mBitmap == null) 
      { 
       mBitmap = Bitmap.createBitmap (mContent.getWidth(), mContent.getHeight(), Bitmap.Config.RGB_565);; 
      } 
      Canvas canvas = new Canvas(mBitmap); 
      try 
      { 
       FileOutputStream mFileOutStream = new FileOutputStream(mypath); 

       v.draw(canvas); 
       mBitmap.compress(Bitmap.CompressFormat.PNG, 90, mFileOutStream); 
       mFileOutStream.flush(); 
       mFileOutStream.close(); 
       String url = Images.Media.insertImage(getContentResolver(), mBitmap, "title", null); 
       Log.v("log_tag","url: " + url); 

      } 
      catch(Exception e) 
      { 
       Log.v("log_tag", e.toString()); 
      } 
     } 

     public void clear() 
     { 
      path.reset(); 
      invalidate(); 
     } 

     @Override 
     protected void onDraw(Canvas canvas) 
     { 
      canvas.drawPath(path, paint); 
     } 

     @Override 
     public boolean onTouchEvent(MotionEvent event) 
     { 
      float eventX = event.getX(); 
      float eventY = event.getY(); 
      mGetSign.setEnabled(true); 

      switch (event.getAction()) 
      { 
      case MotionEvent.ACTION_DOWN: 
       path.moveTo(eventX, eventY); 
       lastTouchX = eventX; 
       lastTouchY = eventY; 
       return true; 

      case MotionEvent.ACTION_MOVE: 

      case MotionEvent.ACTION_UP: 

       resetDirtyRect(eventX, eventY); 
       int historySize = event.getHistorySize(); 
       for (int i = 0; i < historySize; i++) 
       { 
        float historicalX = event.getHistoricalX(i); 
        float historicalY = event.getHistoricalY(i); 
        expandDirtyRect(historicalX, historicalY); 
        path.lineTo(historicalX, historicalY); 
       } 
       path.lineTo(eventX, eventY); 
       break; 

      default: 
       debug("Ignored touch event: " + event.toString()); 
       return false; 
      } 

      invalidate((int) (dirtyRect.left - HALF_STROKE_WIDTH), 
        (int) (dirtyRect.top - HALF_STROKE_WIDTH), 
        (int) (dirtyRect.right + HALF_STROKE_WIDTH), 
        (int) (dirtyRect.bottom + HALF_STROKE_WIDTH)); 

      lastTouchX = eventX; 
      lastTouchY = eventY; 

      return true; 
     } 

     private void debug(String string) 
     { 

     } 

     private void expandDirtyRect(float historicalX, float historicalY) 
     { 
      if (historicalX < dirtyRect.left) 
      { 
       dirtyRect.left = historicalX; 
      } 
      else if (historicalX > dirtyRect.right) 
      { 
       dirtyRect.right = historicalX; 
      } 

      if (historicalY < dirtyRect.top) 
      { 
       dirtyRect.top = historicalY; 
      } 
      else if (historicalY > dirtyRect.bottom) 
      { 
       dirtyRect.bottom = historicalY; 
      } 
     } 

     private void resetDirtyRect(float eventX, float eventY) 
     { 
      dirtyRect.left = Math.min(lastTouchX, eventX); 
      dirtyRect.right = Math.max(lastTouchX, eventX); 
      dirtyRect.top = Math.min(lastTouchY, eventY); 
      dirtyRect.bottom = Math.max(lastTouchY, eventY); 
     } 
    } 

    @Override 
    public void onLocationChanged(Location mlocation) 
    { 
     tvLoc.setText("Latitude: "+ location.getLatitude() +", Longitude: "+ location.getLongitude()); 
    } 

    @Override 
    public void onProviderDisabled(String provider) 
    { 
     // TODO Auto-generated method stub 
     Toast.makeText(getApplicationContext(), "GPS Disabled", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onProviderEnabled(String provider) 
    { 
     // TODO Auto-generated method stub 
     Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) 
    { 
     // TODO Auto-generated method stub 

    } 
} 

Этот код представляет собой сочетание того, что я получил от here, с некоторыми изменениями. Я прокомментировал некоторые вещи - это может повлиять на ошибку, но на данный момент я больше одержим обновлением объекта JobStatus.

И я уверен, что код выше, в первую очередь UpdateJobTask AsyncTask должен быть исправлен в значительной степени.

У меня не было каких-либо ошибок, запущенные, но здесь необходимо войти ошибка в любом случае: -

05-29 10:33:37.214: W/dalvikvm(17704): threadid=1: thread exiting with uncaught exception (group=0x40f13258) 
05-29 10:33:37.226: E/AndroidRuntime(17704): FATAL EXCEPTION: main 
05-29 10:33:37.226: E/AndroidRuntime(17704): android.app.SuperNotCalledException: Activity {com.signonglass/com.signonglass.CaptureSignature} did not call through to super.onDestroy() 
05-29 10:33:37.226: E/AndroidRuntime(17704): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3260) 
05-29 10:33:37.226: E/AndroidRuntime(17704): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3289) 
05-29 10:33:37.226: E/AndroidRuntime(17704): at android.app.ActivityThread.access$1200(ActivityThread.java:134) 
05-29 10:33:37.226: E/AndroidRuntime(17704): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280) 
05-29 10:33:37.226: E/AndroidRuntime(17704): at android.os.Handler.dispatchMessage(Handler.java:99) 
05-29 10:33:37.226: E/AndroidRuntime(17704): at android.os.Looper.loop(Looper.java:154) 
05-29 10:33:37.226: E/AndroidRuntime(17704): at android.app.ActivityThread.main(ActivityThread.java:4624) 
05-29 10:33:37.226: E/AndroidRuntime(17704): at java.lang.reflect.Method.invokeNative(Native Method) 
05-29 10:33:37.226: E/AndroidRuntime(17704): at java.lang.reflect.Method.invoke(Method.java:511) 
05-29 10:33:37.226: E/AndroidRuntime(17704): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809) 
05-29 10:33:37.226: E/AndroidRuntime(17704): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576) 
05-29 10:33:37.226: E/AndroidRuntime(17704): at dalvik.system.NativeStart.main(Native Method) 

Заранее спасибо за ваши советы и предложения, ребята! :)

+0

@codeMagic, вы столкнулись с этим раньше? – cmnunis

ответ

0

Чтобы исправить журнал ошибок, вы должны вызвать super.onDestroy() в вашем onDestroy().

+0

Спасибо за подсказку. Я включил его. – cmnunis

Смежные вопросы