Android httppost响应并加载新的xml

时间:2013-04-09 18:42:20

标签: java android http-post

我正在创建登录页面并使用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) {
       //
   }






   }
 }

1 个答案:

答案 0 :(得分: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));