我对PHP, 和许多其他脚本语言很有经验,但我没有很多Java或Android的经验。
我正在寻找一种方法将POST数据发送到PHP脚本并显示结果。
解决方法:
通过这种方式,我们可以使用http post方法发送数据并获得结果
public class MyHttpPostProjectActivity extends Activity implements Listener {
private EditText usernameEditText;
private EditText passwordEditText;
private Button sendPostReqButton;
private Button clearButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
usernameEditText = (EditText) findViewById(R.id.login_username_editText);
passwordEditText = (EditText) findViewById(R.id.login_password_editText);
sendPostReqButton = (Button) findViewById(R.id.login_sendPostReq_button);
sendPostReqButton.set Listener(this);
clearButton = (Button) findViewById(R.id.login_clear_button);
clearButton.set Listener(this);
}
@Override
public void (View v) {
if(v.getId() == R.id.login_clear_button){
usernameEditText.setText(\"\");
passwordEditText.setText(\"\");
passwordEditText.setCursorVisible(false);
passwordEditText.setFocusable(false);
usernameEditText.setCursorVisible(true);
passwordEditText.setFocusable(true);
}else if(v.getId() == R.id.login_sendPostReq_button){
String givenUsername = usernameEditText.getEditableText().toString();
String givenPassword = passwordEditText.getEditableText().toString();
System.out.println(\"Given username :\" + givenUsername + \" Given password :\" + givenPassword);
sendPostRequest(givenUsername, givenPassword);
}
}
private void sendPostRequest(String givenUsername, String givenPassword) {
class SendPostReqAsyncTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
String paramUsername = params[0];
String paramPassword = params[1];
System.out.println(\"*** doInBackground ** paramUsername \" + paramUsername + \" paramPassword :\" + paramPassword);
HttpClient httpClient = new DefaultHttpClient();
// In a POST request, we don\'t pass the values in the URL.
//Therefore we use only the web page URL as the parameter of the HttpPost argument
HttpPost httpPost = new HttpPost(\"http://www.nirmana.lk/hec/android/postLogin.php\");
// Because we are not passing values over the URL, we should have a mechanism to pass the values that can be
//uniquely separate by the other end.
//To achieve that we use BasicNameValuePair
//Things we need to pass with the POST request
BasicNameValuePair usernameBasicNameValuePair = new BasicNameValuePair(\"paramUsername\", paramUsername);
BasicNameValuePair passwordBasicNameValuePAir = new BasicNameValuePair(\"paramPassword\", paramPassword);
// We add the content that we want to pass with the POST request to as name-value pairs
//Now we put those sending details to an ArrayList with type safe of NameValuePair
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
nameValuePairList.add(usernameBasicNameValuePair);
nameValuePairList.add(passwordBasicNameValuePAir);
try {
// UrlEncodedFormEntity is an entity composed of a list of url-encoded pairs.
//This is typically useful while sending an HTTP POST request.
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairList);
// setEntity() hands the entity (here it is urlEncodedFormEntity) to the request.
httpPost.setEntity(urlEncodedFormEntity);
try {
// HttpResponse is an interface just like HttpPost.
//Therefore we can\'t initialize them
HttpResponse httpResponse = httpClient.execute(httpPost);
// According to the JAVA API, InputStream constructor do nothing.
//So we can\'t initialize InputStream although it is not an interface
InputStream inputStream = httpResponse.getEntity().getContent();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
String bufferedStrChunk = null;
while((bufferedStrChunk = bufferedReader.readLine()) != null){
stringBuilder.append(bufferedStrChunk);
}
return stringBuilder.toString();
} catch (ClientProtocolException cpe) {
System.out.println(\"First Exception caz of HttpResponese :\" + cpe);
cpe.printStackTrace();
} catch (IOException ioe) {
System.out.println(\"Second Exception caz of HttpResponse :\" + ioe);
ioe.printStackTrace();
}
} catch (UnsupportedEncodingException uee) {
System.out.println(\"An Exception given because of UrlEncodedFormEntity argument :\" + uee);
uee.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(result.equals(\"working\")){
Toast.makeText(getApplicationContext(), \"HTTP POST is working...\", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(), \"Invalid POST req...\", Toast.LENGTH_LONG).show();
}
}
}
SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
sendPostReqAsyncTask.execute(givenUsername, givenPassword);
}
} 继续阅读与本文标签相同的文章
上一篇 :
6大分类,17大有用的Docker工具
下一篇 :
如何使用PHP发送电子邮件
-
Robotframework-RED-指定测试报告存放的路径
2026-05-15栏目: 教程
-
株洲首个数据中心站投运
2026-05-15栏目: 教程
-
微软Edge新标签页迎来新布局:新闻区可替换为工作相关内容
2026-05-15栏目: 教程
-
取快递件竟有BUG!这是恶意为之?双十一得好好注意了
2026-05-15栏目: 教程
-
“缺芯少屏”现状或将改善,静电消除及洁净室工程需求增长
2026-05-15栏目: 教程
