从api获取数据到android获取[]方括号下的输出..但是我想要没有[]的输出请帮我解决这个问题..
在android studio中输出就像[2] ,但我希望输出像“2”
此处 my main.java
package com.example.sachin.splashlogin;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Home extends Fragment {
Activity activity;
public Home() {};
TextView visit;
SessionManager session;
CardView cardviewvisit, cardvieworder, cardviewpayment, cardviewdelivery;
JSONObject jsonobject;
private static String url_visitor = "http://10.0.2.2/portal/fetchvisit.php";
JSONParser jParser = new JSONParser();
JSONArray ownerObj, jsonarray;
ArrayList<HashMap<String, String>> arraylist;
ArrayList<String> v_username = new ArrayList<String>();
ArrayList<String> v_parties1 = new ArrayList<String>();
ArrayList<String> v_date = new ArrayList<String>();
String suid, uid, wt_wod_code1, wt_party1;
View view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_home, container, false);
getActivity().setTitle("Home");
cardviewvisit = (CardView) view.findViewById(R.id.cardviewvisit);
cardvieworder = (CardView) view.findViewById(R.id.cardvieworder);
cardviewpayment = (CardView) view.findViewById(R.id.cardviewpayment);
cardviewdelivery = (CardView) view.findViewById(R.id.cardviewdelivery);
session = new SessionManager(getActivity());
HashMap<String, String> user = session.getUserDetails();
uid = user.get(SessionManager.KEY_ID);
visit = (TextView) view.findViewById(R.id.visit1);
cardviewvisit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment fragment = new DeliveryTab();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.framelayout, fragment).commit();
}
});
new DownloadJSON().execute();
return view;
}
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... args) {
// Create an array
try {
arraylist = new ArrayList<HashMap<String, String>>();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("v_username", uid));
JSONObject json = jParser.makeHttpRequest(url_visitor, "GET", params);
ownerObj = json.getJSONArray("visit");
for (int i = 0; i < ownerObj.length(); i++) {
jsonobject = ownerObj.getJSONObject(i);
v_parties1.add(jsonobject.getString("visit"));
}
} catch (Exception e) {
}
return null;
}
@Override
protected void onPostExecute(Void args) {
visit.setText("v_parties1");
}
}
}
这是我的api: -
<?php
//here i posted the query
$count = "100";
if ($count > 0) {
$response["visit"] = array();
$visit["todayvisit"]=$count;
// push single product into final response array
array_push($response["visit"], $visit);
$response["success"] = 1;
echo json_encode($response); else
{
$response["success"] = 0;
$response["message"] = "No Visit found";
// echo no users JSON
echo json_encode($response);
}
?>
这里是浏览器中的输出: -
{"visit":[{"todayvisit":2}],"success":1}
输出总是放在方括号[] ..请帮帮我。
答案 0 :(得分:0)
不
array_push($response["visit"], $visit);
可是:
$response["visit"] = $visit;
答案 1 :(得分:0)
更新您的代码,如下所示 -
$response = array( #Initialize the response array
'visit'=>array(
'todayvisit'=>0
),
'success'=>0,
'message'=>'No Visit found'
);
$count = "100"; #Assign your visited/query data here
if ($count > 0) {
$response['visit']['todayvisit'] = $count;
$response["success"] = 1;
$response["message"] = "Visit found";
}
echo json_encode($response);
输出,如果访问
{"visit":{"todayvisit":"100"},"success":1,"message":"Visit found"}
输出,如果没有访问
{"visit":{"todayvisit":0},"success":0,"message":"No Visit found"}
注意:现在,如果找不到访问,您的代码将返回默认的JSON
数据。因此,无需检查else
条件
另一个答案
如果您只想要2
或0
这样的值。然后无需制作JSON
格式。从您的PHP代码中使用如下所示 -
$count = '100'; #query from Database and assign visited count
echo $count;
在你的android代码中,GET
字符串数据。无需使用json解析器。
仅GET
字符串。
答案 2 :(得分:0)
仅限该代码
$count = "100";
if ($count > 0) {
$response = array();
$visit["todayvisit"]=$count;
// push single product into final response array
$response["visit"] = $visit;
//array_push($response, $visit);
$response["success"] = 1;
echo json_encode($response);
}
else
{
$response["success"] = 0;
$response["message"] = "No Visit found";
// echo no users JSON
echo json_encode($response);
}