I wrote an app where a user has to fill some text fields with infomation like name, age ... When he clicks a button the data is being send to my api controller where the put methode saves the data in a data base.
This is kind of working exept that I don´t know how to send a json to my controller.(I researched several days but nothing worked) So what I do at the moment is that I write an url conform string with my data so that in my controller I read out of the string what I need to know. This is a very bad solution!(I can´t do spaces or slashes and so on...)
To send my UserData I hava build an AsyncTask (When I didn´t I got an Exeption)
class SendUserTask extends AsyncTask<String, Void, String>{
public AsyncResponse delegate = null;
User user;
@TargetApi(Build.VERSION_CODES.KITKAT)
protected String doInBackground(String... urls) {
try {
String r = sendUser("name" + user.getName() + "street" + user.getStreet());
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
public void setUser(User u){user = u;}
public static String sendUser(String userData) throws Exception {
URL url = new URL("http://my-address:port/api/users/"+ userData);
HttpURLConnection c =(HttpURLConnection) url.openConnection();
c.setRequestMethod("PUT");
BufferedReader in = new BufferedReader(new InputStreamReader(
c.getInputStream()));
String inputLine;
String result = "";
while ((inputLine = in.readLine()) != null)
result = inputLine;
in.close();
return result;
}
}
this methode works fine when I just want to send an id or get something!
My controller is the defalt api controller here is my put methode:
public String Put(string id, [FromBody]string value){
//stuff
}
Please can anyone help!?!