2011-11-15 103 views
0

嗨我開始與Android開發,我必須創建一個登錄表單點擊一個按鈕,它會發送這個:http://www.mypage.com/?U=USUARI&K=PASSWORD。 在這裏搜索我已經創建了Vikas Patidar上傳nad的方法,我嘗試將其修改爲符合我的需要。但我認爲有些問題,因爲它不起作用。 你能告訴我我錯在哪裏嗎?android登錄後的方法

這是代碼 `

public class HttpLogin extends Activity { /** Called when the activity is first created. */ private Button login; private EditText U, K; 

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

    login = (Button) findViewById(R.id.login); 
    U = (EditText) findViewById(R.id.U); 
    K = (EditText) findViewById(R.id.K); 

    login.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      String mUsername = U.getText().toString(); 
      String mPassword = K.getText().toString(); 

      tryLogin(mUsername, mPassword); 
     } 
    }); 
} 

protected void tryLogin(String mUsername, String mPassword) 
{   
    HttpURLConnection connection; 
    OutputStreamWriter request = null; 

     URL url = null; 
     String response = null;   
     String parameters = "U="+mUsername+"&K="+mPassword; 

     try 
     { 
      url = new URL("http://http://www.mypage.com/content/frmLogin.aspx"); 
      connection = (HttpURLConnection) url.openConnection(); 
      connection.setDoOutput(true); 
      connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
      connection.setRequestMethod("POST");  

      request = new OutputStreamWriter(connection.getOutputStream()); 
      request.write(parameters); 
      request.flush(); 
      request.close();    
      String line = "";    
      InputStreamReader isr = new InputStreamReader(connection.getInputStream()); 
      BufferedReader reader = new BufferedReader(isr); 
      StringBuilder sb = new StringBuilder(); 
      while ((line = reader.readLine()) != null) 
      { 
       sb.append(line + "\n"); 
      } 
      // Response from server after login process will be stored in response variable.     
      response = sb.toString(); 
      // You can perform UI operations here 
      Toast.makeText(this,"Message from Server: \n"+ response, 0).show();    
      isr.close(); 
      reader.close(); 

     } 
     catch(IOException e) 
     { 
      // Error 
     } 
} 
´ 

和XML看起來是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<EditText 
android:hint="Username" 
android:id="@+id/U" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"> 
</EditText> 

<EditText 
android:hint="Password" 
android:id="@+id/K" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:inputType="textPassword"> 
</EditText> 

<Button 
android:text="Iniciar Sessió" 
android:id="@+id/login" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"> 
</Button> 
</LinearLayout> 

現在我用HTTP GET方法試圖和我得到這個

packge com.android.v3; 

import java.io.IOException; 
import java.io.InputStream; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.client.methods.HttpRequestBase; 
import org.apache.http.impl.client.DefaultHttpClient; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class v3act extends Activity { 
    TextView Tname,Tpass; 
    EditText Ename,Epass; 
    Button btnCreate; 
    String n=null; 
    String contentOfMyInputStream1; 
    String output = null; 


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

     btnCreate = (Button) findViewById(R.id.btnGen); 
     Tname = (TextView) findViewById(R.id.txtName); 
     Ename = (EditText) findViewById(R.id.editName); 
     Tpass = (TextView) findViewById(R.id.txtPass); 
     Epass = (EditText) findViewById(R.id.editPass); 




     btnCreate.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
      Tname.setText(""); 
      // Thread thread = new Thread(); 
      String st1; 

      st1=Ename.getText().toString(); 
      //thread.start(); 

      Tpass.setText(""); 
      // Thread thread = new Thread(); 
       String st2; 

       st2=Epass.getText().toString(); 
      //thread.start(); 

      try { 
       output ="http://www.mypage.com/?U="+st1+"K="+st2; 
       downloadUrl(output); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      if (output != null) 
      { 
       Tname.setText(output); 
      } 


      } 
      }); 


    } 

    public String downloadUrl(String url) throws IOException{ 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpRequestBase httpRequest = null; 
     HttpResponse httpResponse = null; 
     InputStream inputStream = null; 
     String response = ""; 
     StringBuffer buffer = new StringBuffer(); 

     httpRequest = new HttpGet(url); 

     httpResponse = httpclient.execute(httpRequest); 
     inputStream = httpResponse.getEntity().getContent(); 
     int contentLength = (int) httpResponse.getEntity().getContentLength(); 
     if (contentLength < 0){ 
      // Log.e(TAG, "The HTTP response is too long."); 
     } 
     byte[] data = "8" byte[256]; 
     int len = 0; 
     while (-1 != (len = inputStream.read(data))) 
     { 
      buffer.append(new String(data, 0, len)); 
     } 

     inputStream.close(); 

     response = buffer.toString(); 

     return response; 

    } 


} 

但我hav Ë上線的一些錯誤,其中有云:

byte[] data = "8" byte[256]; 

它說:「令牌‘字節’語法錯誤,刪除此令牌」 如果我刪除,我在reacton獲得更多的錯誤。

我該怎麼辦?

回答

0

刪除

The conn.setRequestMethod("POST"); 

然後conn.setDoOutput(true);已經意味着您在使用POST通信。

下一個。它

 request.write(parameters); 
     request.flush(); 
     request.close(); 

變化

 request.write(parameters.getBytes()); 
     request.flush(); 
     //request.close();//it is closing prematurly so you will definitely not get any response at all... 

嘗試這些了,讓我知道,如果它原來好......

如果你想看看嘗試 this example這看起來幾乎類似於

您條件...

可能會幫助太....迴應。

+0

它解決了你的問題...如果是的話,你會介意我的答案作爲你的問題的答案..如果沒有...請問你告訴我你試過的東西, – medampudi

+0

嗨,首先感謝你的幫助! 它不起作用。如果我改變「request.write(parameters.getBytes());」它使我在「寫入」選項上出現錯誤:'OutputStreamWriter類型的方法write(int)不適用於參數(byte []) 我認爲它沒有發送任何內容給我給調用的url。問題是我無法通過物理方式訪問/frmlogin.aspx ...他們告訴我,通過以下網址:'http://stats.serhstourism.com/?U=USUARI&K=PASSWORD',我必須使用表單訪問。我被困在這裏。 – Franco

+0

順便說一句,我也檢查了你給我的例子,但我認爲這對我的Android開發的實際知識來說太複雜了:P – Franco