/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
User1 = (EditText) findViewById(R.id.UserEmailID);
Password1 = (EditText) findViewById(R.id.UserPasswordID);
}
public void clickButton(View view) {
String User = User1.getText().toString();
String Password = Password1.getText().toString();
switch (view.getId()) {
case R.id.LogInButton:
if (User.length() == 0) {
Toast.makeText(this, "User Email can't be blank", Toast.LENGTH_SHORT).show();
return;
} else if (Password.length() == 0) {
Toast.makeText(this, "Password can't be blank", Toast.LENGTH_SHORT).show();
return;
} else {
sign_in(User,Password);
Toast.makeText(this, "Stage3", 1).show();
}
}
}
public void sign_in (String User, String Password) {
try {
String URL = "MYURLHERE";
List <NameValuePair> params = new ArrayList <NameValuePair>();
params.add(new BasicNameValuePair ("user[email]", User));
params.add(new BasicNameValuePair ("user[password]", Password));
HttpPost httpPost = new HttpPost (URL);
HttpClient httpClient = new DefaultHttpClient ();
httpPost.setHeader("Accept", "application/json");
httpPost.setEntity(new UrlEncodedFormEntity (params, HTTP.UTF_8));
HttpResponse response = httpClient.execute(httpPost);
/*HttpEntity entity = response.getEntity();
StringBuilder sb = new StringBuilder();
String templine;
BufferedReader in = new BufferedReader (new InputStreamReader (entity.getContent()));
while ((templine = in.readLine())!=null){
sb.append (templine);
}
Toast.makeText(this, sb.toString(), 1).show();*/
} catch (UnsupportedEncodingException e) {
Toast.makeText(this, "UnsupportedEncodingException", 1).show();
} catch (ClientProtocolException e) {
Toast.makeText(this, "ClientProtocolException", 1).show();
}
catch (IOException e) {
Toast.makeText(this, "IOException", 1).show();
}
}
继续给我一个IOException错误...... 我已经在AndroidManifest文件上启用了Internet权限,但奇怪的是,请求通过并且它可以工作,但它一直在“HttpResponse response = httpClient.execute(httpPost);”行中抛出IOException。我不明白发生了什么。