0

I have a method to insert data into server like this:

public void doInsert(){

    try {
        // setiap parameter yang akan dikirim melalui http
        // harus encode agar
        // dapat terbaca dengan baik oleh server
        String no_imei = URLEncoder.encode(noImei.getText().toString(), "utf-8");
        String nik = URLEncoder.encode(user.getText().toString(), "utf-8");
        String pass = URLEncoder.encode(password.getText().toString(), "utf-8");
        url += "?no_imei=" + no_imei + "&&nik=" + nik + "&&password=" + pass;
        getRequest(url);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

Another method that I have:

public void getRequest(String Url) {

    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(url);
    try {
        HttpResponse response = client.execute(request);
        Toast.makeText(this, "Tambah Data " + request(response) + " ",Toast.LENGTH_SHORT).show();
    } catch (Exception ex) {

        //Toast.makeText(this, "Tambah Data Gagal !", Toast.LENGTH_SHORT).show();
      }
}

And like this:

public static String request(HttpResponse response) {

    String result = "";
    try {
        InputStream in = response.getEntity().getContent();
        BufferedReader reader = new BufferedReader(
        new InputStreamReader(in));
        StringBuilder str = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {

            str.append(line + "\n");

        }
        in.close();
        result = str.toString();
        } catch (Exception ex) {
        result = "Error";
        }
        return result;

}

onClick method:

private View.OnClickListener onSave=new View.OnClickListener() 
{
    public void onClick(View v) 
    {
        //Dbhelper helper = new Dbhelper(UserForm.this);  
        Cursor c = helper.Login(almagId);

        if (noImei.getText().toString().equals("")||
            user.getText().toString().equals("")||
            password.getText().toString().equals("")
            ) {
            Toast.makeText(UserForm.this, "Data Harus di isi", Toast.LENGTH_LONG).show();

    }else if(c.moveToFirst()){
        if(noImei.getText().equals(c.getString(0))||
                user.getText().equals(c.getString(1))){
            Toast.makeText(UserForm.this, "Data Sudah ada", Toast.LENGTH_LONG).show();
            helper.close();
        }
    }else
    {
        doInsert(); 
        helper.insertUser(noImei.getText().toString(),user.getText().toString(),password.getText().toString());
            //Toast.makeText(UserForm.this, "Data Berhasil disimpan", Toast.LENGTH_LONG).show();

    }
        startActivity(new Intent(UserForm.this,MenuUtama.class));
        user.setText("");
        password.setText("");
         return;
    }
};

In the onClick method when response from server SUCCESS I want to do some activity. How can I do that? I tried with condition if my activity not running.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
akubabas
  • 473
  • 5
  • 13
  • 28
  • so what is your question??? or what problem you ran into doing _in onClick method when response from server SUCCESS i want to do some activity_ ?????? – Mayank Apr 02 '12 at 08:14
  • my question,,how i can do some activity when response from server success..i have try with `if(request(response).equals("SUCCESS")){ startActivityForResult(new Intent(this,MainMenu.class))}` but this intent can't running – akubabas Apr 02 '12 at 08:42

3 Answers3

0

You have to add your activity in AndroidManifest.xml. Refer this link : Using Intent in an Android application to show another activity

Community
  • 1
  • 1
Nimit
  • 1,714
  • 3
  • 22
  • 33
0

looks like login in your if statement is little off.

if(request(response).equals("SUCCESS")){ startActivityForResult(new Intent(this,MainMenu.class))} 

first, this does not make sense

if(request(response).equals("SUCCESS"))

what are you trying to do, cast request into response???

second:

.equals("SUCCESS"))

response is not an String object, are you looking for status code??

read over Android's HttpResponse

http://developer.android.com/reference/org/apache/http/HttpResponse.html

Example Code

if (response != null && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
//Do Something
} 
Mayank
  • 8,777
  • 4
  • 35
  • 60
  • oohh i see,i'm wrong with my code..can you tell me how create statusCode when response from server success??i'm newbie in here..thank you – akubabas Apr 02 '12 at 09:15
  • can you give simple code to get httpstatus??i'm confused just see status code..thank you – akubabas Apr 02 '12 at 10:17
0

You should probably go with

if (response != null && response.getStatusLine().getStatusCode() == 200) { }
timoschloesser
  • 2,790
  • 4
  • 25
  • 33