1

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!?!

HHHH
  • 49
  • 11
  • Try to consider good android library - [Retrofit](http://square.github.io/retrofit/) maybe it would help you and make your life easier – Oleh Dokuka Sep 17 '15 at 09:25
  • Also I have not seen here yet any json objects or strings, that contain json. ``sendUser("name" + user.getName() + "street" + user.getStreet());`` line also not assignable to standard html request too. Your example wrong, try to consider [more example](https://www.google.com.ua/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=how%20to%20send%20json%20data%20to%20server%20in%20android) and understand how to send json object to server from android – Oleh Dokuka Sep 17 '15 at 09:34
  • @OlegDokuka that is not my Json there I am just building a string so that I can add it to my url this works. I would profer to send it with a json and I know how to write a json. – HHHH Sep 17 '15 at 09:57

1 Answers1

1

Try to consider this post. It describes how to send object or entity to server using retrofit. Also I have found topic that describes how to retrieve json data in server side.

Update

Android side

  1. Try to create simple entity that would contain all data that you would like to send on server
  2. Create simple interface that would be send data to server. Consider next:

     public interface UserApi {
        @PUT("/api/users/")
        Call<Object> sendData(@Body FooRequest body);
     }
    
  3. Get adapter end send request

     Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("http://my-address:port")
        .build();
    
     UserApi service = retrofit.create(UserApi.class)
     service.sendData(new FooRequest());
    

Server side

  1. Try create apicontroller like this (I`m not sure)

     public class UtilityController : ApiController
     {
         [HttpPut]
         public string Put(FooRequest fooRequest)
         {
             return "bla";
         }
      }
    

I hope it would help you!

here is my test project

Good Luck

Community
  • 1
  • 1
Oleh Dokuka
  • 11,613
  • 5
  • 40
  • 65
  • Thanks for your answer!!! I tried everything exactly as you said but I get this: com.android.internal.os.RuntimeInit$UncaughtHandler any Idea? – HHHH Sep 17 '15 at 10:25
  • Hmm, can you share your code(github, etc), or may be post logcat log – Oleh Dokuka Sep 17 '15 at 10:31
  • 1
    Try to consider next [example](https://futurestud.io/blog/retrofit-getting-started-and-android-client/) , and created new project using Android Studio, if you wait a bit I`ll create simple project and post it for you – Oleh Dokuka Sep 17 '15 at 10:35
  • my logcat error: Unable to create converter for class java.lang.Object for method UserApi.sendData – HHHH Sep 17 '15 at 10:37
  • Ohh, it makes everything clearly! ``Retrofit retrofit = new Retrofit.Builder() .baseUrl("**sample base url here**") .addConverterFactory(GsonConverterFactory.create()) .build();`` Try to add to your adapter initialization next line:``addConverterFactory(GsonConverterFactory.create())`` – Oleh Dokuka Sep 17 '15 at 10:40
  • I did what you said and weardly I still get the same error but my app isn´t crashing anymore.(But my methode still doesn´t get hit) – HHHH Sep 17 '15 at 11:10
  • You mean that app works, UserApp sendData to server, but server does not handle Put method? – Oleh Dokuka Sep 17 '15 at 11:43
  • I can´t tell if the data is being send to server. What I know is that the data never reaches the server. But I also don´t get an error. So when I click the button that invokes the code you gave me nothing seems to happen. – HHHH Sep 17 '15 at 11:46
  • 1
    [Here](https://github.com/OlegDokuka/RetrofitSample) I have already prepared working example for you, test it with your server and tell me results – Oleh Dokuka Sep 17 '15 at 12:52
  • 1
    Thank you so much for your effort! This is working perfectly! I have to see what I am doing wrong in my actual project but now I know how to do it and that my web service isn´t broke. – HHHH Sep 17 '15 at 13:22
  • You are welcome, you can review my project again, I have just fixed several bugs – Oleh Dokuka Sep 17 '15 at 13:31