Selaa lähdekoodia

Bug fixed

ALL web requests are now asynchronous!!
Deben Oldert 10 vuotta sitten
vanhempi
commit
fb03da36da

+ 51 - 21
Android app/java/com/dev/deben/implementation/MainActivity.java

@@ -9,7 +9,6 @@
 package com.dev.deben.implementation;
 
 import android.app.ProgressDialog;
-import android.content.Context;
 import android.graphics.Color;
 import android.os.Bundle;
 import android.support.v7.app.AppCompatActivity;
@@ -19,7 +18,7 @@ import android.view.View;
 import android.widget.Button;
 import android.widget.TextView;
 
-import com.loopj.android.http.*;
+import com.loopj.android.http.AsyncHttpResponseHandler;
 
 import org.json.JSONException;
 import org.json.simple.JSONObject;
@@ -27,7 +26,6 @@ import org.json.simple.JSONValue;
 
 import java.io.IOException;
 
-import cz.msebera.android.httpclient.entity.StringEntity;
 import cz.msebera.android.httpclient.Header;
 
 
@@ -40,7 +38,6 @@ public class MainActivity extends AppCompatActivity {
     Button deny;
     TextView status;
     TextView desc;
-    Context ctx = this;
     boolean opt;
 
     @Override
@@ -53,6 +50,7 @@ public class MainActivity extends AppCompatActivity {
         super.onPause();
         active = false;
     }
+
     @Override
     public void onResume() {
         super.onResume();
@@ -62,12 +60,11 @@ public class MainActivity extends AppCompatActivity {
         status = (TextView) findViewById(R.id.status);
         desc = (TextView) findViewById(R.id.descriptor);
         try {
-            if(fn.readSetting("requestId").equals("0")) {
+            if (fn.readSetting("requestId").equals("0")) {
                 desc.setText(R.string.No_req);
                 accept.setEnabled(false);
                 deny.setEnabled(false);
-            }
-            else {
+            } else {
                 desc.setText(R.string.req);
                 accept.setEnabled(true);
                 deny.setEnabled(true);
@@ -85,17 +82,15 @@ public class MainActivity extends AppCompatActivity {
         setContentView(R.layout.activity_main);
 
 
-
         accept = (Button) findViewById(R.id.Accept);
         deny = (Button) findViewById(R.id.Deny);
         status = (TextView) findViewById(R.id.status);
         try {
-            if(fn.readSetting("requestId").equals("0")) {
+            if (fn.readSetting("requestId").equals("0")) {
                 status.setText("@string/No_req");
                 accept.setEnabled(false);
                 deny.setEnabled(false);
-            }
-            else {
+            } else {
                 status.setText("@string/req");
                 accept.setEnabled(true);
                 deny.setEnabled(true);
@@ -133,28 +128,65 @@ public class MainActivity extends AppCompatActivity {
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
 
-       menu.add("Unregister");
+        menu.add("Unregister");
 
         return super.onCreateOptionsMenu(menu);
     }
 
     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
+        final ProgressDialog progress = new ProgressDialog(this);
         System.out.println(item.getItemId());
-        switch(item.getItemId()) {
+        switch (item.getItemId()) {
             case 0:
                 try {
-                    fn.logout();
+                    JSONObject json = new JSONObject();
+                    json.put("function", "unregister");
+                    json.put("username", fn.readSetting("username"));
+                    json.put("password", fn.readSetting("password"));
+                    json.put("registerCode", fn.readSetting("registerCode"));
+                    json.put("requestId", "0");
+                    fn.makeRequest(fn.readSetting("serverUrl"), json.toJSONString(), new AsyncHttpResponseHandler() {
+                        @Override
+                        public void onFailure(int code, Header[] headers, byte[] responseBody, Throwable error) {
+                            status.setTextColor(Color.parseColor("#ff1700"));
+                            status.setText("HTTP ERROR: " + code);
+                            status.setVisibility(View.VISIBLE);
+                            progress.dismiss();
+                        }
+
+                        @Override
+                        public void onSuccess(int code, Header[] headers, byte[] responseBody) {
+                            String res = new String(responseBody);
+                            Object obj = JSONValue.parse(res);
+                            JSONObject response = (JSONObject) obj;
+
+                            if (response.get("result") != null && response.get("result").equals("0") || Integer.parseInt(response.get("result").toString()) == 0) {
+                                status.setTextColor(Color.parseColor("#04ff00"));
+                                status.setText("SUCCESS");
+                                try {
+                                    fn.logout();
+                                } catch (IOException e) {
+                                    e.printStackTrace();
+                                }
+                            } else {
+                                status.setText(response.get("result").toString() + "\n" + response.get("resultText").toString());
+                                status.setTextColor(Color.parseColor("#ff1700"));
+                            }
+                            status.setVisibility(View.VISIBLE);
+                            progress.dismiss();
+                            fn.cancelNotify();
+                        }
+                    });
+                    break;
                 } catch (IOException | JSONException e) {
                     e.printStackTrace();
                 }
-                break;
+                return true;
         }
         return true;
-
     }
-
-    private void buildReply(boolean grand, Button accept, Button deny) throws JSONException, IOException {
+        private void buildReply(boolean grand, Button accept, Button deny) throws JSONException, IOException {
         accept.setEnabled(false);
         deny.setEnabled(false);
 
@@ -172,9 +204,7 @@ public class MainActivity extends AppCompatActivity {
         json.put("notificationId", fn.readSetting("notificationId"));
         json.put("confirmation", grand ? "approved" : "cancelled");
 
-        AsyncHttpClient client = new AsyncHttpClient();
-
-        client.post(ctx, fn.readSetting("serverUrl"), new StringEntity(json.toJSONString()), "application/json", new AsyncHttpResponseHandler() {
+        fn.makeRequest(fn.readSetting("serverUrl"), json.toJSONString(), new AsyncHttpResponseHandler() {
             @Override
             public void onFailure(int code, Header[] headers, byte[] responseBody, Throwable error) {
                 status.setTextColor(Color.parseColor("#ff1700"));

+ 9 - 39
Android app/java/com/dev/deben/implementation/function.java

@@ -17,6 +17,9 @@ import android.os.PowerManager;
 import android.provider.Settings.Secure;
 import android.support.v4.app.NotificationCompat;
 
+import com.loopj.android.http.AsyncHttpClient;
+import com.loopj.android.http.AsyncHttpResponseHandler;
+
 import org.json.JSONException;
 import org.json.simple.JSONObject;
 import org.json.simple.JSONValue;
@@ -35,6 +38,8 @@ import java.net.URL;
 import java.util.HashMap;
 import java.util.Map;
 
+import cz.msebera.android.httpclient.entity.StringEntity;
+
 public class function {
     private static Context ctx;
     private static String settings;
@@ -107,16 +112,7 @@ public class function {
             }
             return value;
     }
-    public void logout() throws IOException, JSONException {
-        JSONObject json = new JSONObject();
-        json.put("function", "unregister");
-        json.put("username", readSetting("username"));
-        json.put("password", readSetting("password"));
-        json.put("registerCode", readSetting("registerCode"));
-        json.put("requestId", "0");
-
-        makeRequest("POST", readSetting("serverUrl"), json.toJSONString());
-
+    public void logout() throws IOException {
         System.out.println("Logging out");
         File del = new File(ctx.getFilesDir().getAbsolutePath(), file);
         del.delete();
@@ -262,35 +258,9 @@ public class function {
         String newCode = genRegCode(str);
         return code.equals(newCode);
     }
-
-    public String makeRequest(String type, String url, String body) throws IOException {
-        System.out.println(url);
-        System.out.println(body);
-        URL obj = new URL(url);
-        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
-
-        con.setRequestMethod(type);
-                con.setRequestProperty("content-type", "application/json");
-
-                con.setDoOutput(true);
-                DataOutputStream wr = new DataOutputStream(con.getOutputStream());
-                wr.writeBytes(body);
-                wr.flush();
-                wr.close();
-
-                BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream()));
-                StringBuilder sb = new StringBuilder();
-                String line = null;
-                while ((line = rd.readLine()) != null) {
-                    sb.append(line);
-                }
-                String response = sb.toString();
-
-                rd.close();
-                con.disconnect();
-
-                return response;
-
+    public void makeRequest(String url, String body, AsyncHttpResponseHandler handle) throws IOException {
+        AsyncHttpClient client = new AsyncHttpClient();
+        client.post(ctx, url, new StringEntity(body), "application/json", handle);
     }
     public void redirect(String act) {
         Intent intent;