作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我需要使用 HttpPost 将 json 请求发送到服务器。这是我当前的代码:
public static String makeRequest(String uri, String json) {
HttpURLConnection urlConnection;
String data = json;
String result = null;
try {
//Connect
urlConnection = (HttpURLConnection) ((new URL(uri).openConnection()));
urlConnection.setDoOutput(true);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setRequestMethod("POST");
urlConnection.connect();
//Write
OutputStream outputStream = urlConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
writer.write(data);
writer.close();
outputStream.close();
//Read
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
System.out.println("Uploading............");
sb.append(line);
}
bufferedReader.close();
result = sb.toString();
System.out.println("Response : " +result);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
但它给了我 400,这意味着错误的请求。
注意:当 base64 路径很小时,只有它有效,否则无效。请帮助我。
最佳答案
请使用此代码:
try {
HttpURLConnection urlConnection;
String result = "";
try {
String data ="";
data = jsonObj.toString();
// String temp=URLEncoder.encode(uri, "UTF-8");
URL url = new URL(WebServiceConstants.getMethodUrl(WebServiceConstants.METHOD_UPDATEVENDER));
urlConnection = (HttpURLConnection) ((url.openConnection()));
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setChunkedStreamingMode(1024);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setRequestMethod("POST");
urlConnection.connect();
//Write
OutputStream outputStream = urlConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
writer.write(data);
writer.close();
outputStream.close();
//Read
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
System.out.println("Uploading............");
sb.append(line);
}
bufferedReader.close();
_responseMain = sb.toString();
System.out.println("Response of Image Upload : " +_responseMain);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// makeRequest(WebServiceConstants.getMethodUrl(WebServiceConstants.METHOD_UPDATEVENDER), jsonObj.toString());
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
runOnUiThread( new Runnable() {
public void run()
{
Constant.showAlertDialog("Message",getResources().getString(R.string.communicationError), VendorEditProfile.this, false);
}
});
}
关于android - 如何在 Android 中使用 HTTP POST 以 JSON 格式将图像作为 base64 字符串发送?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32223789/
我是一名优秀的程序员,十分优秀!