我想通过我的 Android 设备在 PHP 服务器上执行此查询:
{
"command": "REGISTER",
"data": {
"email": "EMAIL",
"login": "LOGIN",
"password": "PASSWORD",
"language": "USER_LANGUAGE"
}
public class AysnchronousTaskPost extends AsyncTask<String, Long, String> {
private static final String PATH = "http://alphabravo.com/myapp/api.php";
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
InputStream mInputStream;
String lineString = "";
String resultString = "";
StringBuilder mStringBuilder = null;
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
nameValuePairList.add(new BasicNameValuePair("command","register"));
nameValuePairList.add(new BasicNameValuePair("email","abc@gmail.com"));
nameValuePairList.add(new BasicNameValuePair("login", "abc"));
nameValuePairList.add(new BasicNameValuePair("password", "1205"));
nameValuePairList.add(new BasicNameValuePair("language", "en"));
try {
HttpClient mHttpClient = new DefaultHttpClient();
HttpPost mHttpPost = new HttpPost(PATH);
mHttpPost.setEntity(new UrlEncodedFormEntity(nameValuePairList));
HttpResponse mHttpResponse = mHttpClient.execute(mHttpPost);
HttpEntity mHttpEntity = mHttpResponse.getEntity();
StatusLine mStatusLine = mHttpResponse.getStatusLine();
int statusCodeString = mStatusLine.getStatusCode();
if (statusCodeString == 200) {
mInputStream = mHttpEntity.getContent();
mStringBuilder = new StringBuilder();
BufferedReader mBufferedReader = new BufferedReader(
new InputStreamReader(mInputStream, "iso-8859-1"), 8);
while ((lineString = mBufferedReader.readLine()) != null)
mStringBuilder.append(lineString + "/n");
}
resultString = mStringBuilder.toString();
} catch (Exception e) {
// TODO: handle exception
}
return resultString;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
我得到一个空的BufferedReader.ReadLine()
。我创建mHttpPost
正确吗?我也试过HTTPget
方法。但结果是一样的。请帮帮我
我已经在浏览器上检查了我的 php 脚本。我的 PHP 脚本工作正常。