I had used below code for sending data to server in several projects and it works , but in my recent project it doesnt work and give me statuscode 400 ... i use this code in AsyncTask class :
public class sendingDataAsyncHttpTask extends AsyncTask<String, Void, Integer> {
@Override
protected void onPreExecute() {
}
@Override
protected Integer doInBackground(String... params) {
InputStream inputStream = null;
Integer result = 0;
HttpURLConnection urlConnection = null;
String key = params[0];
try {
URL url = new URL(params[0]);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
int statusCode = urlConnection.getResponseCode();
if (statusCode == 200) {
BufferedReader r = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
response.append(line);
}
parseAdminTaskResult(response.toString());
result = 1;
} else {
result = 0;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(Integer result) {
}
上面的代码可以正常工作,但是问题出在我尝试发送的数据中……请始终确保您的数据中没有空格(“”),然后将其发送到服务器...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.