Android 上的 HttpPut 不工作
HttpPut on Android not working
我正在尝试使用 Web 服务将图像从 Android 应用程序发送到 Java 应用程序。
在服务器端,我有这段代码,使用 Postman 作为客户端时它工作正常:
@Path("/report")
public class ReportService {
@PUT
@Path("/put")
@Consumes(MediaType.APPLICATION_JSON)
public Report saveReport(String json){
return ReportDAO.saveReport(json);
}
另一方面,我的 android 应用程序正在尝试将图像上传到服务器:
try {
//Encode the image
String imagenPath = params[1];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap myBitmap = BitmapFactory.decodeFile(imagenPath);
myBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
//Create client,set url and entity
HttpClient httpClient = new DefaultHttpClient();
StringEntity entity = new StringEntity(encodedImage);
HttpPut httpPut = new HttpPut("http://10.0.2.2:8080/xxx/rest/report/put");
httpPut.setEntity(entity);
HttpResponse response = httpClient.execute(httpPut);
HttpEntity resp = response.getEntity();
}catch(Exception e){
e.printStackTrace();
}
}
我正在使用 Android Studio 的虚拟设备,它在使用 GET 方法时工作正常,但在使用 POST/PUT 时失败。
我没有收到任何异常,该方法甚至没有到达服务器。当我使用 EntityUtils 查看响应中的内容时,我得到一个空字符串。
在 Postman 上复制 url 并交换“http://10.0.2.2:8080" to "http://localhost:8080”正在到达服务器,但不是来自我的 Android 应用程序。
感谢帮助!
如你所说
Copying the url and swapping "http://10.0.2.2:8080" to "http://localhost:8080" on Postman is reaching the server but not from my Android application.
我假设你的网络服务和你的邮递员在同一台机器上。那是你的问题。
您需要创建一个服务器,将服务暴露给您的本地网络端口。或者您需要 public IP 托管服务。
您的代码没问题,它需要public仅通过 Internet 或本地网络公开并获取 Network 路径。只有这样它才会起作用。
您现在无法在其他网络中找到您的 PC,可以吗?这是客户端服务器通信和网络的基础知识。
您必须检查您的 android 工作室虚拟设备和您的服务器是否在同一子网中,以便您可以访问那里。
您必须将网络从您的计算机公开到您的虚拟设备才能使其正常工作。
我正在尝试使用 Web 服务将图像从 Android 应用程序发送到 Java 应用程序。
在服务器端,我有这段代码,使用 Postman 作为客户端时它工作正常:
@Path("/report")
public class ReportService {
@PUT
@Path("/put")
@Consumes(MediaType.APPLICATION_JSON)
public Report saveReport(String json){
return ReportDAO.saveReport(json);
}
另一方面,我的 android 应用程序正在尝试将图像上传到服务器:
try {
//Encode the image
String imagenPath = params[1];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap myBitmap = BitmapFactory.decodeFile(imagenPath);
myBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
//Create client,set url and entity
HttpClient httpClient = new DefaultHttpClient();
StringEntity entity = new StringEntity(encodedImage);
HttpPut httpPut = new HttpPut("http://10.0.2.2:8080/xxx/rest/report/put");
httpPut.setEntity(entity);
HttpResponse response = httpClient.execute(httpPut);
HttpEntity resp = response.getEntity();
}catch(Exception e){
e.printStackTrace();
}
}
我正在使用 Android Studio 的虚拟设备,它在使用 GET 方法时工作正常,但在使用 POST/PUT 时失败。
我没有收到任何异常,该方法甚至没有到达服务器。当我使用 EntityUtils 查看响应中的内容时,我得到一个空字符串。
在 Postman 上复制 url 并交换“http://10.0.2.2:8080" to "http://localhost:8080”正在到达服务器,但不是来自我的 Android 应用程序。
感谢帮助!
如你所说
Copying the url and swapping "http://10.0.2.2:8080" to "http://localhost:8080" on Postman is reaching the server but not from my Android application.
我假设你的网络服务和你的邮递员在同一台机器上。那是你的问题。
您需要创建一个服务器,将服务暴露给您的本地网络端口。或者您需要 public IP 托管服务。
您的代码没问题,它需要public仅通过 Internet 或本地网络公开并获取 Network 路径。只有这样它才会起作用。
您现在无法在其他网络中找到您的 PC,可以吗?这是客户端服务器通信和网络的基础知识。
您必须检查您的 android 工作室虚拟设备和您的服务器是否在同一子网中,以便您可以访问那里。
您必须将网络从您的计算机公开到您的虚拟设备才能使其正常工作。