Java Android httppost响应和加载新xml
我正在制作一个登录页面,并使用httppost登录网站的php脚本,但我不知道如何使用httppost响应,并使其在登录时加载一个新的xml。 从网站的回应应该是“确定”时,登录其权利Java Android httppost响应和加载新xml,java,android,http-post,Java,Android,Http Post,我正在制作一个登录页面,并使用httppost登录网站的php脚本,但我不知道如何使用httppost响应,并使其在登录时加载一个新的xml。 从网站的回应应该是“确定”时,登录其权利 public class MainActivity extends Activity { EditText email,password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate
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) {
//
}
}
}
公共类登录实现可运行{
私人字符串a;
私有字符串b;
公共登录(字符串a、字符串b){
这个a=a;
这个.b=b;
}
公开募捐{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://singiras.eu.pn/s/log.php");
试一试{
//添加您的数据
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“email”,this.a));
添加(新的BasicNameValuePair(“密码”,this.b));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
HttpEntity当前性=response.getEntity();
如果(最近性!=null){
Log.i(“响应”,EntityUtils.toString(resEntity));
}
}捕获(客户端协议例外e){
}捕获(IOE异常){
//
}
}
}
尝试使用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));
List name-valuepairs=new-ArrayList(2);
添加(新的BasicNameValuePair(“email”,this.a));
添加(新的BasicNameValuePair(“密码”,this.b));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
尝试使用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));
List name-valuepairs=new-ArrayList(2);
添加(新的BasicNameValuePair(“email”,this.a));
添加(新的BasicNameValuePair(“密码”,this.b));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
这是错误的代码。必须在UI线程之外使用IntentService
或AsyncTask
处理网络。您的响应是否为“ok”?否,服务器应该响应。WebnewMobile,你能给我举个例子吗?这是个糟糕的代码。必须在UI线程之外使用IntentService
或AsyncTask
处理网络。您的响应是否为“ok”?否,服务器应该响应。WebnewMobile,你能给我举个例子吗?