2014-03-12 132 views
0

我已经使用httppost从site.but发送和retrive数据当我运行程序我不是得到所需要的数据参数传递给异步任务

package com.example.im3; 

import java.io.BufferedReader; 
import java.io.DataOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.ProtocolException; 
import java.net.URL; 

import android.app.Activity; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.RelativeLayout; 
import android.widget.Toast; 

public class Homepage extends Activity { 

    Button login1, login2; 
    LinearLayout l1; 
    RelativeLayout r1; 
    EditText un, pw; 
    private static final String TAG = "MyActivity"; 
    Toast toast; 
@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_homepage); 
     login2 = (Button) findViewById(R.id.button1); 
     login1 = (Button) findViewById(R.id.Login1); 
     l1 = (LinearLayout) findViewById(R.id.Homepage); 
     r1 = (RelativeLayout) findViewById(R.id.Loginpage2); 
     un = (EditText) findViewById(R.id.un); 
     pw = (EditText) findViewById(R.id.pw); 
     l1.setVisibility(l1.VISIBLE); 
     r1.setVisibility(r1.GONE); 
     login2.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       // TODO Auto-generated method stub 
       String s1 = un.getText().toString(); 
       String s2 = pw.getText().toString(); 

       if(s1.matches("")){ 

        Toast.makeText(Homepage.this, "enter something", toast.LENGTH_LONG).show(); 
       } 
       Log.d(TAG, "second login page"); 

       Abc task = new Abc(); 
       task.execute(s1, s2); 
      } 
     }); 
     login1.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       l1.setVisibility(v.GONE); 
       r1.setVisibility(v.VISIBLE); 
       Log.d(TAG, "home page"); 

      } 
     }); 
    } 

    private class Abc extends AsyncTask<String, String, String> { 
     String s1,s2; 

     @Override 
     protected String doInBackground(String... params) { 
      // TODO Auto-generated method stub 
      s1=un.getText().toString(); 
      s2=pw.getText().toString(); 
      String urlParameters = s1,s2; 
      String request = "https://beta135.hamarisuraksha.com/web/webservice/HamariSurakshaMobile.asmx/getIMSafeAccountInfoOnLogon"; 
      URL url; 

      try { 
       url = new URL(request); 
       HttpURLConnection connection = (HttpURLConnection) url 
         .openConnection(); 
       connection.setDoOutput(true); 
       connection.setDoInput(true); 
       connection.setInstanceFollowRedirects(false); 
       connection.setRequestMethod("POST"); 
       connection.setRequestProperty("Connection", "Keep-Alive"); 
       connection.setRequestProperty("Content-Type", 
         "application/x-www-form-urlencoded;");// boundary="+CommonFunctions.boundary 
       connection.setUseCaches(false); 

       DataOutputStream wr = new DataOutputStream(
         connection.getOutputStream()); 
       wr.writeBytes(urlParameters); 
       wr.flush(); 
       wr.close(); 


       int responseCode = connection.getResponseCode(); 
       /* 
       * System.out.println("\nSending 'POST' request to URL : " + 
       * url); System.out.println("Post parameters : " + 
       * urlParameters); 
       */ 
       System.out.println("Response Code : " + responseCode); 

       InputStream errorstream = connection.getErrorStream(); 

       BufferedReader br = null; 
       if (errorstream == null) { 
        InputStream inputstream = connection.getInputStream(); 
        br = new BufferedReader(new InputStreamReader(inputstream)); 
       } else { 
        br = new BufferedReader(new InputStreamReader(errorstream)); 
       } 
       String response = ""; 
       String nachricht; 
       while ((nachricht = br.readLine()) != null) { 
        response += nachricht; 
       } 

       // print result 
       // System.out.println(response.toString()); 
       return response.toString(); 

      } catch (MalformedURLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       return null; 
      } catch (ProtocolException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       return null; 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       return null; 
      } 
     } 
      protected void onPostExecute(String response) 
       { 
        // TODO Auto-generated method stub    
        super.onPostExecute(response); 
        Log.d(TAG,"on post execute"); 
        Toast.makeText(Homepage.this, "finally", toast.LENGTH_LONG).show(); 


     } 

    } 
} 

我已经使用httppost发送和retrive的数据来自site.but当我运行该程序即时通讯没有得到所需的数据 我想通过s1和s2参数到htp post method.but im nt得到如何去做

回答

0

你有你的参数为在你的doInBackground方法中的参数String... params。所以,你可以使用它们像这样:

protected String doInBackground(String... params) { 
    s1=params[0]; 
    s2=params[1]; 
    //... 
} 

相反的:

protected String doInBackground(String... params) { 
    s1=un.getText().toString(); 
    s2=pw.getText().toString(); 
    //... 
}