通过发送一些数据,通过android截击获得Jsonarray作为响应
有人能告诉我……如何通过android volley库获取jsonarray作为响应……我使用了名为jsonArrayRequest的方法,但有一件事……我想通过向php脚本发送一些数据来获得jsonarray响应……我的php脚本包含三个变量……我将从android传递这三个变量应用程序…然后我的php脚本根据这三个值生成jsonarray响应…我使用getParams方法将数据发送到php脚本…但是当我要求jsonarray响应时,android从未使用getParams方法…但是当我使用stringRequest时…它将正常工作…但我希望jsonarray响应不是请求 有人能帮我怎么做吗?? 提前谢谢通过发送一些数据,通过android截击获得Jsonarray作为响应,android,mysql,json,android-volley,Android,Mysql,Json,Android Volley,有人能告诉我……如何通过android volley库获取jsonarray作为响应……我使用了名为jsonArrayRequest的方法,但有一件事……我想通过向php脚本发送一些数据来获得jsonarray响应……我的php脚本包含三个变量……我将从android传递这三个变量应用程序…然后我的php脚本根据这三个值生成jsonarray响应…我使用getParams方法将数据发送到php脚本…但是当我要求jsonarray响应时,android从未使用getParams方法…但是当我使用s
package com.example.shreeram.dreamapp;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class QuestionActivity extends AppCompatActivity {
// declare all views...
TextView tv1,tv2,tv3,tv4,tv5;
Spinner spinner;
//php script url.....
String server_url = "http://192.168.212.2/question.php";
ArrayList<String> question;
ArrayList<String> option1;
ArrayList<String> option2;
ArrayList<String> option3;
ArrayList<String> option4;
ArrayList<Integer> correct;
ArrayList<Integer> q_id;
ArrayList<Integer> id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question);
//receive data from previous activity....data is .....value of click and listclick..
Intent in = getIntent();
Bundle b = in.getExtras();
final Integer click = b.getInt("click");
final Integer listclick = b.getInt("listclick");
//initilize all views...
tv1 = (TextView) findViewById(R.id.textView_question);
tv2 = (TextView) findViewById(R.id.textViewop1);
tv3 = (TextView) findViewById(R.id.textViewop2);
tv4 = (TextView) findViewById(R.id.textViewop3);
tv5 = (TextView) findViewById(R.id.textViewop4);
spinner = (Spinner) findViewById(R.id.spinner);
//Initialize all array list....here data will come from mysql database or jsonarray...
question = new ArrayList<String>();
option1 = new ArrayList<String>();
option2 = new ArrayList<String>();
option3 = new ArrayList<String>();
option4 = new ArrayList<String>();
correct = new ArrayList<Integer>();
q_id = new ArrayList<Integer>();
id = new ArrayList<Integer>();
//Set the different fonts....
final Typeface myfont1 = Typeface.createFromAsset(getAssets(),"fonts/Vollkorn-
Regular.ttf");
final Typeface myfont2 = Typeface.createFromAsset(getAssets(),"fonts/PT_Serif-
Web-Regular.ttf");
//This is the request from server....for jsonarray.....
//This is starting of jsonArrayrequest.......
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest
(Request.Method.POST, server_url,null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//response will come here....
int i = 0;
int j = response.length();
//retrieve all json object from json array...and fill that data to the
corresponding arraylist...
while (i<j)
{
try {
JSONObject jsonObject = response.getJSONObject(i);
id.add(jsonObject.getInt("id"));
q_id.add(jsonObject.getInt("q_id"));
question.add(jsonObject.getString("question"));
option1.add(jsonObject.getString("op_1"));
option2.add(jsonObject.getString("op_2"));
option3.add(jsonObject.getString("op_3"));
option4.add(jsonObject.getString("op_4"));
correct.add(jsonObject.getInt("correct"));
i++;
} catch (JSONException e) {
e.printStackTrace();
}
}
//set fonts to all the views....
tv1.setTypeface(myfont1);
tv2.setTypeface(myfont1);
tv3.setTypeface(myfont1);
tv4.setTypeface(myfont1);
tv5.setTypeface(myfont1);
//set values to the views from arraylist....
tv1.setText(question.get(0));
tv2.setText(option1.get(0));
tv3.setText(option2.get(0));
tv4.setText(option3.get(0));
tv5.setText(option4.get(0));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//Error will appear here.....
tv1.setText("error happened...");
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String , String> params = new HashMap<String, String>();
//put values in the params....
params.put("category",""+click);
params.put("quizno",""+listclick);
//return params....and values to the php script...
return params;
}
};
//This is end of jsonArrayrequest.....
//Create a new request queue.....
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Add jsonarray request to the request queue....
requestQueue.add(jsonArrayRequest);
//End of oncreate method....
}
//End of activity class......
}
and this is the php script.....
<?php
//get values from android app......
$click = $_POST['category'];
$listclick = $_POST['quizno'];
//This is fix....
$language = 1;
$user = "root";
$password = "";
$host = "localhost";
//All database names....
$db_name1 = "history_english";
$db_name2= "history_hindi";
$db_name3= "world_english";
$db_name4= "world_hindi";
$db_name5= "science_english";
$db_name6= "science_hindi";
$db_name7= "sports_english";
$db_name8= "sports_hindi";
$db_name9= "india_english";
$db_name10= "india_hindi";
$db_name11= "computer_english";
$db_name12= "computer_hindi";
$db_name13= "politics_english";
$db_name14= "politics_hindi";
$db_name15= "business_english";
$db_name16= "business_hindi";
//Conditions for fetch jsonarray.....
if($click==1)
{
//means history database....
if($language==1)
{
//means english database...history_english
$con1 = mysqli_connect($host,$user,$password,$db_name1);
//Make sql query......
$q1 = "select * from t_".$listclick;
$result1 = mysqli_query($con1,$q1);
$response = array( );
$num1 = mysqli_num_rows($result1);
//Encode data as jsonarray.....
while($row1 = mysqli_fetch_array($result1))
{
array_push($response,array("id"=>$row1["id"],"q_id"=>$row1["q_id"],"question"=>$row1["question"],"op_1"=>$row1["op_1"],
"op_2"=>$row1["op_2"],"op_3"=>$row1["op_3"],"op_4"=>$row1["op_4"],"correct"=>$row1["correct"]));
}
echo json_encode($response);
}
else
{
//means hindi database.....history_hindi
$con2 = mysqli_connect($host,$user,$password,$db_name2);
//Write code later.....
}
?>
package com.example.shreram.dreamapp;
导入android.content.Intent;
导入android.graphics.Typeface;
导入android.support.annotation.NonNull;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.widget.ArrayAdapter;
导入android.widget.Spinner;
导入android.widget.TextView;
导入com.android.volley.AuthFailureError;
导入com.android.volley.Request;
导入com.android.volley.RequestQueue;
导入com.android.volley.Response;
导入com.android.volley.VolleyError;
导入com.android.volley.toolbox.JsonArrayRequest;
导入com.android.volley.toolbox.StringRequest;
导入com.android.volley.toolbox.volley;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.util.ArrayList;
导入java.util.Collection;
导入java.util.HashMap;
导入java.util.Map;
导入java.util.Set;
公共类QuestionActivity扩展了AppCompatActivity{
//声明所有视图。。。
文本视图tv1、tv2、tv3、tv4、tv5;
纺纱机;
//php脚本url。。。。。
字符串服务器\u url=”http://192.168.212.2/question.php";
ArrayList问题;
ArrayList选项1;
ArrayList选项2;
ArrayList选项3;
ArrayList选项4;
数组列表正确;
ArrayList q_id;
ArrayList id;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_-question);
//从以前的活动接收数据…数据是…单击和列表单击的值。。
Intent in=getIntent();
Bundle b=in.getExtras();
最终整数click=b.getInt(“click”);
最终整数listclick=b.getInt(“listclick”);
//初始化所有视图。。。
tv1=(TextView)findViewById(R.id.TextView\u问题);
tv2=(TextView)findViewById(R.id.textViewop1);
tv3=(TextView)findViewById(R.id.textViewop2);
tv4=(TextView)findViewById(R.id.textViewop3);
tv5=(TextView)findViewById(R.id.textViewop4);
微调器=(微调器)findViewById(R.id.spinner);
//初始化所有数组列表…这里的数据将来自mysql数据库或jsonarray。。。
问题=新的ArrayList();
option1=新的ArrayList();
option2=新的ArrayList();
option3=新的ArrayList();
option4=新的ArrayList();
correct=新的ArrayList();
q_id=new ArrayList();
id=新的ArrayList();
//设置不同的字体。。。。
final Typeface myfont1=Typeface.createFromAsset(getAssets(),“fonts/Vollkorn-
常规(ttf);
final Typeface myfont2=Typeface.createFromAsset(getAssets(),“字体/PT_衬线-
网站(www.ttf),;
//这是服务器…对jsonarray…的请求。。。。。
//这是jsonArrayrequest的开始。。。。。。。
JsonArrayRequest JsonArrayRequest=新JsonArrayRequest
(Request.Method.POST,服务器url,null,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONArray响应){
//这里会有回应。。。。
int i=0;
int j=响应.长度();
//从json数组中检索所有json对象…并将该数据填充到
对应的数组列表。。。
而
根据您提供的信息,使用VolleyStringRequest
方法不会有问题——下载功能正常,并且正确调用了getParams()
方法。
知道服务器请求将返回一个字符串!如果您在PHP代码中使用json_encode()
,那么该字符串将是一个json编码的字符串。您没有提供PHP代码…因此我只能假设PHP端的所有设置都正确
只需使用JSONArray array=newjsonarray(response);
即可使用该JSON编码字符串创建JSONArray
,其中“response”是StringRequest
返回的值。
因此,只需创建一个类似下面的downLoadDataVolley()
方法的方法,并根据需要传递url
private void downLoadDataVolley(String url){
RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//INFO:: The response is of String type, but it is a JSON encoded String..
// !! as long as the PHP code is indeed returning JSON!!
// IF the JSON encoded string is structured as a JSONArray then do this...
JSONArray array = new JSONArray(response);
for(int i = 0; i < array.length(); i++){
JSONObject o = array.getJSONObject(i);
Iterator<String> it = o.keys();
while(it.hasNext()){
String key = it.next();
String value = o.optString(key, "NA");
Log.e("Try to Parse", "key = " + key + " value = " + value);
}
//String s = o.optString("key", "NA");
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError e) {
e.printStackTrace();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String , String> params = new HashMap<String, String>();
//put values in the params....
params.put("category",""+click);
params.put("quizno",""+listclick);
//return params....and values to the php script...
return params;
}};
requestQueue.add(stringRequest);
}
private void downLoadDataVolley(字符串url){
RequestQueue RequestQueue=Volley.newRequestQueue(this);
StringRequest StringRequest=新的StringRequest(Request.Method.POST,url,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
//信息::响应是字符串类型,但它是JSON编码的字符串。。
//!!只要PHP代码确实返回JSON!!
//如果JSON编码字符串的结构是JSONArray,那么请执行以下操作。。。
JSONArray数组=新的JSONArray(响应);
对于(int i=0;i