1

我正在制作一个登录页面并使用 httppost 登录网站的 php 脚本,但我不知道如何使用 httppost 响应并使其在登录时加载新的 xml。正确登录时,网站的响应应该是“好的”。

   public class MainActivity extends Activity {

 EditText email,password;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);

    email=(EditText)findViewById(R.id.email);
    password=(EditText)findViewById(R.id.password);
 Button logindugme = (Button) findViewById(R.id.logindugme);

 logindugme.setOnClickListener(new View.OnClickListener() {

     @Override
     public void onClick(View v) {

        String a = email.getText().toString();
        String b = password.getText().toString();
        senddata(a,b);

     }

     public void senddata(String a, String b) {

        Runnable r = new Login(a, b);
        new Thread(r).start();
    }
    });
}

和一个登录类

   public class Login implements Runnable {

private String a;
private String b;

    public Login(String a, String b) {
   this.a = a;
   this.b = b;
  }

   public void run() {
   HttpClient httpclient = new DefaultHttpClient();
   HttpPost httppost = new HttpPost("http://singiras.eu.pn/s/log.php");

   try {
       // Add your data
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
       nameValuePairs.add(new BasicNameValuePair("email", this.a));
       nameValuePairs.add(new BasicNameValuePair("password", this.b));
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

       // Execute HTTP Post Request
       HttpResponse response = httpclient.execute(httppost);
       HttpEntity resEntity = response.getEntity();  
       if (resEntity != null) {    
           Log.i("RESPONSE",EntityUtils.toString(resEntity));
       }

   } catch (ClientProtocolException e) {

   } catch (IOException e) {
       //
   }






   }
 }
4

1 回答 1

1

尝试使用 httppost 将参数插入到请求的 URL 中,它为我解决了一个类似的问题。

HttpPost httppost = new HttpPost("http://singiras.eu.pn/s/log.php?email="+this.a+"&password="+this.b);

并删除:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("email", this.a));
nameValuePairs.add(new BasicNameValuePair("password", this.b));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
于 2013-04-09T19:56:07.597 回答