Android向PHP发送数据不';有时我不会像预期的那样回来
我已经编写了一个Android应用程序,它连接到一个PHP文件。 一切都是正确的,我已经测试过了。 有时PHP文件不返回任何内容。 当我在正常工作的浏览器中测试Android应用程序时,这是真的。 我看不出两种不同模式之间有什么共同之处 获取发送数据的方法:Android向PHP发送数据不';有时我不会像预期的那样回来,php,android,post,get,Php,Android,Post,Get,我已经编写了一个Android应用程序,它连接到一个PHP文件。 一切都是正确的,我已经测试过了。 有时PHP文件不返回任何内容。 当我在正常工作的浏览器中测试Android应用程序时,这是真的。 我看不出两种不同模式之间有什么共同之处 获取发送数据的方法: private class GetData extends AsyncTask<String, String, String> { private ProgressDialog pDialog; private
private class GetData extends AsyncTask<String, String, String> {
private ProgressDialog pDialog;
private InputStream is = null;
private String url;
private String page_output = "";
public String where;
public GetData(String where, String UrlSend) {
this.url = UrlSend;
this.where = where;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
String title = "Please Wait ...";
String content = "Please Wait ...";
SpannableString stitle= new SpannableString(title);
stitle.setSpan(new RelativeSizeSpan(1f), 0, stitle.length(), 0);
stitle.setSpan(new ForegroundColorSpan(Color.DKGRAY), 0, stitle.length(), 0);
SpannableString scontent= new SpannableString(content);
scontent.setSpan(new RelativeSizeSpan(1f), 0, scontent.length(), 0);
scontent.setSpan(new ForegroundColorSpan(Color.DKGRAY), 0, scontent.length(), 0);
//pDialog.setTitle(stitle);
pDialog.setMessage(scontent);
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
page_output = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result->" + e.toString());
}
return page_output;
}
@Override
protected void onPostExecute(String page_output) {
pDialog.dismiss();
try {
// display output of internet page (page_output string)
new analyzeResponse(where, page_output).HandleResponse();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private class GetDataPost extends AsyncTask<String, String, String> {
private ProgressDialog pDialog;
private InputStream is = null;
private String url;
private String page_output = "";
public String where;
public String[] PK;
public String[] PD;
public GetDataPost (String where, String UrlSend, String[] PostKey, String[] PostData) {
this.url = UrlSend;
this.where = where;
this.PK = PostKey;
this.PD = PostData;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
String title = "Please Wait ...";
String content = "Please Wait ...";
SpannableString stitle= new SpannableString(title);
stitle.setSpan(new RelativeSizeSpan(1f), 0, stitle.length(), 0);
stitle.setSpan(new ForegroundColorSpan(Color.DKGRAY), 0, stitle.length(), 0);
SpannableString scontent= new SpannableString(content);
scontent.setSpan(new RelativeSizeSpan(1f), 0, scontent.length(), 0);
scontent.setSpan(new ForegroundColorSpan(Color.DKGRAY), 0, scontent.length(), 0);
//pDialog.setTitle(stitle);
pDialog.setMessage(scontent);
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
for (int i = 0; i < this.PK.length; i++) {
params.add(new BasicNameValuePair(this.PK[i], this.PD[i]));
}
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
page_output = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
return page_output;
}
@Override
protected void onPostExecute(String page_output) {
pDialog.dismiss();
try {
// display output of internet page (page_output string)
new analyzeResponse(where, page_output).HandleResponse();
} catch (Exception e) {
e.printStackTrace();
}
}
}
PHP文件工作正常
谢谢大家! 对于网络通信,我建议您使用截击()。它是一个Google库,用于通过网络发送HTTP请求,而无需处理所有多线程细节 这里有一个很好的教程,以及为什么要使用它:
error_reporting(E_ALL & ~E_NOTICE & ~E_USER_NOTICE);
header('Content-Type: text/html; charset=utf-8');
require_once 'Classes/Application/App/order.php';
require_once 'Classes/Application/practical/main.php';
$result = FALSE;
$msg = '';
@$code_get = $_GET['codeget'];
if (isset($code_get)) {
if (!empty($code_get) && $code_get == 'randcode') {
@$what_get = $_GET['whatget'];
if ($what_get == 'confirmevoucher') {
$voucher = $_GET['voucher'];
$confirme_voucher = voucher::confirme_voucher($voucher);
if ($confirme_voucher) {
$fraction = voucher::get_voucher_fraction($voucher);
$result = array (
'result' => TRUE,
'fraction' => $fraction
);
$result_json = json_encode($result);
echo $result_json;
}else {
$result = array (
'result' => FALSE
);
$result_json = json_encode($result);
echo $result_json;
}
}
}
}else {
$result = array (
'result' => FALSE
);
$result_json = json_encode($result);
echo $result_json;
}