0
我已經發布了這個問題,但沒有人提供解決方案,即使我再次編輯我的帖子,所以然後我再次刪除&後。如何捕獲響應數組然後在Listview上設置?如何獲得數組形式的響應和Android上的ListView設置?
我不太清楚StringRequest類&其工作的反應。請幫助我通過一個簡單的&正確guide.it響應單個Jsonobject,但不是數組。 在此JSON響應數據的數組我不知道如何趕上陣列&然後在列表視圖
服務器響應
I/System.out:
{
"success":true,"first_name":"savha","last_name":"jiii","mobile":2147483647
}
W/System.err: org.json.JSONException:
Value
{
"success":true,"first_name":"savha","last_name":"jiii","mobile":2147483647
}
of type org.json.JSONObject cannot be converted to JSONArray
的Json StringRequest凌空
PHP代碼
< ?php
$con=mysqli_connect(XXXXXXXXXXXXXX);
$state=$_POST["state"];
$dist=$_POST["dist"];
$statement=mysqli_prepare($con,"SELECT first_name,last_name,mobile FROM first WHERE state=? AND dist=?");
mysqli_stmt_bind_param($statement,"ss",$state,$dist);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement,$fname,$lname,$mobile);
$response=array();
$response["success"]=false;
while(mysqli_stmt_fetch($statement))
{
$response["success"]=true;
$response["first_name"]=$fname;
$response["last_name"]=$lname;
$response["mobile"]=$mobile;
}
echo json_encode($response);
?>
設置
ServiceRequest類別
public class SevakRequest extends StringRequest {
private static final String LOGIN_REQUEST_URL="http://XXXXXX.php";
private Map<String ,String> params;
public SevakRequest(String state,String dist, Response.Listener<String> listener)
{
super(Method.POST,LOGIN_REQUEST_URL,listener,null);
params=new HashMap<>();
// params.put("service",service);
params.put("state",state);
params.put("dist",dist);
}
@Override
public Map<String, String> getParams() {
return params;
}
}
MainActivity類別
public class SearchResult extends Activity {
Button btn1,btn2;
ListView lv1;
TextView tv1;
String serv, stat, dist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_result);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
lv1 = (ListView) findViewById(R.id.lv1);
tv1 = (TextView) findViewById(R.id.tv1);
Intent in = getIntent();
serv = in.getStringExtra("key1");
stat = in.getStringExtra("key2");
dist = in.getStringExtra("key3");
tv1.setText(serv + " >> " + stat + " >> " + dist);
Response.Listener<String> listener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success) {
JSONArray jsonArray = new JSONArray(response);
int length = jsonArray.length();
List<String> listContents = new ArrayList<String>(length);
for (int i = 0; i < length; i++) {
String fname = jsonResponse.getString("first_name").toString();
String lname = jsonResponse.getString("last_name").toString();
String mobile = jsonResponse.getString("mobile").toString();
listContents.add("First name: " + fname + "\nLast name: " + lname + "\nMobile: " + mobile);
}
lv1.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, listContents));
} else {
Toast.makeText(getApplicationContext(), "Error occurred", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
SevakRequest sevakRequest = new SevakRequest(stat, dist, listener);
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
queue.add(sevakRequest);
}
}
發佈您的服務器響應。 – Piyush
'I/System.out的: { 「成功」:真實的, 「FIRST_NAME」: 「savha」, 「姓氏」: 「JIII」, 「手機」:2147483647} W/System.err的:org.json .JSONException: 值 { 「成功」:真 「如first_name」: 「savha」, 「姓氏」: 「JIII」, 「移動」:2147483647 }類型org.json.JSONObject的 不能轉換到JSONArray現在在問題中添加了「 –
響應 –