public class MainActivity extends AppCompatActivity {
// Button senddata;
//private final String url = "http://19.168.1.58:9090/data/db";
public static final String KEY_EMAIL = "email";
EditText email1;
String emailString;
String _id, _name, _pass, _email;
TextView displayjsonText;
DataRecieveJson dr;
ArrayList<agentPojo> list;
agentPojo agentdetails;
synck sy;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
email1 = (EditText) findViewById(R.id.email);
displayjsonText = (TextView) findViewById(R.id.displayView);
}
public void checkdata(View view){
emailString= email1.getText().toString();
dr = new DataRecieveJson(this, emailString);
dr.RecieveData();
dr.te();
}
}
public class DataRecieveJson {
Context mcontext;
String jresponse;
public String _id = "";
String _name;
String _pass = "" ;
String _email = "";
String mail;
JSONObject obj;
ArrayList<agentPojo> list;
// ArrayList<agentPojo> nlist;
agentPojo agent;
public DataRecieveJson(Context mcontext,String mail) {
this.mcontext = mcontext;
this.mail = mail;
}
public void RecieveData() {
RequestQueue queue = Volley.newRequestQueue(mcontext);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url.jurl + mail, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
parsejson(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("onErrorResponse: ", error.toString());
Toast.makeText(mcontext, error.toString(), Toast.LENGTH_LONG).show();
}
});
int socketTimeout = 30000;
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
jsonObjectRequest.setRetryPolicy(policy);
queue.add(jsonObjectRequest);
}
public void parsejson(JSONObject response) {
list= new ArrayList<agentPojo>();
agent = new agentPojo();
try {
JSONArray ag1 = response.getJSONArray("agentD");
for (int i = 0; i < ag1.length(); i++) {
JSONObject ag = ag1.getJSONObject(i);
// _id = ag.getString("id");
// _name = ag.getString("name");
// _pass = ag.getString("password");
// _email = ag.getString("email");
agent.setId(ag.getString("id"));
agent.setName(ag.getString("name"));
agent.setPass(ag.getString("password"));
agent.setEmail(ag.getString("email"));
list.add(agent);
Log.d("parsejson: ", String.valueOf(list));
// Log.d("parsejson: ", "Hello");
agentPojo po;
String id = null, email = null;
for (int j = 0; j < list.size() ; j++) {
po = list.get(j);
id = po.getId();
email = po.getEmail();
}
Log.d("RecieveDataInside: ", id + "," + email);
}
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("Pj:Id ", String.valueOf(list));
}
public void te() {
Log.d("test:Id ", String.valueOf(list));
}
}
日志:
09-17 07:24:52.689 21681-21681/com.example.pawar.sendtoservlet D/test:Id: null
09-17 07:24:53.061 21681-21681/com.example.pawar.sendtoservlet D/parsejson:: [com.example.pawar.sendtoservlet.agentPojo@536b7f1c]
09-17 07:24:53.061 21681-21681/com.example.pawar.sendtoservlet D/RecieveDataInside:: 1,ram21@gmail.com
09-17 07:24:53.061 21681-21681/com.example.pawar.sendtoservlet D/Pj:Id: [com.example.pawar.sendtoservlet.agentPojo@536b7f1c]
ArrayList
在方法null
中显示Te()
。
答案 0 :(得分:0)
ArrayList
尚未初始化到您收到日志消息的位置,这就是ArrayList
为null
的原因。
在跟踪您的方法后,我发现您只在parsejson()
中初始化列表,parsejson()
仅在您onResponse()
的{{1}}中调用{ {1}}。
这必须意味着new JsonObjectRequest()
未被调用,这就是ReceiveData()
未运行且onResponse()
为parsejson()
的原因。
我建议你做一些评论提到的内容,即使用更多日志语句来帮助您调查list
未被调用的原因。
答案 1 :(得分:0)
尝试全局初始化
ArrayList list = new ArrayList();