How to send an Android post to Asp.net Core?

Asked

Viewed 33 times

0

I have an application in Asp.net Core that should receive a Post via android and I’m not getting. There is always an error on android that the page was not found.

class of the object in the Asp that will receive the Post sent by android

    public class Localizacao
{
    public string motoristaId { get; set; }
    public string lat { get; set; }
    public string lon { get; set; }
}

Asp Controller that will receive the Post

 [HttpPost]
    public JsonResult AtualizaLocalizacaoMotorista([FromBody] Localizacao localizacao)
    {
        var sa = new JsonSerializerSettings();         
        return Json(localizacao, sa);
    }

code on android to send the Post

import android.content.Context;

import android.os.Asynctask; import android.widget.Toast;

import java.io.Bufferedreader; import java.io.Ioexception; import java.io.Inputstreamreader; import java.io.Outputstreamwriter; import java.net.Httpurlconnection; import java.net.URL;

public class Trylogin extends Asynctask { private context;

@Override
protected String doInBackground(String... params) {

    final String motoristaId = params[0];
    if (null == motoristaId) {
        return "motoristaId não informado";
    }

    HttpURLConnection connection;
    OutputStreamWriter request = null;

    URL url = null;
    String response = null;
    String parameters = "motoristaId=" + motoristaId;

    try {
        url = new  URL("https://MINHA_URL");
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Accept", "application/json");
        //connection.setRequestProperty("X-DreamFactory-Api-Key", "XXXXXXXXXXXXXXXXXXXX");
        //connection.setRequestProperty("X-DreamFactory-Session-Token", "key");
        //connection.setRequestProperty("Authorization", "auth");
        connection.setRequestMethod("POST");


        request = new OutputStreamWriter(connection.getOutputStream());
        request.write(parameters);
        request.flush();
        request.close();
        connection.connect();
        String line = "";
        InputStreamReader isr = new InputStreamReader(connection.getInputStream());
        BufferedReader reader = new BufferedReader(isr);
        StringBuilder sb = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        // Response from server after login process will be stored in response variable.
        response = sb.toString();
        /**
         Não podemos interar com a tela!, vamos mandar para o método onPostExecute
         */
        // Toast.makeText(this,"Message from Server:"+ response, 0).show();
        isr.close();
        reader.close();
        return response;

    } catch (IOException e) {
        e.printStackTrace();
        return e.getMessage();
    }

}

/**
 * Este método irá rodar na Thread de UI após executar a ação!
 *
 * @param s
 */

@Override
protected void onPostExecute(String s) {
    super.onPostExecute(s);
    if (s != null) {
        Toast.makeText(context , s, Toast.LENGTH_SHORT).show();
    }
}

}

From Activity I call with the parameter

new TryLogin().execute("dsfsdfsdfsdfds");
  • I believe the mistake is that the PSA is waiting FormBody and on android be sending application/json. However I recommend you to use the Retrofit lib on android to make the HTTP calls, it is much simpler. Link Retrofit: https://square.github.io/retrofit/

  • Sorry for the delay, I was doing several tests. I had already tried the Retrofit, but I found it complex. I studied this feature more and managed to walk in the project. Now is to upload and download images from the mobile to the API in Asp.core. Thanks for the support. With the retrofit the code became much cleaner and simpler to understand. The question of the [Frombody] in the Asp I removed, had put by own guidelines of the Bowls I made, but I took now.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.