从php获取回显消息
我正在使用httppost方法向php发送3个参数。这3个参数在mysql查询中用于检索特定记录。 但是,我的json.getInt()指向null。有没有办法让我收到回音信息从php获取回显消息,php,android,json,Php,Android,Json,我正在使用httppost方法向php发送3个参数。这3个参数在mysql查询中用于检索特定记录。 但是,我的json.getInt()指向null。有没有办法让我收到回音信息 protected String doInBackground(String... args) { // updating UI from Background Thread List<NameValuePair> params =
protected String doInBackground(String... args) {
// updating UI from Background Thread
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("item_ID", itemID));
params.add(new BasicNameValuePair("booking_StartDate", checkStartDate));
params.add(new BasicNameValuePair("booking_EndDate", checkEndDate));
JSONObject json = jParser.makeHttpRequest(url_checkAvaibility, "POST", params);
// Check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
if(records == null){
success =0;
}
else
{
success =1;
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
JSON makeHttpRequest方法
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
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);
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
/*try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data "+json + e.toString());
}*/
// return JSON String
try {
return new JSONObject(json);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
这是我得到的最新日志
03-12 03:54:28.956: D/JSON-OUTPUT(22405): <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>403 Forbidden</title></head><body><h1>Forbidden</h1><p>You don't have permission to access /checkAva.phpon this server.</p><hr><address>Apache/2.4.9 (Win64) PHP/5.5.12 Server at 192.168.1.132 Port 80</address></body></html>
03-12 03:54:28.961: E/JSON Parser(22405): Error parsing data <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>403 Forbidden</title></head><body><h1>Forbidden</h1><p>You don't have permission to access /checkAva.phpon this server.</p><hr><address>Apache/2.4.9 (Win64) PHP/5.5.12 Server at 192.168.1.132 Port 80</address></body></html>org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
03-12 03:54:28.956:D/JSON-OUTPUT(22405):403禁止禁止您没有权限访问此服务器上的/checkAva.phpon。Apache/2.4.9(Win64)PHP/5.5.12服务器192.168.1.132端口80
03-12 03:54:28.961:E/JSON解析器(22405):错误解析数据403禁止禁止您没有权限访问此服务器上的/checkAva.phpon。查看错误后,您需要执行以下操作:
您需要编辑etc/apache2/httpd.conf,并确保已授予require all
,这将允许网络中的任何人访问您服务器上的文件夹注意:我在mac上
DocumentRoot "/Users/USER_NAME_HERE/FOLDER_YOU_SERVE_FROM/"
<Directory "/Users/USER_NAME_HERE/FOLDER_YOU_SERVE_FROM/">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options FollowSymLinks Multiviews
MultiviewsMatch Any
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>
DocumentRoot”/Users/USER\u NAME\u HERE/FOLDER\u YOU\u service\u FROM/“
#
#选项指令的可能值为“无”、“全部”,
#或以下任何组合:
#索引包括以下符号链接符号链接所有者匹配执行CGI多视图
#
#请注意,“多视图”必须显式命名为*--“所有选项”
#不给你。
#
#期权指令既复杂又重要。请看
# http://httpd.apache.org/docs/2.4/mod/core.html#options
#了解更多信息。
#
选项如下符号链接多视图
多视图匹配任意
#
#AllowOverride控制可在.htaccess文件中放置的指令。
#它可以是“全部”、“无”或关键字的任意组合:
#AllowOverride文件信息AuthConfig限制
#
允许超越所有
#
#控制谁可以从此服务器获取内容。
#
要求所有授权
此代码是否在浏览器中回显?此外,还可以提供此方法jParser.makeHttpRequest()。这个json文件位于哪里?您的本地主机?是的,它在浏览器中回显,这是回显消息。{“records”:[{“booking_ID”:“15”,“booking_Title”:“abc”}],“success”:1}是的,在localhost中。我已经上传了jparser.makeHttpRequest()@internal_class7你是在模拟器上还是在手机上进行测试?发布你的日志,它看起来jsonobject是空的。我在手机上进行测试,但是应用程序会因为空指针而停止。我尝试在高级Rest客户机上运行php,效果很好@我把同样的WiFi也放进去了。在我的手机上,所有其他从php插入或检索记录的功能都在工作,只是这个方法我无法读取响应。
> 03-12 02:36:57.928: E/AndroidRuntime(12689): FATAL EXCEPTION:
> AsyncTask #1 03-12 02:36:57.928: E/AndroidRuntime(12689):
> java.lang.RuntimeException: An error occured while executing
> doInBackground() 03-12 02:36:57.928: E/AndroidRuntime(12689): at
> android.os.AsyncTask$3.done(AsyncTask.java:278) 03-12 02:36:57.928:
> E/AndroidRuntime(12689): at
> java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
> 03-12 02:36:57.928: E/AndroidRuntime(12689): at
> java.util.concurrent.FutureTask.setException(FutureTask.java:124)
> 03-12 02:36:57.928: E/AndroidRuntime(12689): at
> java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
> 03-12 02:36:57.928: E/AndroidRuntime(12689): at
> java.util.concurrent.FutureTask.run(FutureTask.java:137) 03-12
> 02:36:57.928: E/AndroidRuntime(12689): at
> android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208) 03-12
> 02:36:57.928: E/AndroidRuntime(12689): at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
> 03-12 02:36:57.928: E/AndroidRuntime(12689): at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
> 03-12 02:36:57.928: E/AndroidRuntime(12689): at
> java.lang.Thread.run(Thread.java:856) 03-12 02:36:57.928:
> E/AndroidRuntime(12689): Caused by: java.lang.NullPointerException
> 03-12 02:36:57.928: E/AndroidRuntime(12689): at
> com.example.mysqltesting.ShowAvaibility$GetProductDetails.doInBackground(ShowAvaibility.java:89)
> 03-12 02:36:57.928: E/AndroidRuntime(12689): at
> com.example.mysqltesting.ShowAvaibility$GetProductDetails.doInBackground(ShowAvaibility.java:1)
> 03-12 02:36:57.928: E/AndroidRuntime(12689): at
> android.os.AsyncTask$2.call(AsyncTask.java:264) 03-12 02:36:57.928:
> E/AndroidRuntime(12689): at
> java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
> 03-12 02:36:57.928: E/AndroidRuntime(12689): ... 5 more
03-12 03:54:28.956: D/JSON-OUTPUT(22405): <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>403 Forbidden</title></head><body><h1>Forbidden</h1><p>You don't have permission to access /checkAva.phpon this server.</p><hr><address>Apache/2.4.9 (Win64) PHP/5.5.12 Server at 192.168.1.132 Port 80</address></body></html>
03-12 03:54:28.961: E/JSON Parser(22405): Error parsing data <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>403 Forbidden</title></head><body><h1>Forbidden</h1><p>You don't have permission to access /checkAva.phpon this server.</p><hr><address>Apache/2.4.9 (Win64) PHP/5.5.12 Server at 192.168.1.132 Port 80</address></body></html>org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
DocumentRoot "/Users/USER_NAME_HERE/FOLDER_YOU_SERVE_FROM/"
<Directory "/Users/USER_NAME_HERE/FOLDER_YOU_SERVE_FROM/">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options FollowSymLinks Multiviews
MultiviewsMatch Any
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride All
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>