1

Help me to post json data from Android to Asp.net Here is my code below. Android sending json data to webapi

public class UploadByTableActivity extends Activity {

    final static String url = "https://webapi.com/api/post/";

    HttpResponse response;
    AsyncHttpClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_uploadbytable);

        Button btnWO = (Button) findViewById(R.id.btnwo);

        btnWorkOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                JSONObject jsonParams = new JSONObject();
                String username = "id";
                String password = "pw";
                client =  new AsyncHttpClient(true, 80, 443);
                client.addHeader(
                        "Authorization",
                        "Basic " + Base64.encodeToString(
                                (username + ":"+ password).getBytes(),Base64.NO_WRAP)
                );
                client.setEnableRedirects(true);
                RequestParams params = new RequestParams();
                //  params.put("file", new File(pathoffile));
                params.put("name", "name1");
                params.put("phone", "111-111-1111");

                StringEntity entity = null;
                try {
                    entity = new StringEntity(params.toString());
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

                client.post(null,url,entity, "application/json", new AsyncHttpResponseHandler(Looper.getMainLooper()) {

                    @Override
                    public void onSuccess(int i, Header[] headers, byte[] bytes) {
                        Log.i("appTag", "OK");
                    }

                    @Override
                    public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                        Log.i("appTag"," Error = " + throwable.getMessage());
                    }
                });
            }
        });
    }

Try to retrieve jsondata from webapi to asp.net but jsonString is null. Please help me how to get json data.

[HttpPost]
public string post([FromBody]string jsonString){
    return jsonString; 
}
2
  • Hi you can use retrofit to avoid implementation complexity and easier thread management. Also on Asp .net site use Newton softs JObject to receive data. Commented Sep 7, 2018 at 19:19
  • hi use the retrofit stackoverflow.com/a/40817747/5027107 Commented Sep 8, 2018 at 4:39

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.