我试图使用volley创建listview但是没有从服务器获得响应它会抛出错误JSONException这是我对服务器的响应:
[{"UserName":"XXXX ","TaskID":1,"Subject":"fjsdgfgf","DueDate":"2016-01-06T00:00:00","Priority":0,"Owner":762,"OwnerName":null,"RepeatStatus":false,"TaskStatus":"In Progress","RelatedTo":null},{"UserName":"XXX","TaskID":2,"Subject":"test","DueDate":"2016-01-11T00:00:00","Priority":0,"Owner":762,"OwnerName":null,"RepeatStatus":false,"TaskStatus":"Not Started","RelatedTo":null}]
我没有得到响应它直接转到Onerrorresponse它抛出类型java.lang.String无法转换为JSONArray作为初学者我真的很困惑到目前为止我犯的错误是我试过的:
这是我发出截击请求的片段:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
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.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
/**
* Created by 4264 on 27-01-2016.
*/
public class Task extends Fragment {
private List<Model_Task_List> model_task_lists;
//Creating Views
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
private RecyclerView.Adapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_task, container, false);
recyclerView=(RecyclerView)view.findViewById(R.id.my_recycler_view);
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setLayoutManager(layoutManager);
model_task_lists=new ArrayList<Model_Task_List>();
//Showing a progress dialog
String Url="http://XXX.XX.X.XXX/XXX/GetActivitiesByUserID.svc/getlist/GetTask/762";
//Creating a json array request
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//Dismissing progress dialog
for(int i = 0; i<response.length(); i++) {
Model_Task_List model_task_list = new Model_Task_List();
try {
JSONObject json = (JSONObject) response
.get(i);
model_task_list.setSubject(json.getString("Subject"));
model_task_list.setUserName(json.getString("UserName"));
model_task_list.setTaskStatus(json.getString("TaskStatus"));
} catch (JSONException e) {
e.printStackTrace();
}
model_task_lists.add(model_task_list);
}
//Finally initializing our adapter
adapter = new Task_List_Adapter(model_task_lists,getContext());
//Adding adapter to recyclerview
recyclerView.setAdapter(adapter);
//calling method to parse json array
// parseData(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(),error.toString(),Toast.LENGTH_SHORT).show();
Log.e("yog",error.toString());
}
});
//Creating request queue
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
//Adding request to the queue
requestQueue.add(jsonArrayRequest);
return view;
}
}
这是我的recyclerview适配器:
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
public class Task_List_Adapter extends RecyclerView.Adapter<Task_List_Adapter.MyViewHolder> {
private List<Model_Task_List> dataSet;
private Context context;
private static data yog;
Model_Task_List modelTaskList=new Model_Task_List();
public void remove(int position)
{
dataSet.remove(position);
notifyItemRemoved(position);
}
public void edit(int position){
dataSet.set(position, modelTaskList);
notifyItemChanged(position);
}
public void setOnItemClickListener(data listener) {
this.yog = listener;
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
// ModelPersonal modelPersonal=new ModelPersonal();
TextView textname;
TextView textaddress;
TextView textphnum;
TextView textdegree;
TextView textemail;
ImageView call;
data datas;
public MyViewHolder(final View itemView) {
super(itemView);
this.textname = (TextView) itemView.findViewById(R.id.subject);
this.textaddress = (TextView) itemView.findViewById(R.id.username);
this.textphnum = (TextView) itemView.findViewById(R.id.status);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(yog!=null){
yog.yog(itemView,getLayoutPosition());
}
}
});
// this.imageViewIcon = (ImageView) itemView.findViewById(R.id.imageView);
}
}
public interface data
{
void yog(View v,int position);
}
public Task_List_Adapter(List<Model_Task_List> data,Context context) {
this.dataSet = data;
this.context=context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.task_list_view, parent, false);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
TextView textViewName = holder.textname;
TextView textViewaddress = holder.textaddress;
TextView textViewphnum = holder.textphnum;
TextView textdegree = holder.textdegree;
TextView textemail=holder.textemail;
textViewName.setText("Subject:"+dataSet.get(position).getSubject());
textViewaddress.setText("UserName"+dataSet.get(position).getUserName());
textViewphnum.setText("Status:"+dataSet.get(position).getTaskStatus());
}
@Override
public int getItemCount() {
return dataSet.size();
}
}
这是我的模特课:
public class Model_Task_List {
public String UserName;
public String Subject;
public String TaskStatus;
public String getSubject() {
return Subject;
}
public void setSubject(String subject) {
Subject = subject;
}
public String getTaskStatus() {
return TaskStatus;
}
public void setTaskStatus(String taskStatus) {
TaskStatus = taskStatus;
}
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
public Model_Task_List(String subject, String taskStatus, String userName) {
Subject = subject;
TaskStatus = taskStatus;
UserName = userName;
}
public Model_Task_List(){
}
}
我的Jsonresponse:
[{"UserName":"XXXXXX ","TaskID":1,"Subject":"fjsdgfgf","DueDate":"2016-01-06T00:00:00","Priority":0,"Owner":762,"OwnerName":null,"RepeatStatus":false,"TaskStatus":"In Progress","RelatedTo":null},{"UserName":"XXXXX","TaskID":2,"Subject":"test","DueDate":"2016-01-11T00:00:00","Priority":0,"Owner":762,"OwnerName":null,"RepeatStatus":false,"TaskStatus":"Not Started","RelatedTo":null}]
我不知道在哪里犯错误可以帮助任何人!!!
Log cat:
I/Timeline: Timeline: Activity_launch_request time:14940252
02-02 19:22:30.733 11678-11678/XX E/RecyclerView: No adapter attached; skipping layout
02-02 19:22:30.733 11678-11678/XX E/RecyclerView: No adapter attached; skipping layout
02-02 19:22:30.773 11678-11678/XX E/RecyclerView: No adapter attached; skipping layout
02-02 19:22:30.773 11678-11678/XXE/RecyclerView: No adapter attached; skipping layout
02-02 19:22:30.793 11678-11678/xx W/Toast: From XXX, go ahead.
02-02 19:22:30.793 11678-11678/XX E/yog: com.android.volley.ParseError: org.json.JSONException: Value [{"UserName":"XX","TaskID":1,"Subject":"fjsdgfgf","DueDate":"2016-01-06T00:00:00","Priority":0,"Owner":762,"OwnerName":null,"RepeatStatus":false,"TaskStatus":"In Progress","RelatedTo":null},{"UserName":"XXX ","TaskID":2,"Subject":"test","DueDate":"2016-01-11T00:00:00","Priority":0,"Owner":762,"OwnerName":null,"RepeatStatus":false,"TaskStatus":"Not Started","RelatedTo":null}] of type java.lang.String cannot be converted to JSONArray
02-02 19:22:30.893 11678-11678/XXI/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@425c1690 time:14940540
答案 0 :(得分:0)
我认为你从string得到的回应是String。请在服务器端交叉验证