0

Following code helps to post data to server. It was working very well but now I have to send a string named as token.

I also used different ways like to send params but still I get error code

400: bad request

All I get is that token value somehow causes some problems. If you try code it will send message like result will be:

{"durum":"hata","mesaj":"token_gerekli"} it is supposed to be something like {"durum":"hata","mesaj":"token_zaman_Aşımı"}

 @Override protected Void doInBackground(String... params) { try { String link = "http://cvbenim.com/api/v1/isveren/uyeliktamamla"; URL url = new URL(link); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestProperty("User-Agent", ""); connection.setRequestMethod("POST"); connection.setDoInput(true); String token="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjQsImlzcyI6Imh0dHA6XC9cL3Byb2plLmFwcFwvYXBpXC92MVwvZ2lyaXMiLCJpYXQiOjE0NTk5NTA5NTQsImV4cCI6MTQ1OTk3NjE1NCwibmJmIjoxNDU5OTUwOTU0LCJqdGkiOiIwMzRhYmEyY2JmYWEyODg4ZmZjY2ZiZjAxZDA3OTI1YyJ9.Saan9lSUb3FWeFfSNWO4hKyFU-osca0T32CdjC-9Kd8"; Uri.Builder builder = new Uri.Builder() .appendQueryParameter("token",token) .appendQueryParameter("firma_adi","SomeName") .appendQueryParameter("sektor", "1") .appendQueryParameter("sehir", "2") .appendQueryParameter("ilce", "3") .appendQueryParameter("semt","4") .appendQueryParameter("adres","SomePlace") .appendQueryParameter("telefon","02122342111") .appendQueryParameter("email", "[email protected]") .appendQueryParameter("web","dogu.com"); String query = builder.build().getEncodedQuery(); OutputStream os = connection.getOutputStream(); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); writer.write(query); writer.flush(); connection.connect(); InputStream is ; if(connection.getResponseCode()>=400) is=connection.getErrorStream(); else is = connection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); sb.append(reader.readLine() + "\n"); String line = "0"; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } writer.close(); os.close(); is.close(); String result = sb.toString(); Log.e("Final Result ",result); Message msg = new Message(); msg.obj = result; mh.sendMessage(msg); } catch (Exception e) { e.printStackTrace(); } return null; } 
11
  • you can use this check my answer it handles GET and POST with cookies stackoverflow.com/questions/37289109/… Commented May 24, 2016 at 8:22
  • I check that answer but hard to apply, Which part of my code should I modify I couldnt understand Commented May 24, 2016 at 8:30
  • all the data that you are posting is receving at api except token. is it? Commented May 24, 2016 at 8:33
  • Yes, You are right Commented May 24, 2016 at 8:36
  • the work you are doing in do in background just call my method ` Connection.postAPIResponse(address, strJsonData);` where address is your url and strJsonData is formed to post data. calll it in back ground and check response Commented May 24, 2016 at 8:42

2 Answers 2

1

I found solution it was some server problem I sent token with GET and other params with usual Post it works. In case somebody need it here is my solution.

 String link = "http://cvbenim.com/api/v1/isveren/uyeliktamamla?token="+token; URL url = new URL(link); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestProperty("User-Agent", ""); connection.setRequestMethod("POST"); connection.setDoInput(true); Uri.Builder builder = new Uri.Builder() .appendQueryParameter("firma_adi","SomeName") .appendQueryParameter("sektor", "1") .appendQueryParameter("sehir", "2") .appendQueryParameter("ilce", "3") .appendQueryParameter("semt","4") .appendQueryParameter("adres","SomePlace") .appendQueryParameter("telefon","02122342111") .appendQueryParameter("email", "[email protected]") .appendQueryParameter("web","dogu.com"); String query = builder.build().getEncodedQuery(); OutputStream os = connection.getOutputStream(); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); writer.write(query); writer.flush(); connection.connect();

Sign up to request clarification or add additional context in comments.

Comments

0

I get the following response:

{"durum":"hata","mesaj":"token_gerekli"} 

With the following code:

<html> <head> </head> <body> <form action="http://cvbenim.com/api/v1/isveren/uyeliktamamla" method="post"> <input type="hidden" name="token" value="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjQsImlzcyI6Imh0dHA6XC9cL3Byb2plLmFwcFwvYXBpXC92MVwvZ2lyaXMiLCJpYXQiOjE0NTk5NTA5NTQsImV4cCI6MTQ1OTk3NjE1NCwibmJmIjoxNDU5OTUwOTU0LCJqdGkiOiIwMzRhYmEyY2JmYWEyODg4ZmZjY2ZiZjAxZDA3OTI1YyJ9.Saan9lSUb3FWeFfSNWO4hKyFU-osca0T32CdjC-9Kd8" <input type="hidden" name="firma_adi" value="SomeName" /> <input type="hidden" name="sektor" value="1" /> <input type="hidden" name="sehir" value="2" /> <input type="hidden" name="ilce" value="3" /> <input type="hidden" name="semt" value="4" /> <input type="hidden" name="adres" value="SomePlace" /> <input type="hidden" name="telefon" value="02122342111" /> <input type="hidden" name="email" value="[email protected]" /> <input type="hidden" name="web" value="dogu.com" /> <input type="submit" /> </form> </body> </html> 

So this points to something wrong with your reading of the API or the connection or something else. That is, you are getting the correct response from the cvbenim.com API based on what you are passing it (well, posting to it really).

5 Comments

thanks for the answer, I use chrome's post man to test api. But problem is that I cant find trouble in my code
Are you saying you get a valid (correct) response when posting using the Chrome extension "Postman", but invalid when posting from your App?
that is right, I use same token on Postman it works but doesnt work wiith my code
OK, and I also got the error, so this points to some issue / feature with the API that is different when called from your desktop and when called from somewhere else (for example, your App, my desktop). Is it IP restricted? Does the API require a certain User-Agent? These are the sort of questions you need to ask now.
so now I have to work on this answer I hope İt will help link

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.