在片段中获取 JSON 数据时连接到服务器时出错



我正在尝试从 php 文件中获取 JSON 数据,我的片段中都有下面的代码,但每次我运行代码时,系统总是返回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);
?>

下面是片段onCreateView()部分中的代码。

new JSONAsynTask().execute("http://buvick-group.com/app/index.php");

下面是我的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();
}
}

任何帮助将不胜感激。提前谢谢你。

如果你调用https://androidtutorialpoint.com/api/MobileJSONObject.jsonAPI,那么直接从 JsonObject 解析。

JSONObject object = new JSONObject(data);
Property property = new Property();
property.setName(object.getString("companyName"));
property.setDescription(object.getString("name"));
propertiesList.add(property);

你的 JSON 需要重新设计。这是为您的安卓 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);
?>

这将打印

{
"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"
}
]
}

在@UmangBurman的大力帮助下,我能够找到问题的根源。问题是 JSON 数据。使用JSON数据格式和正确的JSONAsynTask,我希望这个问题能帮助将来有人在他们的代码中形成基础。我已经更新了问题中的代码以显示调试的版本。干杯

尝试在删除最后一个语句后运行代码,在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();
}
}

最新更新