如何使用android中的volley使用自定义对象进行调用

时间:2017-03-07 17:44:15

标签: android spring-boot android-volley

我是Android的新手可以对这个问题的任何一个答案,我尝试了很多,但没有找到正确的东西。我想发送我的自定义用户对象弹簧休息服务

员工对象: 在android和spring中

public class MainActivity extends AppCompatActivity  {
    Button clickbtn;
    TextView resText;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
clickbtn=(Button)findViewById(R.id.mybutton);
        resText=(TextView)findViewById(R.id.mytext);


  clickbtn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {

          Employee emp=new Employee();
          emp.setFirstname("naresh");
          RequestQueue queue = Volley.newRequestQueue(getApplicationContext());

          GsonRequest<Employee> myReq = new GsonRequest<Employee>("http://192.168.10.37:8088/adduser",
                  emp,
                  Employee.class,
                  new HashMap<String, String>(),
                  new Response.Listener<Employee>() {
                      @Override
                      public void onResponse(Employee response) {

                      }
                  },
                  new Response.ErrorListener() {
                      @Override
                      public void onErrorResponse(VolleyError error) {
                          Toast.makeText(getApplicationContext(),"error", Toast.LENGTH_SHORT).show();
                          Log.i("post error","post error",error);
resText.setText(error.getMessage());
                          Log.d("post error","post error",error);
                      }
                  });


          queue.add(myReq);




      }
  });

    }
}

GsonRequest:

@RequestMapping("adduser")
public void addUser(@Requestbody User user){
Sysetm.out.println(user.getFirstname());
}

主要活动:

          at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
E/Volley: [216] BasicNetwork.performRequest: Unexpected response code 415 for http://192.168.10.37:8088/adduser
I/post error: post error
              com.android.volley.ServerError
                  at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:163)
                  at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
D/post error: post error
              com.android.volley.ServerError
                  at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:163)
                  at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)

春季休息服务:

        final ListView result_list = (ListView) findViewById(R.id.result_list_view);
    final ResultsListCustomAdapter adapter = new ResultsListCustomAdapter(this);
    result_list.setAdapter(adapter);

例外:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    final SearchResultViewHolder viewHolder;

    if (convertView == null) {

        viewHolder = new SearchResultViewHolder();
        convertView = inflater.inflate(R.layout.search_results_list_item, parent, false);
        viewHolder.wordTextView = (TextView) convertView.findViewById(R.id.Result_TextView);

        viewHolder.addToFavsImgButton = (ImageButton) convertView.findViewById(R.id.add_to_favs_button);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (SearchResultViewHolder) convertView.getTag();
    }

    viewHolder.wordTextView.setText(results.get(position));

    viewHolder.wordTextView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Intent play_activity_intent = new Intent(context, PlayActivity.class);

    //Pulsante FAV
    viewHolder.addToFavsImgButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Utility.addFavs(context, viewHolder.wordTextView.getText().toString());
            Toast.makeText(context, "Added to favorites", Toast.LENGTH_SHORT).show();
        }
    });

    return convertView;
}

1 个答案:

答案 0 :(得分:1)

作为我的评论,请在headers.put("Content-Type", "application/json; charset=utf-8");内添加getHeaders()