在 Android 上使用 HttpPost。我在网上查看并尝试了httpclient并执行了httppost。但是响应是相同的 URL。这是我的代码,我需要什么?谢谢。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_balance);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
String username = getIntent().getExtras().getString("username");
String password = getIntent().getExtras().getString("password");
//Building post parameters, key and value pair
List<NameValuePair> accountInfo = new ArrayList<NameValuePair>(2);
accountInfo.add(new BasicNameValuePair("inputEnterpriseId", username));
accountInfo.add(new BasicNameValuePair("password", password));
//Creating HTTP client
HttpClient httpClient = new DefaultHttpClient();
//Creating HTTP Post
HttpPost httpPost = new HttpPost(LOGIN_URL);
/* Not needed?
BasicHttpParams params = new BasicHttpParams();
params.setParameter("ENT_ID", username);
params.setParameter("PASSWORD", password);
httpPost.setParams(params);
*/
//Url Encoding the POST parameters
try {
httpPost.setEntity(new UrlEncodedFormEntity(accountInfo));
}
catch (UnsupportedEncodingException e) {
// writing error to Log
e.printStackTrace();
startActivity(new Intent(this, InputAccountActivity.class));
}
HttpResponse response = null;
InputStreamReader iSR = null;
String source = null;
// Making HTTP Request
try {
response = httpClient.execute(httpPost);
source = inputStreamToString(response.getEntity().getContent()).toString(); //gets website source
// writing response to log
Log.d("Http Response:", response.toString());
Log.d("hi", source);
/*
iSR = new InputStreamReader(response.getEntity().getContent());
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
Log.d("RESPONSE",resEntity.toString());
}
BufferedReader br = new BufferedReader(iSR);
source = "";
while((source = br.readLine()) != null)
{
source += br.readLine();
Log.d(TAG,source.trim());
}
*/
} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
startActivity(new Intent(this, InputAccountActivity.class));
}
}