这里尝试使用asynctask和httpurlconnection从android传递json字符串,但是当我尝试传递值时,它会抛出http 400错误的请求,我该如何实现呢?
listobj = account_sf_db.list();
String empty="yog";
recyclerView.setAdapter(accountListAdapter);
new Account_async().execute(acnt);
int count = account_sf_db.getTaskCount();
public class Account_async extends AsyncTask<String, String, String> {
String JsonResponse = null;
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
protected String doInBackground(String... params) {
HttpURLConnection urlConnection = null;
String JsonDATA = params[0];
byte[]post=JsonDATA.getBytes();
int request_post=post.length;
BufferedReader reader = null;
try {
URL url = new URL("http://xx.xx.x.xx/xx/xx/xx/xx/CreateAccount/"+JsonDATA);
Log.d("s",url.toString());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Length", String.valueOf(String.valueOf(Integer.toString(request_post)).getBytes()));
urlConnection.setDoOutput(true);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(urlConnection.getOutputStream()));
out.write(params.toString());
out.close();
int response = urlConnection.getResponseCode();
if (response >= 400 && response <= 499) {
Log.e("", "HTTPx Response: " + response + " - " + urlConnection.getResponseMessage());
InputStream in = new BufferedInputStream(urlConnection.getErrorStream());
}
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String inputLine;
while ((inputLine = reader.readLine()) != null)
buffer.append(inputLine + "\n");
if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
return null;
}
JsonResponse = buffer.toString();
Log.i("Error", JsonResponse);
//send to post execute
return JsonResponse;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e("", "Error closing stream", e);
}
}
}
return null;
}
}
}
这里它抛出错误的请求,当我尝试传递json字符串dono在哪里犯了错误,即使我尝试使用凌空但它抛出相同的错误可以有人帮助这是唯一的方法来传递json值这里再一个信息我试图使用gson库解析列表不知道哪里出错了