Siema, od dawna rozwijam aplikacje na androida zrobioną w Cordovie dla pewnej firmy i teraz zachciało im się widgetu na ekranie startowym dla tej aplikacji. Przeszukałem neta ale nie znalazłem nic na temat widgetów w cordovie, więc zdecydowałem się na doklejenie kodu javy do skompilowanych plików cordovy - udało się, widget działa. Problem jest taki, że musze teraz zrobić z poziomu tego widgetu zaptanie POST po kliknięciu w przycisk w widgetcie. Przycisk już mam i znalazłem kod do wysyłania POSTa. Aplikacja się kompiluje, ale zapytanie się nie wysyła. Dodałem Logcata do projektu i okazuje się, że ta klasa z zapytaniem w ogóle się nie odpala, więc na razie zrobiłem, żeby się to zapytanie wysłało po starcie aplikacji
MainActivity
package com.fxteam.widget;
import android.os.Bundle;
import org.apache.cordova.*;
//fx
import android.util.Log;
public class MainActivity extends CordovaActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
//fx widget
Log.e("TEST", "MainActivity123");
super.onCreate(savedInstanceState);
// enable Cordova apps to be started in the background
Bundle extras = getIntent().getExtras();
if (extras != null && extras.getBoolean("cdvStartInBackground", false)) {
moveTaskToBack(true);
}
// Set by <content src="index.html" /> in config.xml
loadUrl(launchUrl);
HttpRequest request = new HttpRequest();
}
}
HttpRequest
package com.fxteam.widget;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.util.Log;
public class HttpRequest extends Activity {
String Name, Email, Login, Pass;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
Log.e("TEST", "POST REQUEST");
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_http_post_example);
try {
// CALL GetText method to make post method call
GetText();
}
catch(Exception ex)
{
//content.setText(" url exeption! " );
}
/*Button saveme=(Button)findViewById(R.id.save);
saveme.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v)
{
try {
// CALL GetText method to make post method call
}
catch(Exception ex)
{
//content.setText(" url exeption! " );
}
}
});*/
}
// Create GetText Metod
public void GetText() throws UnsupportedEncodingException
{
// Get user defined values
Name = "test";
Email = "test";
Login = "test";
Pass = "test";
// Create data variable for sent values to server
String data = URLEncoder.encode("name", "UTF-8")
+ "=" + URLEncoder.encode(Name, "UTF-8");
data += "&" + URLEncoder.encode("email", "UTF-8") + "="
+ URLEncoder.encode(Email, "UTF-8");
data += "&" + URLEncoder.encode("user", "UTF-8")
+ "=" + URLEncoder.encode(Login, "UTF-8");
data += "&" + URLEncoder.encode("pass", "UTF-8")
+ "=" + URLEncoder.encode(Pass, "UTF-8");
String text = "";
BufferedReader reader=null;
// Send data
try
{
// Defined URL where to send data
URL url = new URL("http://fx-team.fulara.com/team/adam/widget/index.php");
// Send POST data request
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write( data );
wr.flush();
// Get the server response
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response
while((line = reader.readLine()) != null)
{
// Append server response in string
sb.append(line + "\n");
}
text = sb.toString();
}
catch(Exception ex)
{
}
finally
{
try
{
reader.close();
}
catch(Exception ex) {}
}
// Show response on activity
//content.setText( text );
}
}
Czemu się ta klasa nie uruchamia?