2011-11-15 14 views
0

こんにちは、アンドロイドの開発から始まり、ボタンをクリックするとログインフォームを作成しなければなりません。http://www.mypage.com/?U=USUARI&K=PASSWORD。 ここで検索する私はVikas Patidarがアップロードしたnadを自分のニーズに合わせて変更しようと試みた方法を見つけました。しかし、私はそれが動作しないので、何か間違っていると思う。 私が間違っている箇所を教えてもらえますか?android login post method

これはコード `

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 Eライン上のいくつかの間違いはどこに行く:それは言う

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

: 「トークン 『バイト』に構文エラーが、このトークンを削除する」そして、私はそれを削除した場合、私はreactonでより多くのエラーを取得します。

どうすればよいですか?

答えて

0

The conn.setRequestMethod("POST"); 

を削除してconn.setDoOutput(true);はすでにあなたがPOST通信を使用していることを意味します。

次。

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

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

変更それを...これらを試してみると、それはオーケーを判明した場合、私に知らせて

あなたはほぼ似試しthis exampleをチェックアウトしたい場合は、あなたの条件...

お手数ですがお答えしてもらえるかもしれません。

+0

それはあなたの問題を解決しましたか...もしあなたがあなたの問題に対する答えとして私の答えを示してくれればいいですか...もしあなたが試したことを教えていただけたら、 – medampudi

+0

こんにちは、まずはありがとうあなたの助け! 動作しません。私が "request.write(parameters.getBytes());"を変更すると、 "write"オプションでエラーが発生する: 'OutputStreamWriter型のメソッドwrite(int)が引数(byte [])に対して使用できません 私が呼び出すURLに何も送信していないと思います。問題は私が/frmlogin.aspxへの物理的なアクセスを得ることができないということです...彼らは私にこのURLで「http://stats.serhstourism.com/?U=USUARI&K=PASSWORD」と私に言ってきました。フォームにアクセスする必要があります。私はこれにこだわっている。 – Franco

+0

btw、私もあなたが私に与えた例をチェックしましたが、私はアンドロイド開発の私の実際の知識のためにはあまりにも複雑だと思う:P – Franco

関連する問題