2012-04-06 95 views
0

我在兩個活動之間傳遞兩個字符串,出於某種奇怪的原因,字符串未被傳遞。我已經完成了所有正確的協議,但似乎沒有任何工作,儘管修改了幾個小時的代碼,我確信這是一個簡單的解決方案,但我不知道如何。字符串未能在兩個活動之間傳遞

第一類:

public class LogIn extends Activity implements OnClickListener { 

    Button ok, back, exit; 
    TextView result; 
    EditText pword; 
    String password; 
    EditText uname; 
    String username; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // Login button clicked 
     ok = (Button) findViewById(R.id.btn_login); 
     ok.setOnClickListener(this); 

     result = (TextView) findViewById(R.id.lbl_result); 

    } 
    //create bracket. 

    public void postLoginData() { 
     uname = (EditText) findViewById(R.id.txt_username); 
     uname.getText().toString(); 


     pword = (EditText) findViewById(R.id.txt_password); 
     pword.getText().toString(); 
     Bundle basket = new Bundle(); 
     basket.putString("keypass", password); 
     basket.putString("keyuname", username); 
     Intent a = new Intent(LogIn.this, ChatService.class); 
     a.putExtras(basket); 
     startActivity(a); 


     // Create a new HttpClient and Post Header 
     HttpClient httpclient = new DefaultHttpClient(); 

     /* login.php returns true if username and password is equal to saranga */ 
     HttpPost httppost = new HttpPost("http://gta5news.com/login.php"); 

     try { 
      // Add user name and password 


      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
      nameValuePairs.add(new BasicNameValuePair("username", username)); 
      nameValuePairs.add(new BasicNameValuePair("password", password)); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

      // Execute HTTP Post Request 
      Log.w("HttpPost(Login)", "Execute HTTP Post Request(Login 1)"); 
      HttpResponse response = httpclient.execute(httppost); 

      String str = inputStreamToString(response.getEntity().getContent()) 
        .toString(); 
      Log.w("HttpPost", str); 

      if (str.toString().equalsIgnoreCase("true")) { 
       Log.w("HttpPost(Login2)", "TRUE"); 
       result.setText("Login successful"); 
       Intent login = new Intent(LogIn.this, ChatService.class); 
       startActivity(login); 

      }else { 
       Log.w("HttpPost(Login(3)", "FALSE"); 
       result.setText(str); 
      } 

     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    private StringBuilder inputStreamToString(InputStream is) { 
     String line = ""; 
     StringBuilder total = new StringBuilder(); 
     // Wrap a BufferedReader around the InputStream 
     BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
     // Read response until the end 
     try { 
      while ((line = rd.readLine()) != null) { 
       total.append(line); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     // Return full string 
     return total; 
    } 

    public void onClick(View view) { 
     if (view == ok) { 

      postLoginData(); 
      InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
      imm.hideSoftInputFromWindow(pword.getWindowToken(), 0); 
     } 
     // Click end 
    } 
    // if statement 
} 

// class ends here 

二等:

public class ChatService extends ListActivity { 
    /** Called when the activity is first created. */ 

    BufferedReader in = null; 
    String data = null; 
    List headlines; 
    List links; 
    String GotPass; 
    String GotUname; 




    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //get strings 
     Bundle gotData = getIntent().getExtras(); 
     if(gotData !=null) { 
     GotPass = gotData.getString("keypass"); 
     GotUname = gotData.getString("keyuname"); 


     try { 
      //listview method 
      ContactsandIm(); 
     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (URISyntaxException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     try { 
      CheckLogin(); 
     } catch (UnsupportedEncodingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
    } 


    private void CheckLogin() throws UnsupportedEncodingException { 
// posts login data from "LogIn" class 
     // Create a new HttpClient and Post Header 
       HttpClient httpclient = new DefaultHttpClient(); 

       /* login.php returns true if username and password is equal to saranga */ 
       HttpPost httppost = new HttpPost("http://gta5news.com/login.php"); 

       try { 
        // Add user name and password 


        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
        nameValuePairs.add(new BasicNameValuePair("username", GotUname)); 
        nameValuePairs.add(new BasicNameValuePair("password", GotPass)); 
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

        // Execute HTTP Post Request 
        Log.w("HttpPost(Login)", "Execute HTTP Post Request(ChatService 1)"); 
        HttpResponse response = httpclient.execute(httppost); 

        String str = inputStreamToString(response.getEntity().getContent()) 
          .toString(); 
        Log.w("HttpPost", str); 

        if (str.toString().equalsIgnoreCase("true")) { 
         Log.w("HttpPost(ChatService 2)", "TRUE"); 
         // make toast if str.equals("True") 
         Toast.makeText(getApplicationContext(), "Yayayaya, loged in", Toast.LENGTH_LONG); 


        }else { 
         Log.w("HttpPost(ChatService 3", "FALSE"); 
         Toast.makeText(getApplicationContext(), "failed", Toast.LENGTH_LONG); 
        } 

       } catch (ClientProtocolException e) { 
        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 

      private StringBuilder inputStreamToString(InputStream is) { 
       String line = ""; 
       StringBuilder total = new StringBuilder(); 
       // Wrap a BufferedReader around the InputStream 
       BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
       // Read response until the end 
       try { 
        while ((line = rd.readLine()) != null) { 
         total.append(line); 
        } 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       // Return full string 
       return total; 
      } 

    public void ContactsandIm() throws URISyntaxException, 
      ClientProtocolException, IOException { 
     headlines = new ArrayList(); 

     // TODO Auto-generated method stub 
     BufferedReader in = null; 
     String data = null; 

     HttpClient get = new DefaultHttpClient(); 
     URI website = new URI("http://www.gta5news.com/test.php"); 
     HttpGet webget = new HttpGet(); 
     webget.setURI(website); 
     HttpResponse response = get.execute(webget); 
     Log.w("HttpPost", "Execute HTTP Post Request"); 
     in = new BufferedReader(new InputStreamReader(response.getEntity() 
       .getContent())); 
     StringBuffer sb = new StringBuffer(""); 
     String l =""; 
     String nl =""; 
     while ((l =in.readLine()) !=null) { 
      sb.append(l + nl); 
     } 
     in.close(); 
     data = sb.toString(); 
     if(data.contains("null")); 
     ListView lv = getListView(); 
     lv.setTextFilterEnabled(true); 

     headlines.add(data); 


     ArrayAdapter adapter = new ArrayAdapter(this, 
       android.R.layout.simple_list_item_1, headlines); 

     setListAdapter(adapter); 



    } 

    // end bracket for "ContactsandIm" 

} 
+0

究竟是什麼問題?字符串不在捆綁中?該應用程序崩潰?等等更多信息是必需的。 – Kuffs 2012-04-06 19:59:40

+0

字符串不在捆綁中,抱歉沒有明確說明。 – TheBlueCat 2012-04-06 20:29:19

回答

1

嘗試這種方式。

Intent a = new Intent(context, MyActivity.class); 
    a.putExtra("String1", "Hello World"); 
    context.startActivity(a); 

Bundle extras = getIntent().getExtras(); 
    String s1 = extras.getString("String1"); 
+0

不,崩潰的應用程序,只是不工作。 – TheBlueCat 2012-04-06 19:44:08

+0

這只是一個片段。你必須在正確的地方使用它,並檢查空引用等。基本上我的觀點是使用這種方法來添加你的額外的意圖,而不是將它們添加到你添加到意圖的捆綁。它對我來說非常合適。 – Kuffs 2012-04-06 19:49:57

+0

我知道,我實現了你的代碼片段和所有的空引用,但它仍然無法工作。 – TheBlueCat 2012-04-06 20:11:14

0

您應該添加在Mainfest.xml兩個2活動文件

+0

我有。他們都在清單中。 – TheBlueCat 2012-04-06 20:10:37

0

檢查你有清單文件註冊您的活動。

+0

我有。他們都在清單中。 – TheBlueCat 2012-04-06 20:10:43