从第一张图片中我发送请求的两项活动 到两个不同的PHP php文件,我得到了有效的Android活动响应。
在第二张图片中,将上述两个php文件合并为一个 php文件,当从Activity 1发送请求到php时,我无法做到 至 得到回应和活动2。
private void login(final String textViewNameq, String textViewNameqw,String day) {
class LoginAsync extends AsyncTask<String, Void, String> {
private Dialog loadingDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
loadingDialog = ProgressDialog.show(MainActivity.this, "Please wait", "Loading...");
}
@Override
protected String doInBackground(String... params) {
String uname = params[0];
String pass = params[1];
String day=params[2];
InputStream is = null;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", uname));
nameValuePairs.add(new BasicNameValuePair("password", pass));
nameValuePairs.add(new BasicNameValuePair("day", day));
String result = null;
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(
"Your php link goes here");
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
result = sb.toString();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
和我的PHP代码在这里,
<?php
include("my php file path");
// if (!empty($_POST['username']) && !empty($_POST['password'])){
$Fromname = trim($_POST['username']);
$Toname = trim($_POST['password']);
//$Fromdate=$_POST['iddate'];
//$Fromd = date("Y-m-d", strtotime($Fromdate));
$from_id=trim($_POST['idss']);
$to_id=trim($_POST['iddd']);
// }
$url="http://my url with API key goes here";
$headers = array('Content-Type: application/json');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_ENCODING , "gzip");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$page = curl_exec($ch);
curl_close($ch);
$result=json_decode($page,true);
if(empty($result)){
$page=file_get_contents($url);
$result=json_decode($page,true);
}
答案 0 :(得分:0)
可以从同一个php文件中获取所需的响应。您需要做的就是在请求方法中发送所需信息并在php文件中进行相应处理并生成响应。如果你展示你的Php脚本,我们可以帮助你更好。