以下代码有助于将数据发布到服务器。它工作得很好,但现在我必须发送一个名为 token 的字符串。
我也使用了不同的方式来发送params,但我仍然得到错误代码
400:错误请求
我得到的只是令牌值以某种方式导致一些问题。如果您尝试代码,它将发送类似结果的消息:
{"durum":"hata","mesaj":"token_gerekli"}
它应该是{"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", "someName@gmail.com")
.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;
}
答案 0 :(得分:1)
我发现解决方案是一些服务器问题我用 GET 发送了令牌,而其他参数与通常的发布有效。如果有人需要它,这是我的解决方案。
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", "someName@gmail.com")
.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();

答案 1 :(得分:0)
我收到以下回复:
{"durum":"hata","mesaj":"token_gerekli"}
使用以下代码:
<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="someName@gmail.com" />
<input type="hidden" name="web" value="dogu.com" />
<input type="submit" />
</form>
</body>
</html>
因此,这表明您阅读API或连接或其他内容时出现了问题。也就是说,您根据传递的内容从cvbenim.com
API获得正确的响应(好吧,真的发布到它)。