Vocabulary management from PCB, upload images, implemented

parent fa93884b
...@@ -37,7 +37,7 @@ android { ...@@ -37,7 +37,7 @@ android {
debug { debug {
resValue "string", "db_name", "PCB.db" resValue "string", "db_name", "PCB.db"
resValue "bool", "force_db_create", "true" resValue "bool", "force_db_create", "false"
resValue "bool", "ssl_connect", "false" resValue "bool", "ssl_connect", "false"
resValue "bool", "force_img_download", "false" resValue "bool", "force_img_download", "false"
resValue "integer", "netservice_timing", "20" resValue "integer", "netservice_timing", "20"
......
...@@ -23,6 +23,9 @@ package com.yottacode.net; ...@@ -23,6 +23,9 @@ package com.yottacode.net;
import android.os.StrictMode; import android.os.StrictMode;
import android.util.Log; import android.util.Log;
import com.google.gson.JsonParser;
import com.koushikdutta.async.parser.JSONObjectParser;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
...@@ -59,6 +62,9 @@ public class RestapiWrapper { ...@@ -59,6 +62,9 @@ public class RestapiWrapper {
} }
public void ask(String operation, Hashtable<String, String> params, String postOrGet, iRestapiListener listener) { public void ask(String operation, Hashtable<String, String> params, String postOrGet, iRestapiListener listener) {
ask(operation, params, postOrGet, false, listener);
}
public void ask(String operation, Hashtable<String, String> params, String postOrGet, boolean json, iRestapiListener listener) {
// call preExecute listener to show loading window // call preExecute listener to show loading window
listener.preExecute(); listener.preExecute();
// call AsynTask to perform network operation on separate thread // call AsynTask to perform network operation on separate thread
...@@ -74,6 +80,8 @@ public class RestapiWrapper { ...@@ -74,6 +80,8 @@ public class RestapiWrapper {
httpAsyncTaskParams.listener=listener; httpAsyncTaskParams.listener=listener;
httpAsyncTaskParams.url_params=params; httpAsyncTaskParams.url_params=params;
httpAsyncTaskParams.url=this.server + '/' + operation; httpAsyncTaskParams.url=this.server + '/' + operation;
httpAsyncTaskParams.json_params=json;
new HttpAsyncTask().executeOnExecutor(HttpAsyncTask.THREAD_POOL_EXECUTOR,httpAsyncTaskParams); new HttpAsyncTask().executeOnExecutor(HttpAsyncTask.THREAD_POOL_EXECUTOR,httpAsyncTaskParams);
} }
...@@ -145,20 +153,14 @@ public class RestapiWrapper { ...@@ -145,20 +153,14 @@ public class RestapiWrapper {
} }
return result; return result;
} }
public String POST(String surl, String request_method, Hashtable<String, String> params) { public String POST(String surl, String request_method, Hashtable<String, String> params, boolean json_params) {
URL url = null; URL url = null;
String response = ""; String response = "";
try { try {
url = new URL(surl); url = new URL(surl);
String sparams="";
for (String param : params.keySet()) {
String value = params.get(param);
sparams += param + '=' + value + '&';
}
if (sparams.length()>0) sparams=sparams.substring(0,sparams.length()-1);
Log.d(LOG_TAG, "POST url:" + surl+" params:"+sparams);
HttpURLConnection urlConnection = null; HttpURLConnection urlConnection = null;
urlConnection = (HttpsURLConnection) url.openConnection(); urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setReadTimeout(60000); urlConnection.setReadTimeout(60000);
...@@ -166,6 +168,26 @@ public class RestapiWrapper { ...@@ -166,6 +168,26 @@ public class RestapiWrapper {
urlConnection.setRequestMethod(request_method); urlConnection.setRequestMethod(request_method);
urlConnection.setDoInput(true); urlConnection.setDoInput(true);
urlConnection.setDoOutput(true); urlConnection.setDoOutput(true);
if (json_params) {
urlConnection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
urlConnection.setRequestProperty("Accept", "application/json, text/plain, */*");
}
String sparams;
if (json_params) {
sparams = params.get("json");
params.remove("json");
} else
sparams="";
for (String param : params.keySet()) {
String value = params.get(param);
if (param.equals("token"))
urlConnection.setRequestProperty("Authorization", "Bearer " + value);
else {
if (sparams.length() > 0) sparams += '&';
sparams += param + '=' + value;
}
}
//Send request //Send request
DataOutputStream wr = new DataOutputStream ( DataOutputStream wr = new DataOutputStream (
...@@ -178,8 +200,6 @@ public class RestapiWrapper { ...@@ -178,8 +200,6 @@ public class RestapiWrapper {
os.close(); os.close();
int responseCode=urlConnection.getResponseCode(); int responseCode=urlConnection.getResponseCode();
Log.d(LOG_TAG, "RESPONSECODE: " + responseCode);
String line; String line;
BufferedReader br = new BufferedReader(new InputStreamReader(responseCode == HttpsURLConnection.HTTP_OK BufferedReader br = new BufferedReader(new InputStreamReader(responseCode == HttpsURLConnection.HTTP_OK
? urlConnection.getInputStream() ? urlConnection.getInputStream()
...@@ -191,6 +211,7 @@ public class RestapiWrapper { ...@@ -191,6 +211,7 @@ public class RestapiWrapper {
Log.e(com.yottacode.net.RestapiWrapper.class.getName(), "Error:" + e.getLocalizedMessage() + " when asking for " + surl); Log.e(com.yottacode.net.RestapiWrapper.class.getName(), "Error:" + e.getLocalizedMessage() + " when asking for " + surl);
response=e.getMessage(); response=e.getMessage();
} }
return response; return response;
} }
...@@ -210,6 +231,7 @@ public class RestapiWrapper { ...@@ -210,6 +231,7 @@ public class RestapiWrapper {
protected String request_method; protected String request_method;
protected String url; protected String url;
protected Hashtable<String, String> url_params; protected Hashtable<String, String> url_params;
protected boolean json_params;
protected iRestapiListener listener; protected iRestapiListener listener;
protected String result; protected String result;
} }
...@@ -221,7 +243,7 @@ public class RestapiWrapper { ...@@ -221,7 +243,7 @@ public class RestapiWrapper {
protected HttpAsyncTaskParams doInBackground(HttpAsyncTaskParams... params) { protected HttpAsyncTaskParams doInBackground(HttpAsyncTaskParams... params) {
params[0].result = params[0].request_method.equalsIgnoreCase("GET") params[0].result = params[0].request_method.equalsIgnoreCase("GET")
? GET(params[0].url, params[0].url_params, null) ? GET(params[0].url, params[0].url_params, null)
: POST(params[0].url, params[0].request_method, params[0].url_params); : POST(params[0].url, params[0].request_method, params[0].url_params, params[0].json_params);
return params[0]; return params[0];
} }
......
...@@ -425,9 +425,9 @@ public class PictogramActivity extends Activity implements iVocabularyListener, ...@@ -425,9 +425,9 @@ public class PictogramActivity extends Activity implements iVocabularyListener,
public boolean onLongClick(View v) { public boolean onLongClick(View v) {
Log.d(LOG_TAG, "DELETE LONGCLICK..."); Log.d(LOG_TAG, "DELETE LONGCLICK...");
count_deletelong++; count_deletelong++;
Log.i("FERNANDO BORRAR", "SAVE LOCAL PICTO 1");
Picto picto=PCBcontext.getVocabulary().saveLocalPicto("/sdcard/DCIM/camera/javo.jpg", "javier", 7515, 2, 2); //Picto picto=PCBcontext.getVocabulary().saveLocalPicto("/sdcard/DCIM/camera/javo.jpg", "javier", 7515, 2, 2);
Log.i("FERNANDO BORRAR", "SAVE LOCAL PICTO 2:"+picto.get_id());
if (count_deletelong >= 3){ if (count_deletelong >= 3){
//Log.d(LOG_TAG, "SALTO A LOGIN"); //Log.d(LOG_TAG, "SALTO A LOGIN");
// Paso un parámetro a la SerialActivity, para controlar de dónde viene // Paso un parámetro a la SerialActivity, para controlar de dónde viene
......
...@@ -110,7 +110,7 @@ public class PictoUploader { ...@@ -110,7 +110,7 @@ public class PictoUploader {
(response == null ? -1 : response.getHeaders().code())); (response == null ? -1 : response.getHeaders().code()));
img_id=-1; img_id=-1;
} }
ion.dump(); // ion.dump();
return img_id; return img_id;
} }
...@@ -123,13 +123,16 @@ public class PictoUploader { ...@@ -123,13 +123,16 @@ public class PictoUploader {
final int id_stupicto[]=new int[1]; final int id_stupicto[]=new int[1];
Hashtable<String, String> params = new Hashtable<String, String>(4); Hashtable<String, String> params = new Hashtable<String, String>(4);
try { try {
params.put("attributes", new JSONObject().put("id_cat", Integer.toString(picto.get_category())) params.put("json", new JSONObject().put("attributes",
.put("coord_x", Integer.toString(picto.get_column())) (new JSONObject().put("id_cat", picto.get_category())
.put("coord_y", Integer.toString(picto.get_row())).toString()); .put("coord_x", picto.get_column())
.put("coord_y", picto.get_row())
.put("status",picto.get_status()))).toString());
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
Log.e(this.getClass().getCanonicalName(), " Error: " + e.getLocalizedMessage());
} }
PCBcontext.getRestapiWrapper().ask(PCBcontext.getPcbdb().getCurrentUser().get_restapi_operation_stu() + "/picto/"+id_picto, params, "post", new iRestapiListener() { PCBcontext.getRestapiWrapper().ask(PCBcontext.getPcbdb().getCurrentUser().get_restapi_operation_stu() + "/picto/"+id_picto, params, "post", true, new iRestapiListener() {
@Override @Override
public void preExecute() { public void preExecute() {
} }
...@@ -141,18 +144,19 @@ public class PictoUploader { ...@@ -141,18 +144,19 @@ public class PictoUploader {
@Override @Override
public void result(JSONObject result) { public void result(JSONObject result) {
Log.i(this.getClass().getCanonicalName(), "Attributes uploaded: " + result.toString()); Log.i(this.getClass().getCanonicalName(), " Attributes uploaded: " + result.toString());
try { /* try {
id_stupicto[0]=result.getJSONObject("picto").getInt("id"); id_stupicto[0]=result.getJSONObject("picto").getInt("id");
} catch (JSONException e) { } catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} Log.e(this.getClass().getCanonicalName(), "Error: " + e.getLocalizedMessage());
}*/
} }
@Override @Override
public void error(Exception e) { public void error(Exception e) {
Log.e(this.getClass().getCanonicalName(), "Error uploading attributes: " + e.getLocalizedMessage()); Log.e(this.getClass().getCanonicalName(), " Error uploading attributes: " + e.getLocalizedMessage());
id_stupicto[0]=-1; id_stupicto[0]=-1;
} }
}); });
...@@ -162,10 +166,10 @@ public class PictoUploader { ...@@ -162,10 +166,10 @@ public class PictoUploader {
/** /**
* if the a picto was included from the PCB, the translation is uploaded to the server * if the a picto was included from the PCB, the translation is uploaded to the server
*/ */
private void uploadTranslation( int id_stupicto) { private void uploadTranslation( int id_picto) {
Hashtable<String, String> params = new Hashtable<String, String>(3); Hashtable<String, String> params = new Hashtable<String, String>(3);
params.put("picto", Integer.toString(id_stupicto)); params.put("picto", Integer.toString(id_picto));
params.put("lang", PCBcontext.getPcbdb().getCurrentUser().get_lang_stu()); params.put("lang", PCBcontext.getPcbdb().getCurrentUser().get_lang_stu());
params.put("text", picto.get_translation()); params.put("text", picto.get_translation());
PCBcontext.getRestapiWrapper().ask("picto/exp", params, "post", new iRestapiListener() { PCBcontext.getRestapiWrapper().ask("picto/exp", params, "post", new iRestapiListener() {
...@@ -195,8 +199,8 @@ public class PictoUploader { ...@@ -195,8 +199,8 @@ public class PictoUploader {
public void upload() throws IOException { public void upload() throws IOException {
int img_id=uploadImg(this.picto); int img_id=uploadImg(this.picto);
if (img_id>0) { if (img_id>0) {
int stupicto_id=uploadAttributes(img_id); uploadAttributes(img_id);
uploadTranslation(stupicto_id); uploadTranslation(img_id);
} }
} }
......
...@@ -126,7 +126,7 @@ public class Img { ...@@ -126,7 +126,7 @@ public class Img {
Log.e(Img.class.getCanonicalName(), "Out of memory when decoding "+this.get_url()); Log.e(Img.class.getCanonicalName(), "Out of memory when decoding "+this.get_url());
} }
ByteArrayOutputStream outstream = new ByteArrayOutputStream(); ByteArrayOutputStream outstream = new ByteArrayOutputStream();
ls -l this.bitmap.setHasAlpha(true); this.bitmap.setHasAlpha(true);
this.bitmap.compress(Bitmap.CompressFormat.PNG, 100, outstream); this.bitmap.compress(Bitmap.CompressFormat.PNG, 100, outstream);
byte[] byteArray = outstream.toByteArray(); byte[] byteArray = outstream.toByteArray();
os.write(byteArray); os.write(byteArray);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment