1. Check out our app, Forums for Android! Download and leave feedback here!

Can't append text in new Intent

Discussion in 'Application Development' started by zuhra0105, Dec 21, 2009.

  1. zuhra0105

    zuhra0105 New Member
    Thread Starter
    5

    Dec 21, 2009
    1
    0
    5
    Hi,
    I am working on a student project and i have to make an android based application that will connect on a server having mysql databse(using servlets), and this server has automatic response on every update of database. I made standalone non android application in java and it works fine. When i use this code, adapted for android, to run on android emulator i can't see these results on new intent layout. I am using 2 intents, the first, starting intent, executes mysql query and it calls second intent that has a simple EditText compomnent and I want to see results in that second component. The problem is when second intent is started there is only black screen.

    Here is the code of the first Intent:

    Java:
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.*;
    import android.content.Intent; // postavljanje Intenta
    import android.net.Uri; // parsiranje URI-a




    public class projekt extends Activity implements OnClickListener {


    private CheckBox prvi;
    private CheckBox drugi;
    private Button gumb;
    static public String url;


    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    prvi = (CheckBox) findViewById(R.id.prvi);
    drugi = (CheckBox) findViewById(R.id.drugi);
    gumb = (Button) findViewById(R.id.gumb);
    gumb.setOnClickListener(this);


    }

    public void onClick(View v) {
    if(v == gumb) {
    url = upit(prvi,drugi);

    Intent prozor = new Intent (this,prozor.class);

    prozor.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
    prozor.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


    startActivity(prozor); }

    }

    public String upit (CheckBox prvi, CheckBox drugi) {

    if(prvi.isChecked() && drugi.isChecked()) {

    return "select%*%from%tablica" ;

    } else if (prvi.isChecked()) return "select%ime%from%tablica";

    else if(drugi.isChecked()) return "select%prezime%from%tablica";
    else return "ERROR!!";



    }
    }



    Here is the code of second intent:

    Java:

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.PowerManager;
    import android.os.PowerManager.WakeLock;
    import android.widget.*;
    import java.io.*;
    import org.xml.sax.SAXException;
    import org.xml.sax.XMLReader;
    import java.io.FileOutputStream;
    import java.io_OutputStreamWriter;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;


    public class prozor extends Activity {


    private int id=0;
    private EditText prikaz;
    private FileOutputStream fOut = null;
    private String path_local = "/data/data/projekt.android/files/bogovski.xml";
    String adresa = projekt.url;

    public static StringBuffer parsbuf = new StringBuffer();;




    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.prozor);


    prikaz = (EditText) findViewById(R.id.tekst);
    prikaz.append("...."); //-----------------------------------------------------------------TESTING SECOND INTENT
    prvakonekcija(adresa,id);


    }











    public void prvakonekcija(String address,int id) {

    URL page;
    String line;
    StringBuffer text = new StringBuffer();

    try {

    page = new URL("SERVER_URL_GOES_HERE"+projekt.url);

    try {
    HttpURLConnection konekcija1 = (HttpURLConnection) page.openConnection();
    konekcija1.connect();



    BufferedReader in1 = new BufferedReader(
    new InputStreamReader(
    konekcija1.getInputStream()));


    do {

    line = in1.readLine();
    if(line == null) break;
    text.append(line + "\n");
    } while (line != null);


    try {

    BufferedWriter out = new BufferedWriter(new FileWriter(path_local));
    out.write(text.toString());
    out.close();


    } catch (IOException e) {
    System.out.println(e.getMessage());
    }



    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser sp = spf.newSAXParser();
    XMLReader xr = sp.getXMLReader();
    Parser handler = new Parser(id);
    xr.setContentHandler(handler);
    xr.setErrorHandler(handler);
    sp.parse(new File(path_local), handler);

    System.out.println(parsbuf.toString());
    prikaz.append(parsbuf.toString());


    URL page1 = new URL("SERVER_URL_GOES_HERE");
    HttpURLConnection konekcija2 = (HttpURLConnection) page1.openConnection();
    konekcija2.connect();


    BufferedReader in2 = new BufferedReader(
    new InputStreamReader(
    konekcija2.getInputStream()));


    do {
    line = in2.readLine();
    } while (line == null);


    konekcija2.disconnect();
    konekcija1.disconnect();
    int salji = Integer.parseInt(line);
    prvakonekcija(adresa,salji);


    } catch (IOException ioe) {
    System.out.println(ioe.getMessage());

    } catch (SAXException e) {
    System.out.println(e.getMessage());

    } catch (Exception e) {
    System.out.println(e.getMessage());
    }

    } catch (MalformedURLException e) {
    System.out.println(e.getMessage());
    }

    }
    }



    I have a parser class that parses the xml and second intent is using this class i have added to my project. The code that is commented as "testing second intent" in onCreate method of second intent is shown only when i comment out the next line of code (calling of function prvakonekcija(adresa,id). It is interesting that application works with System.out.println and results can be seen in Logcat, but when i try to append it on EditText component nothing is happening (only black screen). I've uploaded screen shot of snippet of logcat log here: http://img689.imageshack.us/img689/2677/logcat127.jpg .

    In red circle you can see correct application results that I want to see on second Intent. Maybe the line in blue square (in Logcat) is causing problems?!?

    Thank you in advance for your time
     

    Advertisement

Share This Page

Loading...