Java 获取片段中的JSON数据时连接到服务器时出错
我试图从php文件中获取JSON数据,代码如下所示,但每次我运行代码时,系统总是在Java 获取片段中的JSON数据时连接到服务器时出错,java,android,json,android-fragments,httpclient,Java,Android,Json,Android Fragments,Httpclient,我试图从php文件中获取JSON数据,代码如下所示,但每次我运行代码时,系统总是在result=false指示未获取数据时返回Toast。导致数据提取失败的问题可能是什么 下面是包含JSON数据的inded.php文件的代码 <?php $arr = array ( "properties" => array( array( "companyName" => "Motorola",
result=false
指示未获取数据时返回Toast。导致数据提取失败的问题可能是什么
下面是包含JSON数据的inded.php文件的代码
<?php
$arr = array ( "properties" => array(
array(
"companyName" => "Motorola",
"name" => "Moto"
),
array(
"companyName" => "Sony",
"name" => "xPeria"
),
array(
"companyName" => "Infinix",
"name" => "S2 Pro"
)
)
);
echo json_encode($arr);
?>
下面是我的JSONAsynTask()
类的代码
class JSONAsynTask extends AsyncTask<String, Void, Boolean> {
String result;
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(getActivity());
dialog.setMessage("Loading, please wait");
dialog.setTitle("Connecting server");
dialog.show();
dialog.setCancelable(false);
}
@Override
protected Boolean doInBackground(String... urls) {
try {
HttpGet httppost = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
int status = response.getStatusLine().getStatusCode();
Log.e(TAG, "HTTP Internet Result: " + String.valueOf(status));
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsono = new JSONObject(data);
JSONArray jarray = jsono.getJSONArray("properties");
Log.e(TAG, "JSON Result Length: " + String.valueOf(jarray.length()));
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
Property property = new Property();
property.setName(object.getString("companyName"));
property.setDescription(object.getString("name"));
propertiesList.add(property);
}
return true;
}
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
protected void onPostExecute(Boolean result) {
dialog.dismiss();
adapter.notifyDataSetChanged();
if(result == false)
Toast.makeText(getActivity(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
}
}
类JSONAsynTask扩展了AsyncTask{
字符串结果;
进程对话;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
dialog=newprogressdialog(getActivity());
setMessage(“正在加载,请稍候”);
setTitle(“连接服务器”);
dialog.show();
对话框。可设置可取消(false);
}
@凌驾
受保护的布尔doInBackground(字符串…URL){
试一试{
HttpGet-httppost=新的HttpGet(URL[0]);
HttpClient HttpClient=新的DefaultHttpClient();
HttpResponse response=httpclient.execute(httppost);
int status=response.getStatusLine().getStatusCode();
Log.e(标记“HTTP Internet结果:”+String.valueOf(状态));
如果(状态==200){
HttpEntity=response.getEntity();
字符串数据=EntityUtils.toString(实体);
JSONObject jsono=新的JSONObject(数据);
JSONArray jarray=jsono.getJSONArray(“属性”);
Log.e(标记“JSON结果长度:”+String.valueOf(jarray.Length()));
for(int i=0;i
任何帮助都将不胜感激。先谢谢你 删除最后一条语句后尝试运行代码在doInBackground方法的Try Catch块后返回false。 它总是返回false。 同时在ELSE块中添加**返回false**。。 因此,您的代码如下所示,请重试
class JSONAsynTask extends AsyncTask<String, Void, Boolean> {
String result;
ProgressDialog dialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(getActivity());
dialog.setMessage("Loading, please wait");
dialog.setTitle("Connecting server");
dialog.show();
dialog.setCancelable(false);
}
@Override
protected Boolean doInBackground(String... urls) {
try {
HttpGet httppost = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsono = new JSONObject(data);
JSONArray jarray = jsono.getJSONArray("properties");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
Property property = new Property();
property.setName(object.getString("companyName"));
property.setDescription(object.getString("name"));
propertiesList.add(property);
}
return true;
}else{
return false;
}
} catch (ParseException e1) {
e1.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (JSONException e) {
e.printStackTrace();
return false;
}
}
protected void onPostExecute(Boolean result) {
dialog.dismiss();
adapter.notifyDataSetChanged();
if(result == false)
Toast.makeText(getActivity(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
}
}
类JSONAsynTask扩展了AsyncTask{
字符串结果;
进程对话;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
dialog=newprogressdialog(getActivity());
setMessage(“正在加载,请稍候”);
setTitle(“连接服务器”);
dialog.show();
对话框。可设置可取消(false);
}
@凌驾
受保护的布尔doInBackground(字符串…URL){
试一试{
HttpGet-httppost=新的HttpGet(URL[0]);
HttpClient HttpClient=新的DefaultHttpClient();
HttpResponse response=httpclient.execute(httppost);
int status=response.getStatusLine().getStatusCode();
如果(状态==200){
HttpEntity=response.getEntity();
字符串数据=EntityUtils.toString(实体);
JSONObject jsono=新的JSONObject(数据);
JSONArray jarray=jsono.getJSONArray(“属性”);
for(int i=0;i
如果您正在呼叫https://androidtutorialpoint.com/api/MobileJSONObject.json
API然后直接从JsonObject解析
JSONObject object = new JSONObject(data);
Property property = new Property();
property.setName(object.getString("companyName"));
property.setDescription(object.getString("name"));
propertiesList.add(property);
您的JSON需要重新设计。下面是为android json解析代码生成正确json的代码
<?php
$response = array();
$response["code"] = 200;
$response["properties"] = array();
for($i = 0; $i < 6; $i++) {
$item = array();
$item["companyName"] = "Company ".$i;
$item["name"] = "Model ".$i;
array_push($response["properties"], $item );
}
echo json_encode($response);
?>
在@UmangBurman的帮助下,我找到了问题的根源。问题是JSON数据。有了JSON数据格式和正确的
JSONAsynTask
,我希望这个问题能帮助将来的人在他们的代码中建立一个基础。我已经更新了问题中的代码以呈现调试过的版本。干杯否。如果状态为200,则返回true。看他的code@ShreyaPrajapati返回类型末尾的布尔值肯定是必需的,因此您的代码在try/catch之后会要求返回类型。@UmanBurman我已经添加了,请检查我的更新代码,还需要在else块中添加return语句。@shryaprajapati我的意思是在最后一次catch()之后需要返回语句。@。。您没有在末尾添加,而是在
<?php
$response = array();
$response["code"] = 200;
$response["properties"] = array();
for($i = 0; $i < 6; $i++) {
$item = array();
$item["companyName"] = "Company ".$i;
$item["name"] = "Model ".$i;
array_push($response["properties"], $item );
}
echo json_encode($response);
?>
{
"code":200,
"properties":[
{
"companyName":"Company 0",
"name":"Model 0"
},
{
"companyName":"Company 1",
"name":"Model 1"
},
{
"companyName":"Company 2",
"name":"Model 2"
},
{
"companyName":"Company 3",
"name":"Model 3"
},
{
"companyName":"Company 4",
"name":"Model 4"
},
{
"companyName":"Company 5",
"name":"Model 5"
}
]
}